Reputation: 51
I'm learning the Socket Programming and I want to design a simple program, which is able to send and receive messages between 2 ports. I use the same IP address 127.0.0.1 which should be the localhost, but set 2 different ports which represents two different people who are messaging each other. I opened 2 terminals to run Class Demo2 and Class Demo2B in order to send and receive message between two classes. However, I can't see any message received. May I know where is wrong with my code please?
This is Demo2
public class Demo2 {
public static void main(String[] args) throws SocketException {
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receiveSocket = new DatagramSocket(7705); //Set the Demo2 will use port 7705 to receive data
new Thread(new send(sendSocket)).start();
new Thread(new send(receiveSocket)).start();
}
static class send implements Runnable {
private DatagramSocket datagramSocket;
private send(DatagramSocket datagramSocket) {
this.datagramSocket = datagramSocket;
}
@Override
public void run() {
// TODO Auto-generated method stub
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
try {
while ((line=bufferedReader.readLine())!=null) {
if(line.equals("88")) {
break;
}
byte[] buf = line.getBytes();
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 8805); //set the data from Demo2 will be sent to port 8805
datagramSocket.send(datagramPacket);
}
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException("Fail to send");
}
}
}
class receive implements Runnable {
private DatagramSocket datagramSocket;
private receive(DatagramSocket datagramSocket) {
this.datagramSocket = datagramSocket;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
byte[] buf = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
try {
datagramSocket.receive(datagramPacket);
String ip = datagramPacket.getAddress().getHostAddress();
String data = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
System.out.println(ip+":"+data);
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException("Fail to send");
}
}
}
}
}
This is Demo2B
public class Demo2B {
public static void main(String[] args) throws SocketException {
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receiveSocket = new DatagramSocket(8805); //Set the Demo2B will use port 8805 to receive data
new Thread(new send(sendSocket)).start();
new Thread(new send(receiveSocket)).start();
}
static class send implements Runnable {
private DatagramSocket datagramSocket;
private send(DatagramSocket datagramSocket) {
this.datagramSocket = datagramSocket;
}
@Override
public void run() {
// TODO Auto-generated method stub
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
try {
while ((line=bufferedReader.readLine())!=null) {
if(line.equals("88")) {
break;
}
byte[] buf = line.getBytes();
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 7705); //set the data from Demo2 will be sent to port 7705
datagramSocket.send(datagramPacket);
}
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException("Fail to send");
}
}
}
class receive implements Runnable {
private DatagramSocket datagramSocket;
private receive(DatagramSocket datagramSocket) {
this.datagramSocket = datagramSocket;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
byte[] buf = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
try {
datagramSocket.receive(datagramPacket);
String ip = datagramPacket.getAddress().getHostAddress();
String data = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
System.out.println(ip+":"+data);
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException("Fail to send");
}
}
}
}
}
What I expect to see is that once I run these two classes separately, if I send a message from Class Demo2 to Class Demo2B, Demo2B should be able to capture the data and display it. Same as reverse, I'm able to reply from Class Demo2B to Class Demo2, and Class Demo2 also will capture the data and display it. I really appreciate it if someone can help me to solve this confusion.
Upvotes: 0
Views: 49
Reputation: 4502
In both of the class Demo2
and Demo2B
you only initialized send thread. You forget to start received thread.
public class Demo2B {
public static void main(String[] args) throws SocketException {
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receiveSocket = new DatagramSocket(8805); //Set the Demo2B will use port 8805 to receive data
new Thread(new send(sendSocket)).start();
// This sould be new Thread(new receive(receiveSocket)).start();
new Thread(new send(receiveSocket)).start();
}
So changing the following code, in both class: would fix the problem:
new Thread(new send(sendSocket)).start();
new Thread(new receive(receiveSocket)).start();
As you can only differences between this two class is a port
, so you can simply keep a single class and supply port as a parameter to listen.
Upvotes: 1