How to receive data through socket in C #

Source: Internet
Author: User
When using socket for TCP/IP programming, data transmission and receiving are inevitable, the most commonly used is the socket synchronous function receive (or its overload): Public int receive (byte [] buffer, int offset, int size, socketflags) or the asynchronous function beginreceive (or its overload): Public iasyncresult beginreceive (byte [] buffer, int offset, int size, socketflags, asynccallback callback, object state) Here, what I want to talk about is the asynchronous function beginreceive (). The asynchronous function beginreceive () is used to define a function of the asynccallback type (for example, the receiveend function is called below). In this function, call the endreceive () function of the socket when we do the least. Protected void receiveend (iasyncresult AR) {Socket Client = (socket) ar. asyncstate; int recvlen = client. endreceive (AR); If (recvlen> 0) {/** // * Received data */client. beginreceive (buffer, 0, buffer. length, socketflags. none, new asynccallback (receiveend), client);/** // * continue to receive data */} else {client. close ();}}
In the MS-help: // Ms. msdnqtr. v80.chs/MS. msdn. v80/MS. netdevfx. v20.chs/cpref10/html/m_system_net_sockets_socket_beginreceive_3_2b547d4d.htm basically means the above, but now the problem is: if the buffer length I have defined is 10, but the data I have sent is 12 or 8, then, the function will not go to the else part, but will block the beginreceive () call in the function. At first, I thought I would add a special character (string) at the end of the sent data, and then I would like to judge whether this character (string) was received at the time of receiving) (I checked some online materials, but some of them did this !), However, there is always a problem with this practice: the body sent by the sending end cannot contain this character (string). Otherwise, the body contains this character (string) the subsequent text will be lost. Therefore, I think we can only implement it through the internal mechanism of the socket, that is, try to make Program Go to the else section. Tested: after the client sends the data, use socket. Shutdown (socketshutdown. Send) or socket. Close () to make the program go to The else part. However, this also brings about a problem: If the buffer length I have defined is 10 and the length of the data I have sent is exactly 10, then, when the client does not need to shut down or close, the function has already reached the else part, and the socket on the server side is closed. When the client uses Shutdown () or close, the connection socket of the server is no longer available, and an exception is thrown when the endreceive operation is called. Therefore, I think it is necessary to determine whether the socket is available before calling endreceive. (This conclusion has not been tested.) At the same time, I tested the socket. shutdown (socketshutdown. send), changed to socket. shutdown (socketshutdown. receive), does not cause the function to go to The else part, so I think the socket Shutdown (socketshutdown. send) and close () will cause a data transmission between the client and the server.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.