Sunday 18 September 2011

DATE/TIME INFORMATION SERVER AND CLIENT USING UDP


DATE/TIME  INFORMATION SERVER AND CLIENT USING UDP
AIM
 
To get time and date information from the server over a User Datagram Protocol(UDP) Using Socket Programming.

PROBLEM DESCRIPTION

The User Datagram Protocol (UDP) is a protocol for sending data over IP and it is unreliable protocol.  In UDP, there is no way of knowing whether data received and data arrived in the order in which you sent them.

COMMAND DESCRIPTION

DatagramPacket uses different constructors depending on whether the packet will be used to send  data or to receive data.  These two constructors create new DatagramPacket ohects for receiving data from the network.

DatagramPacket( byte[] buffer, int length)
DatagramPacket(byte [] buffer, int offset, int length);

When a socket receives a datagram, it stores the datagram’s data in buffer beginning at buffer[0] and continuing until the packet is completely stored or until length bytes have been written into the buffer. If  the second constructor is used, storage begins at buffer[offset] instead.  Otherwise, these two constructors are identical.  Length must be less that or equal to buffer.length-offset.

 InetAddress getAddress()

 The getAddress() method returns an InetAddress object containing the address of the remote host.  I f the datagram was received from the Internet, the address returned is the address of he machine that sent it. On the other hand, if the datagram was created locally to be sent to a remote machine, this method returns the address of the host to which the datagram is addressed.

 int getport()

The getport() method returns an integer specifying the remote port. If this datagram was received from the internet, this is the port on the host that sent the packet. If the datagram was created locally to be sent to a remote host, this is the port to which the packet is addresseed on the remote machine.

Socketaddress getsocketaddress()

The getsocketaddress() method returns the socket address object containing the IP address and port of the remote host. As is the case of the getinetaddress(), if the datagram was received from the internet, the address of the machine that sent it.

Byte[] getdata()

The getdata() method returns a byte array containing the data from the datagram.

int getlength()

The getlength() method returns the number of bytes of data in the datagram. This is not necessarily the same as the length of the array returned by getdata().length. The int  returned by the getlength() may be less than the length of the array returned by getdata().

 int getoffset()

This method simply returns the point in the array returned by getdata() where the data from the datagram begins.

SET METHODS

setdata(byte[] data)

The satdata() method changes the payload of the UDP datagram. It is used while sending large files to remote host.

setdata(byte[] data, int offset, int length)

The overload variant of the setdata() method provides as alternative approach to sending a large quantity of data.

setaddress(inetaddress remote)

The setaddress() method changes the address a datagram packet is sent to. This might allow you to sent the same datagram to many different recipients.

 setport( int port)

The setport() method changes the port a datagram is addressed to.

setaddress( socket address remote)

The setsocketaddress() method changes the address and port a datagram packet is sent to.

setlength(int length)

The setlength method changes the number of bytes of datain the internal buffer that are considered to be part of the Datagrams data as opposed to merely unfilled space.
 
ALGORITHM

CLIENT PROGRAM

Step 1: Create a datagram socket clientsocket for the client through the port  of the
server.
Step 2:  Get the IP address of the server using inetaddress.getbyname(host name) method.
Step 3:  Get the input from the user and sent the datagram packets to the server using  
send method.
Step 4: Wait for the server response.
Step 5: Get the Date/Time packet from the server.
Step 6: After receiving all the packets close the socket connection and print the content.

SERVER PROGRAM

Step 1: Open the datagram socket sersoc with the port number .
Step 2:  Get the address, port number and data packets from the client
Step 3: Get the packets from the client and process the packet.
Step 4: Sent the Date/Time packet back to the client.
Step 5: Print the received packet.

CODE

TimeServer

import java.net.*;
import java.io.*;
import java.util.*;
public class userver
{
public static void main(String args[]) throws Exception
{
DatagramSocket ds=new DatagramSocket();
DatagramPacket dp;
InetAddress addr;
byte b[]=new byte[512];
addr=InetAddress.getByName("localhost");
while(true)
{
String s=(new Date()).toString();
b=s.getBytes();
dp=new DatagramPacket(b,b.length,addr,3000);
ds.send(dp);
}
}
}


TimeClient

import java.io.*;
import java.net.*;
import java.util.*;

public class uclient
{
public static void main(String args[]) throws Exception
{
DatagramSocket ds;
DatagramPacket dp;
byte b[]=new byte[512];
ds=new DatagramSocket(3000);
dp=new DatagramPacket(b,b.length);
ds.receive(dp);
String s=new String(dp.getData());
System.out.println("date"+s);
}
}




OUTPUT

SERVER
$ cd webtec
$ javac userver.java
$ java userver

CLIENT
$ cd webtec
$ javac uclient.java
$ java uclient
dateTue May 19 11:00:23 GMT+05:30 2009
[csme0802@pgoracle webtec]$

0 comments:

Post a Comment