通过 TCP 套接字发送数据包

发布于 2024-10-21 19:14:15 字数 1817 浏览 2 评论 0原文

我正在编写这个微小的实用方法来测试将原始数据包发送到特定的消息网络(计划开发一个客户端来连接到它)。

该网络是 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= agent

每个数据包必须以换行符和 null 结尾。

我的握手数据包看起来像:

dAmnClient 0.3\nagent=SomeAgent\n\0

但是服务器只是回复 disconnect

我认为某些内容被错误地解析,有什么建议吗?另外,如果您非常有兴趣帮助我:这里有一些有关客户端的快速文档 ->服务器协议: http://botdom.com/wiki/DAmn#dAmnClient_.28handshake.29

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).

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

Every packet must end with a newline and a null.

My handshake packet would look something like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

污味仙女 2024-10-28 19:14:15

您应该使用 Wireshark

通过 Wireshark,您可以嗅探来自/到主机的流量。它可以很容易地发现您的应用程序在哪里执行标准客户端之外的其他操作。

顺便说一句,你在agent=前面有一个\n,这可能是问题所在

You should use 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.

BTW you have a \n in front of agent=, it might be the problem

〗斷ホ乔殘χμё〖 2024-10-28 19:14:15

从用户读取的行将不包含实际的行终止符,也不会包含任何空终止符。在输入中键入 \n 实际上会传输“\n”而不是换行符。

您可以通过用 println 替换 write 来添加换行符(小心,它可能使用 \n、\r\n 或仅 \r,具体取决于平台):

out.println(userInput);

您可以通过检查特定用户输入来支持数据包终止,如下所示:

if (userInput.equals(".")) {
  out.write((char) 0);
  out.flush();
} else {
  out.println(userInput);
}

用户现在可以通过键入点来终止数据包。

(实际上代码可以自动执行握手而无需等待用户输入,但那是另一个故事了。)

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.

You could add a new-line by replacing write with println (careful, it may use \n, \r\n or just \r depending on platform):

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文