Steps for creating a receiver:
1. Create an initramsocket object and specify the listening port number.
Datagramsocket socket = new datagramsocket (4567 );
2. Create a byte array for receiving
Byte data [] = new byte [1024];
3. Create an empty initrampackage object
Required rampackage package = new required rampackage (data, data. Length );
4. Use the receive method to receive the data sent by the sender, which is also a blocking method.
Socket. Receive (Package );
5. Get the sent data
New String (package. getdata (), package. getoffset (), package. getlength ());
Sender creation steps:
1. Create a initramsocket object
Datagramsocket socket = new datagramsocket (4567 );
2. Create an inetaddress, equivalent to the address
Inetaddress serveraddress = inetaddress. getbyname ("the IP address to be sent ");
3. This is to send a random data
String STR = "hello ";
4. Convert to byte type
Byte data [] = Str. getbytes ();
5. Create an initrampacket object and specify the address to which the packet is sent to the network and the port number.
Datagrampacket package = new datagrampacket (data, data. length, serveraddress, 4567 );
6. Call the send method of the initramsocket object to send data.
Socket. Send (Package );