0% found this document useful (0 votes)
13 views3 pages

UDP Chat Application Implementation

This document contains code for a chat application using UDP protocol. It includes code for a MyClientUDP class that implements a UDP client that can send and receive messages from a server. It also includes code for a MyServerUDP class that implements a UDP server that can receive and send messages back to clients. The client and server can communicate back and forth to chat until either side sends a "bye" message to end the connection.

Uploaded by

Dhrumin Patel
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)
13 views3 pages

UDP Chat Application Implementation

This document contains code for a chat application using UDP protocol. It includes code for a MyClientUDP class that implements a UDP client that can send and receive messages from a server. It also includes code for a MyServerUDP class that implements a UDP server that can receive and send messages back to clients. The client and server can communicate back and forth to chat until either side sends a "bye" message to end the connection.

Uploaded by

Dhrumin Patel
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

Practical 3 (b):

Aim: To create chat application using UDP protocol.

[Link] :

import [Link];
import [Link];
import [Link];
import [Link];

/**
*
* @author Dhrumin
*/
public class MyClientUDP {

public static void main(String[] args) throws Exception {

DatagramSocket clientsocket = new DatagramSocket();

Scanner sc = new Scanner([Link]);

while(true)
{
byte[] sendbuff = new byte[1024];
byte[] receivebuff = new byte[1024];
[Link]("Client : ");
String clientdata = [Link]();
sendbuff = [Link]();
DatagramPacket sendpacket = new

DatagramPacket(sendbuff, [Link],
[Link]("localhost"), 9999);

[Link](sendpacket);

if([Link]("bye"))
{
[Link]("Connection ended by Clieent");
break;
}

DatagramPacket receivepacket = new DatagramPacket(receivebuff,


[Link]);

[Link](receivepacket);

String serverdata = new String([Link]());

[Link]("Server : " + serverdata);


}
[Link]();

}
}

[Link] :

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

/**
*
* @author Dhrumin
*/
public class MyServerUDP {

public static void main(String[] args) throws Exception {

DatagramSocket serversocket = new DatagramSocket(9999);

BufferedReader br = new BufferedReader(new


InputStreamReader([Link]));

while(true)
{
byte[] sendbuff = new byte[1024];
byte[] receivebuff = new byte[1024];
DatagramPacket receivepkt = new DatagramPacket(receivebuff,
[Link]);

[Link](receivepkt);

InetAddress ip = [Link]();
int portno = [Link]();
String clientdata = new String([Link]());
[Link]("Client : " + clientdata);

[Link]("Server : " );
String serverdata = [Link]();
sendbuff = [Link]();

DatagramPacket sendpacket = new DatagramPacket(sendbuff,


[Link] , ip , portno );

[Link](sendpacket);

if([Link]("bye"))
{
[Link]("Connection ended by server !");
break;
}

}
[Link]();
[Link]();

}
}

Output:

You might also like