我正在编写这个小实用工具方法来测试将原始数据包发送到特定的消息传递网络(计划开发客户端以连接到它)。
网络是Deviantart消息传递网络(chat.deviantart.com:3900; TCP)。
我的课程:
protected void connect() throws IOException{
Socket dAmn = null;
//BufferedWriter out = null;
PrintWriter out = null;
BufferedReader in = null;
/*
* Create Socket Connection
*/
try{
dAmn =
new Socket("chat.deviantart.com", 3900);
/*out =
new BufferedWriter(new OutputStreamWriter(dAmn.getOutputStream()));*/
out =
new PrintWriter(dAmn.getOutputStream(), true);
in =
new BufferedReader(new InputStreamReader(dAmn.getInputStream()));
}
catch(SocketException e){
System.err.println("No host or port for given connection");
//handle
}
catch(IOException e){
System.err.println("I/O Error on host");
//handle
}
String userInput;
BufferedReader userIn =
new BufferedReader(new InputStreamReader(System.in));
/*
* dAmn communication
*/
while((userInput = userIn.readLine()) != null){
out.write(userInput);
System.out.println(in.readLine());
}
if(in!=null)
in.close();
if(out!=null)
out.close();
if(dAmn!=null)
dAmn.close();
}
服务器要求在登录进行之前发送握手。典型的登录数据包如下所示:
dAmnclient damnClient (目前为0.3) agent = 代理
每个数据包必须以换行符结束并且为空。
我的握手包看起来像是:
dAmnClient 0.3 \ nagent = SomeAgent \ n \ 0
但是服务器只是回复 disconnect
我认为某些内容被错误地解析了,有什么建议吗?另外,如果你非常有兴趣帮助我:这里有一些关于客户的快速文档 - >服务器dAmn协议: http://botdom.com/wiki/DAmn#dAmnClient_.28handshake.29
答案 0 :(得分:4)
你应该使用Wireshark
使用Wireshark,您可以嗅探来自/到主机的流量。它使您可以很容易地发现应用程序在标准客户端之外的其他位置。
顺便说一句,你在agent =前面有一个\ n,这可能是问题
答案 1 :(得分:0)
从用户读取的行不包含实际的行终止,也不包含任何空终止。在输入处键入\ n实际上将传输“\ n”而不是换行符。
您可以通过用println替换write来添加换行符(注意,它可能会使用\ n,\ r \ n或者仅使用\ r \ n,具体取决于平台):
out.println(userInput);
您可以支持数据包终止,例如通过检查特定的用户输入,如下所示:
if (userInput.equals(".")) {
out.write((char) 0);
out.flush();
} else {
out.println(userInput);
}
用户现在可以通过键入点来终止数据包。
(实际上,代码可以自动执行握手而无需等待用户输入,但这是另一个故事。)