0% found this document useful (0 votes)
62 views5 pages

Exp 5

This document provides code for a simple chat application using UDP datagram sockets and packets. It includes code for both a server and client. The server code initializes a datagram socket, receives and displays messages from clients. The client code initializes a socket, takes user input, sends it to the server and receives/displays the server responses. The code demonstrates basic UDP communication between a server and client for chatting.

Uploaded by

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

Exp 5

This document provides code for a simple chat application using UDP datagram sockets and packets. It includes code for both a server and client. The server code initializes a datagram socket, receives and displays messages from clients. The client code initializes a socket, takes user input, sends it to the server and receives/displays the server responses. The code demonstrates basic UDP communication between a server and client for chatting.

Uploaded by

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

Aim:

To write a program for creating simple chat application using datagram sockets and datagram
packets.
Program:
Server:
import java.net.*;
import java.io.*;
public class server10
{
public static DatagramSocket ds;
public static int clientPort=777,serverPort=666;
public static byte buff[]=new byte[1024];
public static void main (String[] args) throws Exception
{
ds=new DatagramSocket(serverPort);
BufferedReader dfs=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Server is waiting..”);
InetAddress in=InetAddress.getByName(“localhost”);
System.out.println(“Enter a choice 1 or 2”);
int cho;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
cho=Integer.parseInt(br.readLine());
switch(cho)
{
case 1:
while(true)
{
String s=dfs.readLine();
if(s==null||s.equals(“ends”))
break;
buff=s.getBytes();
ds.send(new DatagramPacket(buff,s.length(),in,clientPort));
}
break;
case 2:
while(true)
{
DatagramPacket p=new DatagramPacket(buff,buff.length);
ds.receive(p);
String s1=new String(p.getData(),0,p.getLength());
System.out.println(s1);
}
}
}
}
import java.net.*;
import java.io.*;
class client10
{
public static DatagramSocket ds;
public static int clientPort=777,serverPort=666;
public static byte buff[]=new byte[1024];
public static void main(String args[]) throws Exception
{
ds=new DatagramSocket(clientPort);
System.out.println(“Client is waiting for Server to send data…..”);
InetAddress in=InetAddress.getLocalHost();
int cho;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter a choice 1 or 2”);
cho=Integer.parseInt(br.readLine());
switch(cho)
{
case 1:
while(true)
{
DatagramPacket p=new DatagramPacket(buff,buff.length);
ds.receive(p);
String s1=new String(p.getData(),0,p.getLength());
System.out.println(s1);
}
case 2:
while(true)
{
String s=br.readLine();
if(s==null||s.equals(“ends”))
break;
buff=s.getBytes();
ds.send(new DatagramPacket(buff,s.length(),in,serverPort));
}
break;
}
}
}
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("Serv er 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));
}
}
}

Output:-
C:\IPLAB>javac UDPServer.java
C:\IPLAB>java UDPServer
Server is Running...
Client: Hello
Welcome
Terminated...
source code java programming UDP 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 UDP Chat Client


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

You might also like