java - UDP 数据报已发送但从未收到
问题描述
我为客户端和服务器创建了两个不同的 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 文档
在您的类的receive()
方法中:TransferClient
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();
}
推荐阅读
- ios - 为什么 UIImageview 不是完美的圆圈?
- css - CSS-transitions/transforms (scale) 上的超链接未打开
- sql - SQL,SQL查询
- awk - AWK:如何抑制默认打印
- android - 改造 2 队列等待大小
- android - kotlin 控制流“while”中的错误
- mysql - 将 selectizeInput 中的多个选择传递给 MySQL 查询
- django - 当 debug =false 或在生产中时,Django 提供 CSS 文件和 Favicon,但不提供来自根静态的图像
- c# - 将 Visual Studio 2012 附加到在 Android 移动设备上运行的 Unity 游戏,以进行调试
- sqlite - 如何通过相对路径(无本地路径)在 sqlite 中导入 .txt 文件?