【问题标题】:TCP connection - strings are recieved mixedTCP 连接 - 接收的字符串是混合的
【发布时间】:2018-09-06 11:20:25
【问题描述】:

我有一个 Java 的 TCP 服务器和一个 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 "";
    }

【问题讨论】:

  • 您发布的 c# 代码无效,in 和 out 是保留关键字。它看起来更像 Java。现在我仔细看,您似乎发布了两次 Java 代码。

标签: java c# tcp


【解决方案1】:

嘿,我帮了自己...

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循环的更好方法... 我发现我正在从客户端以不同的线程发送消息,这可以解释为什么字符串是混合的......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 2019-10-25
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    相关资源
    最近更新 更多