0% found this document useful (0 votes)
958 views4 pages

Practical No 17

The document contains a program to demonstrate the use of DatagramPacket and DatagramSocket to create a chat application. The program contains a server side and client side code. The server side code creates a DatagramSocket to listen on port 2019 and receives and sends DatagramPackets. The client side code creates a DatagramSocket and sends and receives DatagramPackets to communicate with the server. The programs allow sending messages between the client and server in a loop.

Uploaded by

nstrnsdtn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
958 views4 pages

Practical No 17

The document contains a program to demonstrate the use of DatagramPacket and DatagramSocket to create a chat application. The program contains a server side and client side code. The server side code creates a DatagramSocket to listen on port 2019 and receives and sends DatagramPackets. The client side code creates a DatagramSocket and sends and receives DatagramPackets to communicate with the server. The programs allow sending messages between the client and server in a loop.

Uploaded by

nstrnsdtn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Name:- Ayush Maroti Wadje class:-CO5I

Roll No:-76

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:-
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:-
`
2) Write a program using DatagramPacket and DatagramSocket to copy the contents of one
file into other.

Ans:-
ServerFile.java:

You might also like