Wednesday, July 6, 2016

Advanced Java Lab Program UDP Client Server Communication

Aim:  Write programs for Datagram server and Client interaction as per given below.
             i. A program to create Datagram server to send a message to client.
             ii. A program to create Datagram client to receive the message sent by the server.


Theory:

Datagrams

For most of your internetworking needs, you will be happy with TCP/IP-style
networking. It provides a serialized, predictable, reliable stream of packet data. This is
not without its cost, however. TCP includes many complicated algorithms for dealing
with congestion control on crowded networks, as well as pessimistic expectations
about packet loss. This leads to a somewhat inefficient way to transport data.
Datagrams provide an alternative.
Datagrams are bundles of information passed between machines. They are
somewhat like a hard throw from a well-trained but blindfolded catcher to the third
baseman. Once the datagram has been released to its intended target, there is no
assurance that it will arrive or even that someone will be there to catch it. Likewise,
when the datagram is received, there is no assurance that it hasn’t been damaged in

transit or that whoever sent it is still there to receive a response.

Java implements datagrams on top of the UDP protocol by using two classes: The
DatagramPacket object is the data container, while the DatagramSocket is the
mechanism used to send or receive the DatagramPackets.
DatagramPacket
DatagramPacket defines several constructors. Four are described here. The first
constructor specifies a buffer that will receive data, and the size of a packet. It is used
for receiving data over a DatagramSocket. The second form allows you to specify an
offset into the buffer at which data will be stored. The third form specifies a target
address and port, which are used by a DatagramSocket to determine where the data in
the packet will be sent. The fourth form transmits packets beginning at the specified
offset into the data. Think of the first two forms as building an “in box,” and the second
two forms as stuffing and addressing an envelope. Here are the four constructors:

DatagramPacket(byte data[ ], int size)
DatagramPacket(byte data[ ], int offset, int size)
DatagramPacket(byte data[ ], int size, InetAddress ipAddress, int port)
DatagramPacket(byte data[ ], int offset, int size, InetAddress ipAddress, int port)


There are several methods for accessing the internal state of a DatagramPacket.
They give complete access to the destination address and port number of a packet, as
well as the raw data and its length. Here are some of the most commonly used
:
InetAddress getAddress( )            Returns the destination InetAddress, typically
                                                   used for sending.
int getPort( )                                Returns the port number.
byte[ ] getData( )                         Returns the byte array of data contained in the
                                                   datagram. Mostly used to retrieve data from the
                                                   datagram after it has been received.
int getLength( )                            Returns the length of the valid data contained in
                                                   the byte array that would be returned from the
getData( )                                   This typically does not equal the length of the whole byte                                                        array.


Server Program:

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.io.IOException;
public class udpsocketserver
{
DatagramSocket socket=null;
public udpsocketserver()
{
}
public void createAndListenSocket()
{
try
{
socket=new DatagramSocket(9876);
byte[]incomingData=new byte[1024];
while(true)
{
DatagramPacket incomingPacket=new DatagramPacket(incomingData,incomingData.length);
socket.receive(incomingPacket);
String message= new String(incomingPacket.getData());
System.out.println("Received message from client:"+message);
InetAddress IpAddress=incomingPacket.getAddress();
int port=incomingPacket.getPort();
String reply= "thank you for the message";
byte[] data= reply.getBytes();
DatagramPacket replyPacket= new DatagramPacket(data,data.length,IpAddress,port);
socket.send(replyPacket);
Thread.sleep(2000);
socket.close();
}
}
catch(SocketException e)
{
e.printStackTrace();
}
catch(IOException i)
{
i.printStackTrace();
}
catch(InterruptedException e)
{
e.printStackTrace();
}

}

public static void main(String args[])
{
udpsocketserver server=new udpsocketserver();
server.createAndListenSocket();
}
}





Client Program:


import java.net.*;
import java.io.IOException;

public class udpsocketclient
{
DatagramSocket socket=null;
public udpsocketclient()
{
}
public void createAndListenSocket()
{
try
{
socket=new DatagramSocket();
InetAddress IpAddress=InetAddress.getByName("localhost");
byte[]incomingData=new byte[1024];
String sentence= "this is message from client";
byte[] data= sentence.getBytes();
DatagramPacket sendPacket= new DatagramPacket(data,data.length,IpAddress,9876);
socket.send(sendPacket);
System.out.println("Message sent from client:");
DatagramPacket incomingPacket= new DatagramPacket(incomingData,incomingData.length);
socket.receive(incomingPacket);
String response=new String(incomingPacket.getData());
System.out.println("Response from server:"+response);
socket.close();
}

catch(UnknownHostException e)
{
e.printStackTrace();
}
catch(SocketException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}


}

public static void main(String args[])
{
udpsocketclient client=new udpsocketclient();
client.createAndListenSocket();
}
}





Output:






Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home