Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Wednesday, 20 July 2011

Program for Client - Server communication to perform Echo Opreration using UDP in Java


     The DatagramSocket object is used to establish communication between Client and Server.
client
import java.io.*;
import java.net.*;
import java.util.*;
class Client1
{
            public static void main(String args[])
            {
            try
            {
                        DatagramSocket client=new DatagramSocket();

                        InetAddress addr=InetAddress.getByName("127.0.0.1");
                        byte[] sendbyte=new byte[1024];
                        byte[] receivebyte=new byte[1024];
                        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("Enter the String:");
                        String str=in.readLine();
                        sendbyte=str.getBytes();
                        DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,1309);
                        client.send(sender);
                        DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
                        client.receive(receiver);
                        String s=new String(receiver.getData());
                        System.out.println(s.trim());   
                        client.close();
            }
            catch(Exception e)
            {
                        System.out.println(e);
            }
            }
}
Server
import java.io.*;
import java.net.*;
import java.util.*;
class Server1
{
            public static void main(String args[])
            {
            try
            {
                        DatagramSocket server=new DatagramSocket(1309);
                        while(true)
                        {
                                    byte[] sendbyte=new byte[1024];
                                    byte[] receivebyte=new byte[1024];
                                    DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
                                    server.receive(receiver);
                                    String str=new String(receiver.getData());
                                    System.out.println(str.trim());
                                    InetAddress addr=receiver.getAddress();
                                    int port=receiver.getPort();
                                    sendbyte=str.getBytes();
                                    DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
                                    server.send(sender);

                        }
            }
            catch(Exception e)
            {
                        System.out.println(e);
            }
            }
}
Output
E:\networks>javac Client1.java
E:\networks>javac Server1.java
E:\networks>java Client1
Enter the String:
hai
hai
E:\networks>java Server1
Hai

No comments:

Post a Comment

Followers