Programming based on UDP protocol in Java

xiaoxiao2021-03-06  74

Network programming in Java is relatively easy, because Java.net packages in J2SE have been packaged in a variety of communication protocols. This article mainly tells how to write applications based on UDP (User Dataset) protocol.

Usually we are using Socket-based TCP / IP programming, after all, TCP / IP applications are very broad. For example, we browse the Internet is based on HTTP protocol, and we send emails through the SMTP protocol. They are all based on TCP / IP. The most important thing for TCP / IP is that it ensures that data arrives at the destination, and UDP does not guarantee accurate transmission, data is possible to lose. If you are interested, the reader can refer to the "Computer Network" book.

It is necessary to introduce an important class inetaddress before introducing UDP programming, describe the role of this class with the simplest sentence is: it represents an IP address. This is very important in the Internet If you know the IP address, we know the endpoint of the communication. This class does not have a constructor, but there are several factory methods. By transmitting different parameters such as IP, Hostname, etc., the instance of inetaddress, the following small example can get the IP address of my machine. Import java.net. *;

public class TestNet {public static void main (String [] args) throws Exception {InetAddress ia = InetAddress.getByName ( "compaq"); String ipAdr = ia.getHostAddress (); System.out.println (ipAdr);}} course My machine's name is Compaq, and if you get to LocalHost, you will get 127.0.0.1.

Next, let's talk about how to use UDP programming, which is very easy to understand, we should first construct a datagram, then send it out, and we can also receive the datagram. DataGrampacket and DataGramsocket are provided in Java to complete such tasks, the former is responsible for constructing the latter latter to send and receive. See DatagramPacket constructor DatagramPacket (byte [] buf, int length, InetAddress address, int port) DatagramPacket (byte [] buf, int offset, int length, InetAddress address, int port) DatagramPacket (byte [] buf, int offset , int length, SocketAddress address) DatagramPacket (byte [] buf, int length, SocketAddress address) DatagramPacket (byte [] buf, int length) DatagramPacket (byte [] buf, int offset, int length) for the first four of which are configured The sended datagram is used because they have inetaddress or socketinetaddress as the address of the receiving endpoint, and then one is to accept data report.

Similarly, we write an example of a C / S model to explain how to use these two important classes. If you are not familiar with the API, please refer to Java DOC. The following program makes a time server at this institution, and the client gets time. A similar time server program has been previously written, but this is based on UDP programming. Import java.io. *; import java.Net. *; Import java.util. *;

public class TimeServer {final private static int DAYTIME_PORT = 13; public static void main (String args []) throws IOException {DatagramSocket socket = new DatagramSocket (DAYTIME_PORT); while (true) {byte buffer [] = new byte [256]; DataGrampacket Packet = New DataGrampacket (Buffer, Buffer.Length); Socket.Receive (Packet); string Date = new date (). Tostring (); buffer = Date.getbytes (); // Get Response Address / Port // for Client from packet inetaddress address = packet.getaddress (); int port = packet.get port (); packet = new database, buffer, buffer.length, address, port); socket.send (packet);}}} import Java. IO. *; import java.net. *;

转载请注明原文地址:https://www.9cbs.com/read-120301.html

New Post(0)