无法让 2 个客户端监听一个 UDP 服务器
Can't get 2 clients to listen to one UDP server
我有一个 UDP(服务器)正在从用户接收数据并进行一些计算并将新数据发回,我可以肯定地说服务器正在从第一个客户端以及从第二个但只有第一个客户端正在接收回数据
这是我在两个客户端中的接收方法
private void receive(){
try{
DatagramSocket socket = new DatagramSocket(2390);
byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String msg = new String(buffer,0,packet.getLength());
String[] coor = msg.split(" ");
x = Integer.parseInt(coor[0]);
y = Integer.parseInt(coor[1]);
x1 = Integer.parseInt(coor[2]);
y1 = Integer.parseInt(coor[3]);
packet.setLength(buffer.length);
socket.close();
}catch(Exception e){
e.printStackTrace();
}
}
当第二个客户端尝试使用此接收方法时出现异常:
java.net.BindException: Address already in use: Cannot bind
at java.net.DualStackPlainDatagramSocketImpl.socketBind(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.bind0(Unknown Source)
at java.net.AbstractPlainDatagramSocketImpl.bind(Unknown Source)
at java.net.DatagramSocket.bind(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at Game.receive(Game.java:73)
at Game.<init>(Game.java:58)
at Game.main(Game.java:92)
您应该为客户端使用无参数构造函数new DatagramSocket();
The no-arg constructor is used to create a client that binds to an arbitrary port number. The second constructor is used to create a server that binds to the specific port number, so the clients know how to connect to.
private void receive(){
try{
DatagramSocket socket = new DatagramSocket();
byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String msg = new String(buffer,0,packet.getLength());
String[] coor = msg.split(" ");
x = Integer.parseInt(coor[0]);
y = Integer.parseInt(coor[1]);
x1 = Integer.parseInt(coor[2]);
y1 = Integer.parseInt(coor[3]);
packet.setLength(buffer.length);
socket.close();
}catch(Exception e){
e.printStackTrace();
}
}
此外,套接字应该只创建一次,所以可能不存在,我认为这个 receive
方法在循环中,就像这样...
// here is a good place to init the socket
DatagramSocket socket = new DatagramSocket();
while(true){
//receive();
receive(socket); //pass the socket if it is a local
}
我有一个 UDP(服务器)正在从用户接收数据并进行一些计算并将新数据发回,我可以肯定地说服务器正在从第一个客户端以及从第二个但只有第一个客户端正在接收回数据 这是我在两个客户端中的接收方法
private void receive(){
try{
DatagramSocket socket = new DatagramSocket(2390);
byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String msg = new String(buffer,0,packet.getLength());
String[] coor = msg.split(" ");
x = Integer.parseInt(coor[0]);
y = Integer.parseInt(coor[1]);
x1 = Integer.parseInt(coor[2]);
y1 = Integer.parseInt(coor[3]);
packet.setLength(buffer.length);
socket.close();
}catch(Exception e){
e.printStackTrace();
}
}
当第二个客户端尝试使用此接收方法时出现异常:
java.net.BindException: Address already in use: Cannot bind
at java.net.DualStackPlainDatagramSocketImpl.socketBind(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.bind0(Unknown Source)
at java.net.AbstractPlainDatagramSocketImpl.bind(Unknown Source)
at java.net.DatagramSocket.bind(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at Game.receive(Game.java:73)
at Game.<init>(Game.java:58)
at Game.main(Game.java:92)
您应该为客户端使用无参数构造函数new DatagramSocket();
The no-arg constructor is used to create a client that binds to an arbitrary port number. The second constructor is used to create a server that binds to the specific port number, so the clients know how to connect to.
private void receive(){
try{
DatagramSocket socket = new DatagramSocket();
byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String msg = new String(buffer,0,packet.getLength());
String[] coor = msg.split(" ");
x = Integer.parseInt(coor[0]);
y = Integer.parseInt(coor[1]);
x1 = Integer.parseInt(coor[2]);
y1 = Integer.parseInt(coor[3]);
packet.setLength(buffer.length);
socket.close();
}catch(Exception e){
e.printStackTrace();
}
}
此外,套接字应该只创建一次,所以可能不存在,我认为这个 receive
方法在循环中,就像这样...
// here is a good place to init the socket
DatagramSocket socket = new DatagramSocket();
while(true){
//receive();
receive(socket); //pass the socket if it is a local
}