简体   繁体   English

通过TCP套接字发送数据包

[英]Sending packets over TCP socket

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)agent = 代理

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

You should use Wireshark 您应该使用Wireshark

With Wireshark you can sniff traffic from/to hosts. 使用Wireshark,您可以嗅探主机之间的通信。 It makes it really easy to spot where your application does something else than the standard client. 它非常容易发现您的应用程序在标准客户端以外的其他位置。

BTW you have a \\n in front of agent=, it might be the problem 顺便说一句,您在agent =前面有一个\\ n,可能是问题所在

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 eg 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.) (实际上,代码可以自动执行握手,而无需等待用户输入,但这是另一回事了。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM