UDP 数据报已发送但从未收到
UDP datagram is sent but never received
我为客户端和服务器做了两个不同的 java 类,每个都有发送和接收方法。根据任务,我必须通过 DatagramPacket
建立服务器到客户端的连接,通过 DatagramChannel
建立客户端到服务器的连接。最后一个效果很好,但我在处理数据报时遇到了麻烦——它们是从服务器发送的,从未在客户端接收过。怎么了?
public class TransferClient implements Serializable{
private static final long serialVersionUID = 26L;
public TransferClient() {
}
public void send(Command com) throws IOException {
SocketAddress socketAddressChannel = new InetSocketAddress("localhost", 1);
DatagramChannel datagramChannel=DatagramChannel.open();
ByteBuffer bb;
datagramChannel.connect(socketAddressChannel);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(com);
oos.flush();
bb=ByteBuffer.wrap(baos.toByteArray(),0, baos.size());
datagramChannel.send(bb,socketAddressChannel);
baos.close();
oos.close();
}
public Command receive() throws IOException, ClassNotFoundException {
SocketAddress address = new InetSocketAddress(2029);
DatagramSocket s = new DatagramSocket();
DatagramPacket inputPacket = new DatagramPacket(new byte[1024],1024, address);
s.receive(inputPacket);
ByteArrayInputStream bais = new ByteArrayInputStream(inputPacket.getData());
ObjectInputStream ois = new ObjectInputStream(bais);
return (Command) ois.readObject();
}
}
public class TransferServer {
public TransferServer(){
}
public void send(Command com) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
byte[] buffer;
oos.writeObject(com);
oos.flush();
buffer=baos.toByteArray();
SocketAddress address = new InetSocketAddress("localhost",2029);
DatagramSocket s = new DatagramSocket();
DatagramPacket outputPacket = new DatagramPacket(buffer,buffer.length,address);
s.send(outputPacket);
}
public Command receive() throws IOException, ClassNotFoundException {
SocketAddress socketAddressChannel = new InetSocketAddress("localhost", 1);
DatagramChannel datagramChannel = DatagramChannel.open();
datagramChannel.bind(socketAddressChannel);
ByteBuffer bb = ByteBuffer.allocate(1024);
datagramChannel.receive(bb);
ByteArrayInputStream bais = new ByteArrayInputStream(bb.array());
ObjectInputStream ois = new ObjectInputStream(bais);
Command command;
System.out.println(ois.available());
command =(Command) ois.readObject();
datagramChannel.close();
ois.close();
bais.close();
return command;
}
}
您必须指定绑定 DatagramSocket
的端口才能接收您的数据。否则它将绑定到您计算机上找到的第一个可用端口。
此处不需要SocketAddress
,但需要考虑实际接收到的数据包长度。
见java.net.DatagramSocket Documentation
在你的 TransferClient
class 的 receive()
方法中:
public Command receive() throws IOException, ClassNotFoundException {
DatagramSocket s = new DatagramSocket(2029); //Add your port Here
DatagramPacket inputPacket = new DatagramPacket(new byte[1024],1024);
s.receive(inputPacket);
ByteArrayInputStream bais = new ByteArrayInputStream(inputPacket.getData(), 0, packet.getLength());
ObjectInputStream ois = new ObjectInputStream(bais);
return (Command) ois.readObject();
}
我为客户端和服务器做了两个不同的 java 类,每个都有发送和接收方法。根据任务,我必须通过 DatagramPacket
建立服务器到客户端的连接,通过 DatagramChannel
建立客户端到服务器的连接。最后一个效果很好,但我在处理数据报时遇到了麻烦——它们是从服务器发送的,从未在客户端接收过。怎么了?
public class TransferClient implements Serializable{
private static final long serialVersionUID = 26L;
public TransferClient() {
}
public void send(Command com) throws IOException {
SocketAddress socketAddressChannel = new InetSocketAddress("localhost", 1);
DatagramChannel datagramChannel=DatagramChannel.open();
ByteBuffer bb;
datagramChannel.connect(socketAddressChannel);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(com);
oos.flush();
bb=ByteBuffer.wrap(baos.toByteArray(),0, baos.size());
datagramChannel.send(bb,socketAddressChannel);
baos.close();
oos.close();
}
public Command receive() throws IOException, ClassNotFoundException {
SocketAddress address = new InetSocketAddress(2029);
DatagramSocket s = new DatagramSocket();
DatagramPacket inputPacket = new DatagramPacket(new byte[1024],1024, address);
s.receive(inputPacket);
ByteArrayInputStream bais = new ByteArrayInputStream(inputPacket.getData());
ObjectInputStream ois = new ObjectInputStream(bais);
return (Command) ois.readObject();
}
}
public class TransferServer {
public TransferServer(){
}
public void send(Command com) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
byte[] buffer;
oos.writeObject(com);
oos.flush();
buffer=baos.toByteArray();
SocketAddress address = new InetSocketAddress("localhost",2029);
DatagramSocket s = new DatagramSocket();
DatagramPacket outputPacket = new DatagramPacket(buffer,buffer.length,address);
s.send(outputPacket);
}
public Command receive() throws IOException, ClassNotFoundException {
SocketAddress socketAddressChannel = new InetSocketAddress("localhost", 1);
DatagramChannel datagramChannel = DatagramChannel.open();
datagramChannel.bind(socketAddressChannel);
ByteBuffer bb = ByteBuffer.allocate(1024);
datagramChannel.receive(bb);
ByteArrayInputStream bais = new ByteArrayInputStream(bb.array());
ObjectInputStream ois = new ObjectInputStream(bais);
Command command;
System.out.println(ois.available());
command =(Command) ois.readObject();
datagramChannel.close();
ois.close();
bais.close();
return command;
}
}
您必须指定绑定 DatagramSocket
的端口才能接收您的数据。否则它将绑定到您计算机上找到的第一个可用端口。
此处不需要SocketAddress
,但需要考虑实际接收到的数据包长度。
见java.net.DatagramSocket Documentation
在你的 TransferClient
class 的 receive()
方法中:
public Command receive() throws IOException, ClassNotFoundException {
DatagramSocket s = new DatagramSocket(2029); //Add your port Here
DatagramPacket inputPacket = new DatagramPacket(new byte[1024],1024);
s.receive(inputPacket);
ByteArrayInputStream bais = new ByteArrayInputStream(inputPacket.getData(), 0, packet.getLength());
ObjectInputStream ois = new ObjectInputStream(bais);
return (Command) ois.readObject();
}