Handling dropped TCP packets in C#

前端 未结 4 1021
执笔经年
执笔经年 2020-12-15 12:44

I\'m sending a large amount of data in one go between a client and server written C#. It works fine when I run the client and server on my local machine but when I put the

4条回答
  •  天涯浪人
    2020-12-15 13:30

    Well there's one thing wrong with your code to start with, if you're counting the number of calls to Receive which complete: you appear to be assuming that you'll see as many Receive calls finish as you made Send calls.

    TCP is a stream-based protocol - you shouldn't be worrying about individual packets or reads; you should be concerned with reading the data, expecting that sometimes you won't get a whole message in one packet and sometimes you may get more than one message in a single read. (One read may not correspond to one packet, too.)

    You should either prefix each method with its length before sending, or have a delimited between messages.

提交回复
热议问题