Categories
TE Computer Engineering

11. Write a program using UDP sockets for wired network to implement a. Peer to Peer Chat b. Multiuser Chat Demonstrate the packets captured traces using Wireshark Packet Analyzer Tool for peer to peer mode.

Download write-up here

Multi user Chat
Client.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.net.UnknownHostException;

public class Client implements Runnable
{
static MulticastSocket clientSocket ;
static byte[] recvData = new byte[1024];
static String getsen=”hmmm”;
static boolean send_flag=false;
public static void main(String[] args)
{
// TODO Auto-generated method stub
BufferedReader br ;
try
{
clientSocket = new MulticastSocket(4447);
InetAddress group = InetAddress.getByName(“230.0.0.1”);
InetAddress IPAddr = InetAddress.getByName(“localhost”);
clientSocket.joinGroup(group);
byte[] sendData = new byte[1024];
String sentence=null;
new Thread(new Client()).start();
//SEND DATA
while(true)
{
sentence=null;
br= new BufferedReader(new InputStreamReader(System.in));
sentence=br.readLine();
sendData = new byte[1024];
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,sendData.length,IPAddr,9877);
clientSocket.send(sendPacket);
send_flag = true;
}

}
catch (SocketException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
//READ FROM SERVER
public void run()
{
// TODO Auto-generated method stub
DatagramPacket recvPacket;
while(!getsen.equals(“Bye”))
{
try
{
recvPacket = new DatagramPacket(recvData,recvData.length);
clientSocket.receive(recvPacket);
getsen = new String(recvPacket.getData());
if(send_flag!=true)
System.out.println(“BROADCASTED MESSAGE:”+getsen);
getsen= ” “;
send_flag = false;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}

Server.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class Server
{
static byte[] recvData = new byte[1024];
static byte[] sendData = new byte[1024];
static DatagramSocket ds;
static InetAddress group;
public static void main(String[] args) { // TODO Auto-generated method stub
try {
ds = new DatagramSocket(9877);
group = InetAddress.getByName(“230.0.0.1”);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); DatagramPacket recvPack;
while(true) {
recvData = new byte[1024];
try {
recvPack = new DatagramPacket(recvData,recvData.length);
ds.receive(recvPack);
String sentence = new String(recvPack.getData());
System.out.println(sentence); method(sentence);
}
catch (IOException e)
{ // TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SocketException e) { // TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
} public static void method(String sentence) { // TODO Auto-generated method stub sendData = sentence.getBytes();
DatagramPacket sendPack = new DatagramPacket(sendData,sendData.length,group,4447);
try {
ds.send(sendPack);
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Peer to Peer Chat
Client.java

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

public class PeerClient {
public final static int PORT=8778;
public final static String SERVER_IP=”127.0.0.1″;
public static void main(String[] args) {
BufferedReader br=null;
DatagramSocket socket=null;
String s;
try {
socket=new DatagramSocket();
br=new BufferedReader(new InputStreamReader(System.in));
InetAddress server=InetAddress.getByName(SERVER_IP);
System.out.println(“Client Socket Created.\nENter msg:”);

while(true) {
System.out.print(“>”);
s=br.readLine();
byte[] sendMsg=s.getBytes();
DatagramPacket sendingPacket=new DatagramPacket(sendMsg, sendMsg.length,server,PORT);
socket.send(sendingPacket);
System.out.println(“Waiting for reply.”);
byte[] replyMsg=new byte[1000];
DatagramPacket receivingPacket=new DatagramPacket(replyMsg, replyMsg.length); socket.receive(receivingPacket);
byte[] data=receivingPacket.getData();
String s1=new String(data, 0,data.length); System.out.println(receivingPacket.getAddress().getHostAddress()+”:”+receivingPacket.getPort()+”=> “+s1);
if(s1.equalsIgnoreCase(“bye”)) { System.out.println(“Connection Closed”);
break;
}
} socket.close();
} catch (Exception e) { // TODO: handle exception } }}

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

public class PeerServer {
public static void main(String[] args) {
DatagramSocket socket=null;
try { socket=new DatagramSocket(8778);
//server socket byte[] buffer=new byte[1000]; //buffer for msg DatagramPacket incomingPacket=new DatagramPacket(buffer, buffer.length); //packet to receive data System.out.println(“Server Socket Created.\nWaiting for data..\n”);

while(true) {
socket.receive(incomingPacket);
byte[] msg=incomingPacket.getData();
String s=new String(msg,0,msg.length); System.out.println(incomingPacket.getAddress().getHostAddress()+”:”+incomingPacket.getPort()+” > “+s);
if(s.equalsIgnoreCase(“bye”))
{ String reply=new String(“Bye”);
msg=reply.getBytes();
DatagramPacket sendPacket=new DatagramPacket(msg, msg.length,incomingPacket.getAddress(),incomingPacket.getPort()); socket.send(sendPacket); break;
}
else {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print(“>”);
s=br.readLine();
msg=s.getBytes();
DatagramPacket sendPacket=new DatagramPacket(msg, msg.length,incomingPacket.getAddress(),incomingPacket.getPort()); socket.send(sendPacket);
}
}
socket.close();
} catch(Exception e) { } }
}

Leave a Reply

Your email address will not be published. Required fields are marked *