我正在为我的项目设计一个通信系统。但是,我在处理数据包时遇到了一些麻烦。我的通信系统是使用ServerSocket,Socket,BufferedReader和PrintWriter在java.net中编写的。
目前,我的系统每500ms侦听一次数据包。它处理登录数据包很好,但它无法处理登录后的数据包(发送ID,断开连接)。每隔500ms,我需要我的服务器来监听登录和登录后的数据包(发送id,断开连接)。我如何设计一种检测和处理登录后数据包的方法?登录数据包由握手代码,用户名和密码组成。登录后数据包(打印ID,断开连接)包括握手代码,用户ID,事件ID和其他事件参数。
使用bufferedreader和printwriter作为一行文本发送和接收数据包。
这是我的服务器端解码器
protected void decode(Socket server) throws Exception {
final BufferedReader reader = new BufferedReader(new InputStreamReader(server.getInputStream()));
final int handshake = Integer.parseInt(reader.readLine());
if (handshake == LOGIN) {
final Session session = new LoginSession(server, reader);
session.read(-1);
} else if (handshake == POST_LOGIN) {
final int index = Integer.parseInt(reader.readLine());
final int opcode = Integer.parseInt(reader.readLine());
final Session session = store.get(index).getSession();
session.reader = reader;
session.read(opcode);
}
}
@Override
public void run() {
Socket server;
try {
server = serverSocket.accept();
server.setTcpNoDelay(false);
decode(server);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这些是我的客户 - >服务器数据包
public void sendLogin() {
write.write("100");
write.write('\n');
write.write("raees2");
write.write('\n');
write.write("LOL2");
write.write('\n');
write.flush();
}
public void sendPrintId() throws NumberFormatException, IOException {
final BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
final int id = Integer.parseInt(reader.readLine());
System.out.println(id);
write.write("101");
write.write('\n');
write.write(Integer.toString(id));
write.write('\n');
write.write(Integer.toString(0));
write.write('\n');
write.write(Integer.toString(10003));
write.write('\n');
write.flush();
}
public void sendDisconnection() throws NumberFormatException, IOException {
final BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
final int id = Integer.parseInt(reader.readLine());
System.out.println(id);
write.write("101");
write.write('\n');
write.write(Integer.toString(id));
write.write('\n');
write.write(Integer.toString(1));
write.write('\n');
write.flush();
}
答案 0 :(得分:1)
基本上,您只需要一个循环来继续从流中读取,直到对话完成。现在,它会读取一条消息并终止。
我不确定你的协议是如何工作的,但这样的事情可以给出一个起点:
protected void decode(Socket server) throws Exception {
final BufferedReader reader = new BufferedReader(new InputStreamReader(server.getInputStream()));
/*
* Somewhere in this loop, detect your protocol's termination condition
* and break out.
*/
for (;;) {
final int handshake = Integer.parseInt(reader.readLine());
if (handshake == LOGIN) {
final Session session = new LoginSession(server, reader);
session.read(-1);
} else if (handshake == POST_LOGIN) {
final int index = Integer.parseInt(reader.readLine());
final int opcode = Integer.parseInt(reader.readLine());
final Session session = store.get(index).getSession();
session.reader = reader;
session.read(opcode);
}
}
}
如果您需要同时处理多个连接,您可能希望您的acceptor线程为成功连接的每个连接启动一个新的客户端线程,因为decode
将在等待下一条消息时阻塞。