我正在学习Socket编程,我想设计一个简单的程序,它能够在2个端口之间发送和接收消息。我使用相同的IP地址127.0.0.1,它应该是localhost,但设置2个不同的端口,代表两个不同的人互相发送消息。我打开2个终端来运行Class Demo2和Class Demo2B,以便在两个类之间发送和接收消息。但是,我看不到收到的任何消息。我可以知道我的代码出了什么问题吗?
这是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");
}
}
}
}
}
这是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");
}
}
}
}
}
我希望看到的是,一旦我分别运行这两个类,如果我从Class Demo2发送消息到Class Demo2B,Demo2B应该能够捕获数据并显示它。与reverse相同,我可以从Class Demo2B回复到Class Demo2,Class Demo2也会捕获数据并显示它。如果有人能帮我解决这个困惑,我真的很感激。
答案 0 :(得分:1)
在Demo2
和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();
// This sould be new Thread(new receive(receiveSocket)).start();
new Thread(new send(receiveSocket)).start();
}
因此,在以下两个类中更改以下代码:将解决问题:
new Thread(new send(sendSocket)).start();
new Thread(new receive(receiveSocket)).start();
因为你只能将这两个类之间的差异设为port
,所以你可以简单地将一个类和供应端口作为一个参数来监听。