Practical 17: Write a program to demonstrate use of DataGramSocket and DataGram Packet

1) Write a program using DatagramPacket and DatagramSocket to create chat application
Ans:
ServerSideData.java:
import java.net.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ServerSideData
{
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(2019);

        byte[] receiveData = new byte[512];
        byte[] sendData = new byte[512];

        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in)
        );

        System.out.println(" UDP Server Socket is created, waiting for client ");

        do
        {
            DatagramPacket receiveDP = new DatagramPacket(receiveData,receiveData.length);
            ds.receive(receiveDP);

            String clientMessage = new String(receiveDP.getData(),0,receiveDP.getLength());
            System.out.println("Client Message:"+clientMessage);

            InetAddress ip = receiveDP.getAddress();

            System.out.print("\n\nEnter Server Message:");
            String serverMessage = br.readLine();
            sendData = serverMessage.getBytes();
            DatagramPacket sendDP = new DatagramPacket(sendData, sendData.length, ip, receiveDP.getPort()); 
            ds.send(sendDP);
            
            receiveData = new byte[512];
        }while(true);

    }
}    


ClientSideData.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;

public class ClientSideData
{
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket();

        byte[] receiveData = new byte[512];
        byte[] sendData = new byte[512];

        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in)
        );

        System.out.println(" UDP Client Socket is created, waiting for server ");

        InetAddress ip = InetAddress.getLocalHost();

        do
        {
            System.out.print("\nEnter Client Message:");
            String clientMessage = br.readLine();
            sendData = clientMessage.getBytes();

            DatagramPacket sendDP = new DatagramPacket(sendData, sendData.length, ip, 2019); 
  
            ds.send(sendDP);

            DatagramPacket receiveDP = new DatagramPacket(receiveData,receiveData.length);
            ds.receive(receiveDP);

            String serverMessage = new String(receiveDP.getData(),0,receiveDP.getLength());

            System.out.println("\n\nServer Message:"+serverMessage);
        }while(true);

        
    }
}


Output:
Client Side and Server Side Chat using UDP


2) Write a program using DatagramPacket and DatagramSocket to copy the contents of one file into other.

ServerFile.java:
import java.net.*;
import java.io.*;
public class ServerFile
{
            public static void main(String args[])throws IOException
            {
                        byte b[]=new byte[3072];
                        DatagramSocket dsoc=new DatagramSocket(2019);
                        FileOutputStream f=new FileOutputStream("F:\\Project\\Java\\Advanced Java\\4 Networking Basics\\File\\SFile.txt");
                        
                        
                                    DatagramPacket dp=new DatagramPacket(b,b.length);
                                    dsoc.receive(dp);
                                    String data = new String(dp.getData(),0,dp.getLength());  
                                    f.write(data.getBytes(), 0, data.length());
                        
            }
}


ClientFile.java:
import java.net.*;
import java.io.*;
public class ClientFile
{
            public static void main(String args[])throws Exception
            {         
                        byte b[] = new byte[1024];
                        FileInputStream f = new FileInputStream("F:\\Project\\Java\\Advanced Java\\4 Networking Basics\\File\\CFile.txt");
                        DatagramSocket dsoc = new DatagramSocket();
                        int i=0;
                        while(f.available() != 0)
                        {
                                    b[i]=(byte)f.read();
                                    i++;
                        }                     
                        f.close();
                        dsoc.send(new DatagramPacket(b,i,InetAddress.getLocalHost(),2019));
            }

}


Output:
File Copy using UDP


File Output Copy


Previous
Next Post »