**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.io.*;
import java.util.*;
import java.net.*;
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(System.in));
int porttor=7777; // port to send/receive datagrams on
//int porttos=5555;
String remoteIPaddress= ("127.0.0.1"); // 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();
text.setColumns(25);
b = new JButton("SEND");
b.addActionListener(this);
ta = new JTextArea("Chat messages",20,50);
ta.setEditable(false);
f.setLayout(new FlowLayout());
p.add(text,BorderLayout.NORTH);
p.add(b,BorderLayout.SOUTH);
f.setLayout(new BorderLayout());
f.add(ta,BorderLayout.NORTH);
f.add(p,BorderLayout.SOUTH);
f.setSize(400, 400);
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
try
{
String s = text.getText(); // read a String
text.setText(" ");
ta.append(newline);
ta.append(s);
//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);
theSocket.send(theOutput);
}
catch(Exception et)
{
System.out.println("Error sending datagram" + et);
}
}
// thread run method, receives datagram and display contents as a string
public void run()
{
try
{
// 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 {
ds.receive(dp);
} 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 contents as a String
System.out.println("UDP datagram length " + s.length()+ " from IP " + dp.getAddress() + " received: " + s );
ta.append(newline);
ta.append(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();
}
}
Check this you making two types of DatagramSockets in your code, try to provide both the same 7777 port. One in your actionPerformed() Method as :
and the one in your run Method as :
The Upper constructor is creating a datagram socket and binding it to any available port on the local host machine, and the second one is trying to reach port 7777. Try to initialize both by same port number, that will sort things out for you.
Hope that might help in some way.
Regards