java 通过 TCP 套接字发送数据包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5286105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending packets over TCP socket
提问by Francis
I'm writing this tiny utility method to test sending raw packets to a specific messaging network (planning on developing a client to connect to it).
我正在编写这个微小的实用程序方法来测试将原始数据包发送到特定的消息传递网络(计划开发一个客户端来连接到它)。
The network is the Deviantart messaging network (chat.deviantart.com:3900; TCP).
该网络是 Deviantart 消息传递网络 (chat.deviantart.com:3900; TCP)。
My class:
我的课:
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();
}
The server requires a handshake to be sent before the login may proceed. A typical login packet looks like thus:
服务器要求在登录可以继续之前发送握手。典型的登录数据包如下所示:
dAmnclient damnClient(currently 0.3) agent= agent
dAmnclient damnClient(目前0.3)剂=剂
Every packet must end with a newline and a null.
每个数据包都必须以换行符和空值结尾。
My handshake packet would look something like:
我的握手包看起来像:
dAmnClient 0.3\nagent=SomeAgent\n\0
dAmnClient 0.3\nagent=SomeAgent\n\0
However the server simply replies with disconnect
然而,服务器只是回复断开连接
I think something is incorrectly being parsed, any advice? Also, if you're super intersted in helping me out: here's some quick documentation on the client -> server dAmn protocol: http://botdom.com/wiki/DAmn#dAmnClient_.28handshake.29
我认为有些东西被错误地解析了,有什么建议吗?另外,如果你非常有兴趣帮助我:这里有一些关于客户端 -> 服务器 dAmn 协议的快速文档:http://botdom.com/wiki/DAmn#dAmnClient_.28handshake.29
回答by Eric
You should use Wireshark
你应该使用 Wireshark
With Wireshark you can sniff traffic from/to hosts. It makes it really easy to spot where your application does something else than the standard client.
使用 Wireshark,您可以嗅探来自/到达主机的流量。它可以很容易地发现您的应用程序在何处执行了标准客户端以外的其他操作。
BTW you have a \n in front of agent=, it might be the problem
顺便说一句,您在 agent= 前面有一个 \n,这可能是问题所在
回答by volley
The line read from the user will not contain the actual line termination, and it will not contain any null-termination either. Typing \n at the input will actually transmit "\n" rather than a new-line.
从用户读取的行将不包含实际的行终止,也不包含任何空终止。在输入中键入 \n 实际上会传输 "\n" 而不是换行符。
You could add a new-line by replacing write with println (careful, it may use \n, \r\n or just \r depending on platform):
您可以通过用 println 替换 write 来添加新行(注意,它可能使用 \n、\r\n 或仅使用 \r,具体取决于平台):
out.println(userInput);
You could support packet termination e.g. by checking for a specific user input, like so:
您可以通过检查特定的用户输入来支持数据包终止,如下所示:
if (userInput.equals(".")) {
out.write((char) 0);
out.flush();
} else {
out.println(userInput);
}
The user can now terminate packets by typing a dot.
用户现在可以通过键入一个点来终止数据包。
(Actually the code could perform the handshake automatically without waiting for user input, but that's another story.)
(实际上,代码可以在不等待用户输入的情况下自动执行握手,但那是另一回事了。)