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.