我正在使用 RUDP 协议使用这个非常有用的 java 库来发送和接收数据包,它在 java 中实现了 RUDP 协议。该库的设计与 TCP 非常相似。它比较使用 aReliableServerSocket
作为 ServerSocket 和 aReliableSocket
作为 Socket。
但是,当我创建与服务器的客户端连接时,我确实偶然发现了一个错误。服务器和客户端之间的连接已成功创建,因为所有通过 accept() 方法的内容都已执行。但是,输入流在尝试从中读取时不包含任何字节。
客户:
public class Client {
ReliableSocket rs;
public Client() throws IOException {
rs = new ReliableSocket();
rs.connect(new InetSocketAddress("127.0.0.1", 3033));
String message = "Hello";
byte[] sendData = message.getBytes();
OutputStream os = rs.getOutputStream();
os.write(sendData, 0, sendData.length);
}
public static void main(String[] args) throws IOException {
new Client();
}
}
服务器:
public class Server {
ReliableServerSocket rss;
ReliableSocket rs;
public Server() throws Exception {
rss = new ReliableServerSocket(3033);
while (true) {
rs = (ReliableSocket) rss.accept();
System.out.println("Client connected");
byte[] buffer = new byte[100];
InputStream in = rs.getInputStream();
in.read(buffer);
//here the value doesn't return from the inputstream
System.out.println("message from client: " + new String(buffer).trim());
}
}
public static void main(String[] args) throws Exception {
new Server();
}
}