TCP数据的顺序错误和不完整偶尔收到 [英] TCP data occasionally received in wrong order and incomplete

查看:395
本文介绍了TCP数据的顺序错误和不完整偶尔收到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Java中创建TCP服务器应用程序,并在C#客户端应用程序。当我发送数据时,客户有时接收数据失灵,有时部分完全错过了。基本上,我在服务器(JAVA)使用的代码看起来像这样(剥离):

I have created TCP Server application in Java, and a client application in C#. When i am sending data, the client sometimes receives data out of order, and sometimes parts miss entirely. Basically, the code i use in the server (java) looks like this (stripped):

ServerSocket welcomeSocket = new ServerSocket(port);
Socket connectionSocket = welcomeSocket.accept();

outputStream = new DataOutputStream(socket.getOutputStream()); //Create stream
outputStream.writeBytes(message + "\n");
outputStream.flush();



我用\\\
作为分隔符。在客户端(C#)我用下面的代码:

I use "\n" as a delimiter. On the client side (C#) i use the following code:

private const char Delimiter = '\n';

tcpclnt = new TcpClient();
tcpclnt.NoDelay = true;
tcpclnt.Client.DontFragment = true;
tcpclnt.Connect(ip, port);

//This function is executed in a separate thread
public void Receive() 
{
try
{
    stream = tcpclnt.GetStream();
    streamreader = new StreamReader(stream);
    this.Connected = true;
    while (Connected)
    {
        string line = ReadLine(streamreader);
        Console.WriteLine("Received data: " + line);
    }
}
}

private string ReadLine(StreamReader reader)
{
    bool finished = false;
    string line = "";

    while (finished == false)
    {
        int asciiNumber = reader.Read();
        char character = Convert.ToChar(asciiNumber);

        if (!character.Equals(Delimiter))
            line += character;
        else finished = true;
    }

    return line;
}



中的代码也不是很复杂。然而,从服务器发送的数据不总是正确地在客户端接收。举个例子,我会收到以下两个字符串:
5_8_1和6_LEVELDATA

The code is not very complicated. However, the data sent from the server is not always received correctly in the client. As an example, I should receive the following two strings: "5_8_1" and "6_LEVELDATA"

我所得到的(有时),但是,是这样的: 5_8_61和_LEVELDATA

What i get (sometimes) however, is this: "5_8_61" and "_LEVELDATA"

又如:5_4_1和6_LEVELDATA的结果在一个单一的字符串:5_6_LEVELDATA

Another example: "5_4_1" and "6_LEVELDATA" result in one single string: "5_6_LEVELDATA"

这似乎有些小问题,但它实际上确实非常毁了我的申请。我看了很多帖子,但我看过的唯一答案要么是这不应该与TCP发生或发送TCP报文首长度,这将不会在这种情况下,任何帮助,因为这个问题ISN 't对数据的多个包被分裂,它根本没有到达正确的顺序,这是一件好事TCP应该做的。

This seems like some small problem, but it does in fact pretty much ruin my application. I have read a lot of posts, but the only answers i have read are either "this shouldnt happen with TCP" or "send the length of the tcp message first" which would not help in any way in this case, because the problem isn't the data being split up in multiple packages, it simply isn't arriving in the right order, which is something TCP should do.

我肯定100%是由Java应用程序发送之前字符串总是完整的。

I am 100% sure the string is always complete before it is being sent by the Java application.

我真的不知道我在做什么错在这里。是什么弄乱了不好的在我的代码?我很感激这个问题的任何帮助。先谢谢了。

I really wonder what i'm doing wrong here. Is something messed up bad in my code? I would appreciate any help with this problem. Thanks in advance.

推荐答案

Wireshark的努力后,它会出现在服务器中存在我的问题。显然,每一个TCP消息是在一个单独的线程发送。感谢您对所有您的意见!我现在的问题就解决了。

After trying Wireshark, it appears my problem existed in the server. Apparently every TCP-message was sent in a seperate thread. Thank you for all of your comments! My problem is solved now.

这篇关于TCP数据的顺序错误和不完整偶尔收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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