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

5a. Chat Application Using UDP

1. The document describes a chat application created using UDP sockets in Java that allows a client and server to exchange messages. 2. The server opens a socket to receive client details, and the client sends its address and messages to the server. The server and client can then send messages back and forth until stopping the connection. 3. The algorithm and code for the UDP server and client are provided to demonstrate how they initialize sockets, send and receive data packets, and continue exchanging messages in a loop until one side sends a stop message.

Uploaded by

Poda sunni
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)
280 views3 pages

5a. Chat Application Using UDP

1. The document describes a chat application created using UDP sockets in Java that allows a client and server to exchange messages. 2. The server opens a socket to receive client details, and the client sends its address and messages to the server. The server and client can then send messages back and forth until stopping the connection. 3. The algorithm and code for the UDP server and client are provided to demonstrate how they initialize sockets, send and receive data packets, and continue exchanging messages in a loop until one side sends a stop message.

Uploaded by

Poda sunni
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

Exp.

No: 5 (a) Chat Application using UDP Socket


Date:

AIM
Server gets the message and opens the server socket to read the client details. Client sends its
address to the server. Then client sends the message to the server and vice versa. This simple chat
application implements in java using UDP socket classes.

ALGORITHM

Chat Application Server:


1. Import the required class packages from java toolkit.
2. Initialize the server with the user-defined application port number.
3. Create an interface using DatagramSocket class and check whether the client is accepted by
the server.
4. If so continue with step 5 else quit the program.
5. Read the data from the socket and display it in the server.
6. Read the server response for the client data and place it in the socket.
7. Repeat the steps 5 and 6 until user gives “STOP”.

Chat Application Client:


1. Import the required class packages from java toolkit.
2. Initialize the client with the user-defined application port number.
3. Read the data from user with the help of BufferedReader class and place it on the socket using
PrintWriter class.
4. Read the server response from the socket and display it on the console.
5. Repeat the steps 3 and 4 until user gives “STOP”.

FLOWCHART

<< Draw the flow chart as your own for both Server and Client >>

PROGRAM

Chat Server:
import java.io.*;
import java.net.*;
class UDPServer
{
public static DatagramSocket serversocket;
public static DatagramPacket dp;
public static BufferedReader dis;
public static InetAddress ia;
public static byte buf[] = new byte[1024];
public static int cport = 789,sport=790;
public static void main(String[] a) throws IOException
{
serversocket = new DatagramSocket(sport);
dp = new DatagramPacket(buf,buf.length);
dis = new BufferedReader(new InputStreamReader(System.in));
ia = InetAddress.getLocalHost();
System.out.println("Server is Running...");
while(true)
{
serversocket.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
if(str.equals("STOP"))
{
System.out.println("Terminated...");
break;
}
System.out.println("Client: " + str);
String str1 = new String(dis.readLine());
buf = str1.getBytes();
serversocket.send(new DatagramPacket(buf,str1.length(), ia, cport));
}
}
}

Chat Client:
import java.io.*;
import java.net.*;
class UDPClient
{
public static DatagramSocket clientsocket;
public static DatagramPacket dp;
public static BufferedReader dis;
public static InetAddress ia;
public static byte buf[] = new byte[1024];
public static int cport = 789, sport = 790;
public static void main(String[] a) throws IOException
{
clientsocket = new DatagramSocket(cport);
dp = new DatagramPacket(buf, buf.length);
dis = new BufferedReader(new InputStreamReader(System.in));
ia = InetAddress.getLocalHost();
System.out.println("Client is Running... Type 'STOP' to Quit");
while(true)
{
String str = new String(dis.readLine());
buf = str.getBytes();
if(str.equals("STOP"))
{
System.out.println("Terminated...");
clientsocket.send(new DatagramPacket(buf,str.length(), ia,sport));
break;
}
clientsocket.send(new DatagramPacket(buf,str.length(), ia, sport));
clientsocket.receive(dp);
String str2 = new String(dp.getData(), 0,dp.getLength());
System.out.println("Server: " + str2);
}
}
}
OUTPUT
Output UDP Chat Server
C:\IPLAB>javac UDPServer.java
C:\IPLAB>java UDPServer
Server is Running...
Client: Hello
Welcome
Terminated...

Output UDP Chat Client


C:\IPLAB>javac UDPClient.java
C:\IPLAB>java UDPClient
Client is Running... Type ‘STOP’ to Quit
Hello
Server: Welcome
STOP
Terminated...

RESULT
This experiment created a simple chat application with Java UDP socket classes for sending
messages from client to server and vice-versa. This java application satisfies the first course outcome
because it is implemented using UDP protocol.

You might also like