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

Practical 3 (B) :: Aim: To Create Chat Application Using UDP Protocol

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)
39 views3 pages

Practical 3 (B) :: Aim: To Create Chat Application Using UDP Protocol

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
You are on page 1/ 3

Practical 3 (b):

Aim: To create chat application using UDP protocol.

MyClientUDP.java :

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

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

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

DatagramSocket clientsocket = new DatagramSocket();

Scanner sc = new Scanner(System.in);

while(true)
{
byte[] sendbuff = new byte[1024];
byte[] receivebuff = new byte[1024];
System.out.print("Client : ");
String clientdata = sc.nextLine();
sendbuff = clientdata.getBytes();
DatagramPacket sendpacket = new

DatagramPacket(sendbuff, sendbuff.length,
InetAddress.getByName("localhost"), 9999);

clientsocket.send(sendpacket);

if(clientdata.equals("bye"))
{
System.out.println("Connection ended by Clieent");
break;
}

DatagramPacket receivepacket = new DatagramPacket(receivebuff,


receivebuff.length);

clientsocket.receive(receivepacket);

String serverdata = new String(receivepacket.getData());

System.out.println("Server : " + serverdata);


}
clientsocket.close();

}
}

MyServerUDP.java :

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

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

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

DatagramSocket serversocket = new DatagramSocket(9999);

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));

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

serversocket.receive(receivepkt);

InetAddress ip = receivepkt.getAddress();
int portno = receivepkt.getPort();
String clientdata = new String(receivepkt.getData());
System.out.println("Client : " + clientdata);

System.out.print("Server : " );
String serverdata = br.readLine();
sendbuff = serverdata.getBytes();

DatagramPacket sendpacket = new DatagramPacket(sendbuff,


sendbuff.length , ip , portno );

serversocket.send(sendpacket);

if(serverdata.equals("bye"))
{
System.out.println("Connection ended by server !");
break;
}

}
serversocket.close();
br.close();

}
}

Output:

You might also like