sending a large amount of data throught TCP socket

前端 未结 4 1327
广开言路
广开言路 2020-12-21 06:55

This is my first question posted on this forum, and I\'m a beginner in c# world , so this is kind of exciting for me, but I\'m facing some issues with sending a large amount

4条回答
  •  一向
    一向 (楼主)
    2020-12-21 07:20

    Most likely the problem is that you are closing the client-side socket before all the data has been transmitted to the server, and it is therefore getting discarded.

    By default when you close a socket, all untransmitted data (sitting in the operating system buffers) is discarded. There are a few solutions:

    [1] Set SO_LINGER (see http://developerweb.net/viewtopic.php?id=2982) [2] Get the server to send an acknowledgement to the client, and don't close the client-side socket until you receive it. [3] Wait until the output buffer is empty on the client side before closing the socket (test using getsocketopt SO_SND_BUF - I'm not sure of the syntax on c#).

    Also you really should be testing the return value of Send(). Although in theory it should block until it sends all the data, I would want to actually verify that and at least print an error message if there is a mismatch.

提交回复
热议问题