DatagramPacket indicates a data packet.
A datagram packet is used to deliver a connectionless package. Each packet is routed only from one machine to another based on the information contained in the packet. Multiple packages sent from one machine to another may have different routes or may arrive in different order. The package delivery is not guaranteed.
Constructor
DatagramPacket (byte [] buf, int length, SocketAddress address)
Constructs a datagram packet to send a packet with a length to the specified port number on the specified host.
// An initrampacket object contains a byte array (used as a buffer for receiving or sending data, where offset and length are operations on this byte array ), the network address and port number of a remote computer.
// A initrampacket object can do two things: 1. as the client sends a datagram to the remote server (in this case, the data to be sent is the data in the buffer) DatagramSocket. send (package); 2. as a server to receive a datagram (the received data is placed in the buffer zone) DatagramSocket. recieve (package );
InetAddress getAddress ()
Returns the IP address of a machine. The datagram is sent to or received from the machine. // Defined network address and port number
Int getPort ()
Returns the port number of a remote host. The datagram is sent to or received from the host.
SocketAddress getSocketAddress ()
Obtain the SocketAddress (usually IP address + port number) of the remote host to which the packet is sent or sent ).
Void setAddress (InetAddress iaddr)
Set the IP address of the machine to which the datagram is sent.
Void setSocketAddress (SocketAddress address)
Set the SocketAddress (usually IP address + port number) of the remote host to which the datagram is sent ).
// Operations on the data in the buffer to be sent or received
Byte [] getData ()
Return data buffer.
Int getLength ()
Returns the length of the data to be sent or received.
Int getOffset ()
Returns the offset of the data to be sent or received.
Void setData (byte [] buf)
Set the data buffer for this package.
Void setData (byte [] buf, int offset, int length)
Set the data buffer for this package.
Void setLength (int length)
Set the length of this package.
Void setPort (int iport)
Set the port number on the remote host to which the datagram is sent.
DatagramSocket indicates the socket used to send and receive data packets.
A datagram socket is the sending or receiving point of the package delivery service. Each packet sent or received on a datagram socket is independently edited and routed.
Mongoramsocket can call bind () to bind to a specific address and port. If the address is null, the system selects a temporary port and a valid local address to bind the socket. You can call connect () to connect to a specific remote address. Then, packets can only be sent or received from this address.
Server: the server is opposite to the client. In fact, the functions are the same on both sides.
Java code
Package com. sea. server;
Import java.net. DatagramPacket;
Import java.net. DatagramSocket;
Import java.net. InetAddress;
Public class Server1 {
Public Server1 (){
While (true ){
Try {
DatagramSocket datagramSocket = new DatagramSocket (12345 );
Byte [] msg = new byte [1, 1000];
Required rampacket packet1 = new required rampacket (msg, msg. length );
// DatagramPacket. setAddress (InetAddress. getByName ("192.168.0.145 "));
// DatagramPacket. setPort (12345 );
// DatagramPacket. setData ("nihao, client". getBytes ());
DatagramSocket. receive (packet1 );
System. out. println ("your own address" + datagramSocket. getLocalAddress ()
+ "" + DatagramSocket. getLocalPort ());
System. out. println ("server:" + new String (packet1.getData ()));
System. out. println ("client network address:" + packet1.getAddress ());
System. out. println ("client port:" + packet1.getPort ());
System. out. println ("\ n ");
// Return data
Required rampacket packet2 = new required rampacket (msg, msg. length );
Packet2.setData ("data received". getBytes ());
Packet2.setAddress (packet1.getAddress ());
Packet2.setPort (packet1.getPort ());
DatagramSocket. send (packet2 );
DatagramSocket. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
Public static void main (String [] args ){
New Server1 ();
}
}
Client:
Java code
Package com. sea. client;
Import java.net. DatagramPacket;
Import java.net. DatagramSocket;
Import java.net. InetAddress;
Import java.net. SocketException;
Import java.net. UnknownHostException;
Public class Client1 {
Public Client1 (){
Try {
DatagramSocket datagramSocket = new DatagramSocket (11111 );
Byte [] msg = new byte [1, 1000];
DatagramPacket datagramPacket = new DatagramPacket (msg, msg. length );
DatagramPacket. setAddress (InetAddress. getByName ("192.168.0.145 "));
Using rampacket. setPort (12345 );
DatagramPacket. setData ("nihao, client". getBytes ());
// Send data
DatagramSocket. send (datagramPacket );
// Receive data
DatagramPacket = new DatagramPacket (msg, msg. length );
DatagramSocket. receive (datagramPacket );
// Retrieve the result
String result = new String (datagramPacket. getData ());
System. out. println ("data returned by the server:" + result );
System. out. println (datagramPacket. getAddress ());
DatagramSocket. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public static void main (String [] args ){
New Client1 ();
}
}
Author: "quding0308 blog"