Telnet按字符发送字符串 [英] Telnet send string by character

查看:1842
本文介绍了Telnet按字符发送字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的代码可以向服务器发送消息.问题是,如果我在Wireshark中捕获了通信,则从我的应用程序发送的字符串消息看起来像这样:

I wrote code can send a message to server. Problem is if I capture communication in Wireshark my string message sent from my application looks like this:

hello    - 1 packet

如果我检查从Telnet cmd终端发送的相同消息,则消息看起来像这样:

If I check the same message sent from Telnet cmd terminal message looks like this:

h      - 1 packet
e      - 1 packet
l      - 1 packet
l      - 1 packet
o      - 1 packet

因此,最终它按字符发送了整个字符串.服务器可以从cmd Telnet终端读取消息并进行回复,但无法读取从我的应用程序发送的消息.有什么方法可以发送这样的字符串吗?我不是在编程服务器.我仅在客户端编程,因此重要的是服务器必须能够正确读取消息.非常感谢!

So finally it sent entire string by character. Server can read the message from cmd Telnet terminal and reply but can't read the message sent from my application. Is there some way how can I send string like this? I'm not programming server. I'm programming only client side so it's important server has to able read the message correctly. Thanks a lot!

PrintWriter out;
BufferedReader in;

 public void run() {
                // TODO Auto-generated method stub
                try {
                    InetAddress serverAddr = InetAddress.getByName(hostname);

                    // create a socket to make the connection with the server
                    socket = new Socket(serverAddr, port);
                    Log.i("Terminal", "Socket connecting");

                    try {

                        // send the message to the server
                        out = new PrintWriter(
                                new BufferedWriter(new OutputStreamWriter(
                                        socket.getOutputStream())), true);

                        Log.i("Terminal", "Connected.");
                        // receive the message which the server sends back
                        in = new BufferedReader(new InputStreamReader(
                                socket.getInputStream()));

                     }catch...
               }catch...
 }

//send code
public void sendMessage(String message) {
        if (out != null && !out.checkError()) {
            out.println(message);
            out.flush();
            Log.i("Terminal", "Message sent.");
        }
}

推荐答案

为什么不只是单独发送每个字符?

Why not just send each character separately?

类似这样的东西:

public void sendMessage(String message) {
    for (String ch : message.split(""))
        sendPacket(ch);
    sendPacket("\r\n");
}

public void sendPacket(String payload) {
    if (out != null && !out.checkError()) {
        out.print(payload);
        out.flush();
        Log.i("Terminal", "Message sent.");
    }
}

您说一切都很好,但是如果将来确实遇到数据包合并问题,则可以通过添加以下行来禁用Nagle算法:

You said everything was working fine, but if you do run in to issues with packet coalescing in the future you can disable the Nagle algorithm by adding this line:

socket.setTcpNoDelay(true);

在此之后:

socket = new Socket(serverAddr, port);

这篇关于Telnet按字符发送字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆