Here is a simple code of chat between two peers. According to me, the code does what it should have but I am facing difficulty solving this SocketException error. This code worked fine till i made the last modifications. And now I am finding it difficult to trace the error.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class UDPchat extends Thread implements ActionListener
	JFrame f;
	JPanel p;
	JTextField text;
	JButton b;
	JTextArea ta;
	private final static String newline = "\n";
   private final static BufferedReader in = new BufferedReader(new InputStreamReader(;
   int porttor=7777; // port to send/receive datagrams on
   //int porttos=5555;
   String remoteIPaddress= ("");      // IP to send datagrams

   public UDPchat() throws Exception
    start();		// start thread to receive and display datagrams
    f=new JFrame("CHAT WINDOW");
    p= new JPanel();
    text = new JTextField();
    b = new JButton("SEND");
    ta = new JTextArea("Chat messages",20,50);
    f.setLayout(new FlowLayout());
    f.setLayout(new BorderLayout());
    f.setSize(400, 400);
    // loop waiting for keyboard input, send datagram to remote IP                
   public void actionPerformed(ActionEvent e)
		   String s = text.getText();					// read a String
	       text.setText(" ");
		   //System.out.println("Sending to " + remoteIPaddress + " socket " + port + " data: " + s);
	       byte[] data = s.getBytes();                               	// convert to byte array
	       DatagramSocket theSocket = new DatagramSocket();                // create datagram socket and the datagram
	       DatagramPacket   theOutput = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 5555);
	   catch(Exception et)
		   System.out.println("Error sending datagram" + et);

   // thread run method, receives datagram and display contents as a string
   public void run()                
              // open DatagramSocket to receive 
              DatagramSocket ds = new DatagramSocket(7777);
              // loop forever reading datagrams from the DatagramSocket
              while (true)
                 byte[] buffer = new byte[65507];        			// array to put datagrams in
                 DatagramPacket dp = new DatagramPacket(buffer, buffer.length);	// DatagramPacket to hold the datagram
                 try {
				} catch (IOException e) {
					// TODO Auto-generated catch block
					System.err.println("chat error2 " + e);
				}                     				// wait for next datagram
                 String s = new String(dp.getData(),0,dp.getLength());		// get contenets as a String
                 System.out.println("UDP datagram length " + s.length()+ "  from IP " + dp.getAddress() + " received: " + s );
          catch (SocketException se)
        	  System.err.println("chat error1 " + se);
          //catch (IOException se) {System.err.println("chat error2 " + se);}
          //System.exit(1);                                                 	// exit on error

public static void main(String args[]) throws Exception
   UDPchat c=new UDPchat();


I think you got problem with port numbers. In one place you have 7777, but in other 5555.
Not shore, but it is what i think.

This question has already been answered. Start a new discussion instead.