Search This Blog

Showing posts with label DatagramSocket. Show all posts
Showing posts with label DatagramSocket. Show all posts

Simplest UDP Chat Program to Run on Single Computer in Java

This is a very simple UDP chat program using java with GUI (Graphical User Interface). The program uses DatagramPackets to send messages over network. Remember, this program only to be run on a single computer (for network simulation purpose). If you want a real UDP program that works on any network, see this post: Chat Room Application using UDP in Java
Program explanation is given below the program.


import java.io.*;
import javax.swing.*;
import java.net.*;
import java.awt.event.*;
import java.util.*;

public class UDPChat extends JFrame implements ActionListener
{
DatagramSocket ds;
JTextField txt;
JButton send;
JTextArea msgs;
int myport;
int destport;
DatagramPacket p;
byte[] b=new byte[200];

public UDPChat()
{
try{
try{
ds=new DatagramSocket(9999);
myport=9999;
p=new DatagramPacket(b,200);
ds.receive(p);
destport=p.getPort();
}catch(Exception ex)
{
ds=new DatagramSocket(0);
myport=ds.getLocalPort();
destport=9999;
String msg="hi";
p=new DatagramPacket(msg.getBytes(),msg.length(),InetAddress.getLocalHost(),destport);
ds.send(p);
}
setLayout(null);
txt=new JTextField();
send=new JButton("Send");
msgs=new JTextArea();
setSize(300,600);
add(txt);
txt.setSize(220,25);
txt.setLocation(5,5);
add(send);
send.setSize(70,25);
send.setLocation(230,5);
add(msgs);
msgs.setSize(300,530);
msgs.setLocation(0,70);
send.addActionListener(this);
receive.start();
}catch(Exception ex){ex.printStackTrace();}
}

public void actionPerformed(ActionEvent ae)
{
try{
DatagramPacket pkt=new DatagramPacket(txt.getText().getBytes(),txt.getText().length(),InetAddress.getLocalHost(),destport);
ds.send(pkt);
}catch(Exception e){e.printStackTrace();}
txt.setText("");
}

Thread receive=new Thread()
{
public void run()
{
try{
while(true)
 {
 DatagramPacket dp=new DatagramPacket(b,200);
 ds.receive(dp);
 msgs.setText(msgs.getText()+"\n"+new String(b));
 }
}catch(Exception exx){exx.printStackTrace();}
}
};

public static void main(String[] args)
{
UDPChat s=new UDPChat();
s.setVisible(true);
}

}

This simple program is enough to run as server or client. When the first user runs the application, it opens a DatagramSocket with port number 9999 (see line 22 in code). Since no Exception will be thrown from this line, it sets myport as 9999. Then waits to receive a DatagramPacket from the other user. When the packet is received, it takes destport from the received packet. Then starts adding GUI elements and then starts the thread to receive messages.

But when the second instance (client) of the program is run, line 22 in code throws a SocketException. Thus execution goes to the catch block. There it opens a DatagramSocket at some random port (ephemeral port assigned by OS). Then, sends a packet to port 9999 in the same PC. This time the first instance (server) is still waiting for a packet as said earlier. It receives this DatagramPacket sent by client a gets its destination port. The client's destport is 9999, as it knew it is already occupied by the SocketException.

The sending of message is carried out when send button is clicked. We use an ActionListener for that. The method public void actionPerformed(ActionEvent ae) is invoked whenever the button is clicked.