How To Communicate Between Two Machines Using UDP in Java



Java implements low level communication with TCP/IP and UDP protocol using two main classes

  1. DatagrapPacket- the detailed implementation is here
  2. DatagramSocket-the detailed implementation is here
The DatagramPacket object is the data container,while the DatagramSocket is the mechanism used to send or receive the DatagramPackets
DatagramPackets can be created using one of the two constructors:


public DatagramPacket(bute iBuff[],int iLength);
//constructs a DatagramPacket for a receiving packets of length iLength.The length argument must be
//or equals to iBuff[] length.
public DatagramPacket(bute iBuff[],int iLength,InetAddress iAddress,int portNumber);
//constructs a DatagramPacket for a sending packets of length iLength to the specified port portNumber
//on the specified host iAddress.The length argument must be or equals to iBuff[] length.
 
The methods can be used as follows:

//Methods:
public InetAddress getAddress();//returns the destination InetAddress.It is used to send data
public byte[] getData();//returns the byte array of data contained in the datagram. It is used to retrieve
//data from the datagram after it is received.
public int getLength();//returns the length of the valid data contained in the byte array that would
//be returned from getData() method
public int getPort();//returns the integer destination port number.It is used to send data 
 
DatagramPacket contains no method to send or receive any datagrams. This functionality is given by DatagramSocket class. The first constructor is used to make communication between two ports in the local machines whereas the second constructor is used to communicate between two non localized ports:

public DatagramSocket();
//constructs a DatagramSocket and binds it to any available port on the loaclhost machine
//It may throw SocketException if the socket could not be opened or the socket could not be
//bind to the specified local port.
public DatagramSocket(int port);
//constructs a DatagramSocket and binds it to any available specified port on the loaclhost machine
//It may throw SocketException if the socket could not be opened or the socket could not be
//bind to the specified local port.
 
the useful methods:

public synchronized void close();//To close the datagram socket that is established when the communication 
//is completed
protected void finalize();
public int getLocalPort();
public synchronized void receive(DatagramPacket pck);//takes the  Datagram packet object as a recipient 
//for datagram to be received.
public void send(DatagramPacket pck);//Takes a Datagram packet object and send the datagram's data to the previously
//defined host and port address.
}
 
An Example of Communication between two system via two ports on the local machine:

class CommMyUDP{
public static int ServerPort=666;//Server port
public static int clientPort=999;//Client port
public static int size=1024;//buffer limit
public static DatagramSocket ds;//socket for communication
public static byte buffer[]=new byte[size];//buffer to store content
public static void theServer()
{
int i=0;
while(true)//run until the session ends
{
int c=System.in.read();//read from keyboard
switch(c)
{
case:1
System.out.println("Server Quits");
return;
case:'\n':
ds.send(new DatagramPacket(bufffer,i,InetAddress.getLocalHost(),clientPort));
i=0;
break;
default:
buffer[i++]=bute(c);
}
}
}
public static void theClient()
{
while(true)
{
DatagramPacket pkt=new DatagramPacket(buffer,buffer.length);
ds.receive(pkt);
System.out.println(new String(pkt.getData(),0,0,pkt.getLength()));
}
}
public static void main(String args[])
{
if(args.length==1){
ds=new DatagramSocket(ServerPort);
theServer();
}
else
{
ds=new DatagramSocket(clientPort);
theClient();
}
}
}
}
 
An Example of Communication between two system via two ports on the non localized machines:

import java.net.*;
class MyUDPOutside{
public static int hostPort=999;//port number of the other machine(distant machine)
public static void main(String args[]){
DatagramSocket ds;
DatagramPacket pkt;
InternetAddress hostAddress;
byte buffer[]=new byte[1024];
ds=new DatagramSocket();
hostAddress=InternetAddress.getByName("www.myTargetmachine.com");
pkt=new DatagramPacket(buffer,buffer.length,hostAddress,hostPort);//load some message
ds.send(pkt);//message is transmitted to host
String message=new String(pkt.getData(),0);//if host is sending some message
System.out.println("Message received"+message);
ds.close();
}
}
 
How To Communicate Between Two Machines Using UDP in Java How To Communicate Between Two Machines Using UDP in Java Reviewed by Animesh Chatterjee on October 08, 2018 Rating: 5

No comments:

Powered by Blogger.