Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

java - Telnet send string by character

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

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

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.");
        }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why not just send each character separately?

Something like this:

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

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

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

right after this one:

socket = new Socket(serverAddr, port);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...