Home > Net >  The socket TCP packet loss problem
The socket TCP packet loss problem

Time:10-16

I wrote a computer program, to receive the W5500 module to send data (k 1/50 seconds and send 16 bit data) and sends the received data packet loss, after using wireshark caught also confirmed that the problem of packet loss, as shown in the figure below

Here is my application at the receiving end
 private void the Receive () 
{
Byte [] buff=new byte 8 * [1024].
Byte [] dataSave=new byte (1 * 1024 * 1024),
int count=0;
String path=FRM. Path + "\ " + "789. TXT";
While (true)
{
If (flag)
{
Array. The Clear (buff, 0, buff. Length);
Int recv=SockClient. ReceiveFrom (buff, ref IP);
If (count + recv & lt; DataSave. Length)
Array. Copy (buff, 0, dataSave, count, recv);
The else
{
Array. Copy (buff, 0, dataSave, count, dataSave. Length - count);
break;
}
Count +=recv;
}
The else//block the current thread
{
Wh. WaitOne ();
}
}
Write2Disk (dataSave, path);
}
can you tell me the packet loss problem is my receiver program problem, how should modify to packet loss,

CodePudding user response:

Do not allow the packet loss situation, generally USES the TCP communications protocol, TCP will often appear glue bag phenomenon, usually do is check length, each packet to packet length, usually takes two byte expressed in short, the receiver receive long after first calculation package, in intercept packet length bytes of storage, if less than the package long received package, continued to receive, if is greater than the packet length, the interception of packet length data storage, the remaining bytes cache read to the next, in the loop

CodePudding user response:

1. The udp itself is no guarantee that the data is not lost,
If you want to reliable, in the TCP
2. It is recommended to use asynchronous receive, oneself use synchronization to
Asynchronous mode efficiency is not high,
3. The data receiving and parsing recommended separation,

CodePudding user response:

refer to the second floor xian_wwq response:
1. Udp itself is no guarantee that the data is not lost,
If you want to reliable, in the TCP
2. It is recommended to use asynchronous receive, oneself use synchronization to
Asynchronous mode efficiency is not high,
3. The data receiving and parsing recommended separation,

Hello, this is I use TCP to send and receive, and I process is to accept the 1 m data at the receiving end will stop receiving then to save, the received data with wireshark caught data is consistent, is in before the wireshark capture the red part is the sender TCP sending failed, I just want to ask next if to receive in asynchronous way whether can solve this problem

CodePudding user response:

For asynchronous receive after still have problem of packet loss, but the effect is better than syncing, here I attached my code, please see if can improve, it is receiving to stop receiving dataSave data length, and preservation,
 public void AsynReceive () 
{
NetworkStream stream=TCP. GetStream ();
Array. The Clear (buff, 0, buff. Length);
Stream. BeginRead (buff, 0, buff. Length, asyncResult=& gt;
{
Int len=stream. EndRead (asyncResult);
If (count + len & lt; DataSave. Length)
{
Array. Copy (buff, 0, dataSave, count, len);
Count +=len;
AsynReceive ();
}
The else
{
TCP. The Close ();
Write2Disk (dataSave, path);
return;
}
}, stream);
}

CodePudding user response:

No one knows how to improve the speed of the receiver to receive

CodePudding user response:

Use a while loop to receive very bad, with the method of the 4th floor, asynchronous + recursive call, in addition, the need to unpack and group package again,

Because TCP has stuck package problem,
  •  Tags:  
  • C#
  • Related