Tuesday, May 13, 2014

Local Network Multichat UDP Application using Java without Server

This is a Multichat application inside a Local Network without any centralized Server. It uses MulticastSocket protocol which is UDP based. It transfer data through network as UDP packets. 

I am using PORT 8880 and 224.2.2.3 as IP. Make sure you this Port is available in the network. If not please compile the program with new PORT number. Also make sure the IP address you compile is a MulticastSocket address (Range between 224.0.0.1 and 239.255.255.254).

Run the same program in two different computer in the same network. Any number of system can be connected and you will be able to sent and receive data from all systems. 

Note :
  • Use ##Bye for exit the program
  • Do not use same username for two different systems.
Filename: Multicast.java

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Multicast
{
public static void main(String args[])
{
Login a = new Login();
String un = a.User();
un=un.concat("#><#");
System.out.println("name :"+un);
new Sender(un);
String[] parts = un.split("#><#", 2);
un=parts[0];
System.out.println("Type the word '##Bye' to exit the program");

MulticastSocket socket = null;
DatagramPacket inPacket = null;
byte[] inBuf = new byte[256];
try
{
try
{
//Prepare to join multicast group
//Recever End
socket = new MulticastSocket(8880);
InetAddress address = InetAddress.getByName("224.2.2.3");
//Range between 224.0.0.1 and 239.255.255.254
socket.joinGroup(address);

while (true)
{
inPacket = new DatagramPacket(inBuf, inBuf.length);
socket.receive(inPacket);
String msg = new String(inBuf, 0, inPacket.getLength());
parts = msg.split("#><#", 2);
String SendersUN = parts[0];
msg = parts[1];
if(SendersUN.equals(un))
{
continue;
}
else
{
System.out.println("From : "+SendersUN+ "IP Address : " +inPacket.getAddress()+ "\n Message :"+msg);
}
}
}
catch (IOException ioe)
{
System.out.println(ioe);
}
Thread.sleep(100);
          }
catch (InterruptedException e)
{
System.out.println("Main Reciever thread interrupted.");
}
}
}
class Sender implements Runnable
{
Thread t;
String un;
Sender(String un)
{
t = new Thread(this,"Sender Thread");
t.start();
this.un=un;
}
public void run()
{
try
{
DatagramSocket socket = null;
DatagramPacket outPacket = null;
byte[] outBuf;
final int PORT = 8880;
Scanner in = new Scanner(System.in);
socket = new DatagramSocket();
String msg;
while (true)
{
InetAddress address = InetAddress.getByName("224.2.2.3");
//Range between 224.0.0.1 and 239.255.255.254
msg = in.nextLine();
if(msg.equals("##Bye"))
{
msg="Good Bye";
msg=un.concat(msg);
outBuf = msg.getBytes();
outPacket = new DatagramPacket(outBuf, outBuf.length, address, PORT);
socket.send(outPacket);
socket.close();
System.exit(0);
}
msg=un.concat(msg);
outBuf = msg.getBytes();
outPacket = new DatagramPacket(outBuf, outBuf.length, address, PORT);
socket.send(outPacket);
try
{
Thread.sleep(500);
}
catch (InterruptedException ie)
{
}
}
}
catch (IOException ioe)
{
System.out.println(ioe);
}
}
}
class Login
{
String User()
{
Scanner in = new Scanner(System.in);
String name;
System.out.println("Enter your user name");
name = in.nextLine();
return name;
}
}

No comments:

Post a Comment

UA-39217154-2