TCP连接 - 收到的字符串是混合的

时间:2018-03-27 22:09:08

标签: java c# tcp

我有一个Java服务器和一个C#客户端。连接已经存在并且可以发送一些消息,但是当我向服务器写入许多消息时,当流关闭时,我会收到这些消息

LOCATION|015|0LOCATION|014,99163|0LOCATION|014,89123|0LOCATION|014,81594|0LOCATION|014,78247|0LOCATION|014,74063|0LOCATION|014,69043|0LOCATION|014,63187|0LOCATION|014,56493|0LOCATION|014,56493|0LOCATION|014,3976|0LOCATION|014,3976|0LOCATION|014,2972|0LOCATION|014,18843|0LOCATION|014,0713|0LOCATION|0,000219846914,07641|-0,000412796LOCATION|0,000259

在一行中,而不是一行中的LOCATION|UniversumGames|double;double;double。 可以请别人帮忙吗? P.S.客户不能错,因为我在我的twitchbot中使用了同样的东西......

服务器代码(Java):

public void run(){
    try{
        BufferedReader in = new BufferedReader((new InputStreamReader(socket.getInputStream())));
        PrintWriter out = new PrintWriter(socket.getOutputStream());
        DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());

        out.println("Connected");
        out.flush();
        out.write("Connected 1");
        out.flush();

        while(socket.isConnected()){
            String line = in.readLine();
            //String line = dataInputStream.readUTF();
            if(line != null){
                CommandController.control(new Data(line), socket);
            }
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}

客户代码(c#):

private TcpClient tcpClient;
    private NetworkStream stream;
    private StreamReader reader;
    private StreamWriter writer;
    private bool recieve;


    public ClientConnection()
    {
        this.recieve = true;
        this.tcpClient = new TcpClient("127.0.0.1", 2700);
        Console.WriteLine("Connecting.....");
        // use the ipaddress as in the server program

        Console.WriteLine("Connected");
        this.stream = tcpClient.GetStream();
        this.writer = new StreamWriter(stream);
        this.reader = new StreamReader(stream);

        Console.WriteLine("Transmitting.....");

        this.writer.AutoFlush = true;
        writer.WriteLine("Connected clientside sucessful");
        writer.WriteLine("Connected clientside sucessful 1");
        writer.WriteLine("Connected clientside sucessful 2");
        writer.WriteLine("Connected clientside sucessful 3");
    }

    public async void Recieve()
    {
        await Task.Run(()=>RecieveString());
    }

    public string RecieveString()
    {
        this.recieve = true;
        while (recieve)
        {
            //if (stream.DataAvailable)
            //{
                string data = reader.ReadLine();
                if (data != null)
                {
                writer.WriteLine(data + " recieved");
                writer.Flush();
                    return data;
                }
                else return "";
            //}   
            //else return "";
        }
        return "";
    }

1 个答案:

答案 0 :(得分:0)

嘿,我帮助了自己......

String line = "1";
while(line != null) {
            byte size = dataInputStream.readByte();
            byte[] buf = new byte[size];
            dataInputStream.read(buf, 0, size);
            line = new String(buf);

        }

这是服务器中while循环的更好方法...... 我发现我从客户端发送不同线程的消息,这可以解释为什么字符串是混合的......