简体   繁体   English

BufferedReader在与TCP套接字一起使用时在字符串中提供更多的空字符

[英]BufferedReader give more null character in string while using with TCP Socket

My TCP Server is like this. 我的TCP服务器就是这样。

import java.net.*;
import java.io.*;
public class NetTCPServer {
public static void main(String[] args) throws Exception{

ServerSocket sock;
sock = new ServerSocket(1122);
if(sock == null)
    System.out.println("Server binding failed.");
System.out.println("Server is Ready ..");

do{
    System.out.println("Waiting for Next client.");
    Socket clientSocket = sock.accept();
    if(clientSocket!=null)
        System.out.println("Clinet accepted. "+sock.getInetAddress().getHostAddress());

    DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
    //DataInputStream in = new DataInputStream(clientSocket.getInputStream());
    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    String name;
    String pass;
    String line;
    name = in.readLine();
    pass = in.readLine();
    for(int i=0;i<name.length();i++)
        System.out.print(name.charAt(i)+","); //see more null char are receiving here

        System.out.println("");
        System.out.println(name +"  "+ name.length()+"  \n" + pass+"  "+pass.length());
    }while(true);

}
}

And respective TCP Client is as follows. 以及各自的TCP Client如下。

import java.net.*;
import java.io.*;
public class NetTCPClient {

    public static void main(String[] args) throws Exception {
        InetAddress addr = InetAddress.getByName("localhost");

        Socket sock;
        sock = new Socket(addr,1122);
        if(sock == null)
            System.out.println("Server Connection failed.");
        System.out.println("Waiting for some data...");
        DataInputStream input = new DataInputStream(sock.getInputStream());
        DataOutputStream output = new DataOutputStream(sock.getOutputStream());
        String uname="ram";
        String pass="pass";
        output.writeChars(uname+"\n");// \n is appended just make to readline of server get line
        output.writeChars(pass+"\n");
        }

}

When i compiled both and the server is started and there after client is run, i get following output. 当我同时编译两个文件并启动服务器并运行客户端后,我得到以下输出。

 Server is Ready .. Waiting for Next client. Clinet accepted. 0.0.0.0 ,r,,a,,m,, ram7 pass9 

The null character after each character receive is somewhat strange to me. 每个字符接收后的空字符对我来说有点奇怪。 To make me unable to compare the string with something stored in server. 使我无法将字符串与服务器中存储的内容进行比较。 What is those null characters and where does they come from. 这些空字符是什么,它们从何而来。

You write characters but read lines of bytes . 您写入字符,但读取字节 That won't work. 那行不通。 If you're going to write characters, you need to read characters with precisely the same encoding. 如果要编写字符,则需要以完全相同的编码读取字符。 If you're going to write bytes, you need to read bytes. 如果要写入字节,则需要读取字节。 Specify your protocol precisely at the byte level and follow the specification in both the client and the server. 精确地在字节级别指定协议,并在客户端和服务器中均遵循规范。

See this question for more information. 有关更多信息,请参见此问题

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

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