Socket Notes [go]

Source: Internet
Author: User

1,To increase efficiency, you can consider using functions without exceptions.In. socket. send, socket. receive has no exception function socket. send (byte [], int32, int32, socketflags, socketerror) socket. receive (byte [], int32, int32, socketflags, socketerror) reduces unnecessary exceptions and increases efficiency.

2,Socket. ConnectedNot currentSocketStatusGet a value indicating whether the socket is connected to the remote host during the last send or receive operation. What should we do?

Msdn also tells us that the value of the connected attribute reflects the connection status during the last operation. If you need to determine the current status of the connection, please do not block, zero-byte send call. If the call is successful or the waewouldblock error code (10035) is triggered, the socket is still in the connection status; otherwise, the socket is no longer in the connection status.

3To useSocket. PollDetermine whether or not it can be received. Do not useSocket. AvailableAlthough the socket. available can peek at the number of bytes in the current Recv buffer, and available is twice the speed of poll, but msdn says: If the remote host uses the shutdown method to close the socket connection, if all available data is received, the receive method is completed immediately and zero bytes are returned. Therefore, when the network is disconnected, the socket is used. available determines whether the Recv data will exist and does not know that the client has been disconnected. Supplement: socket is not recommended. poll traverses the socket list and uses Socket. select (or other models), socket. poll is for socket. select encapsulation, execute socket. poll time consumption is non-blocking socket. three times the Recv.

4Non-blocking mode cannot be usedReceiveIndicates whether to disconnectNote: If the remote host closes the socket connection using the shutdown method and all available data has been received, the receive method will be completed immediately and return zero bytes. However, the blocking mode does not indicate that the socket has been disconnected. This function is different from the Recv function of C, so pay special attention to it. You need to determine the out socketerror. When it is not socketerror. Success, socketerror. interrupted and socketerror. wouldblock, the network is disconnected.

5,SendThe buffer may be full.Determine the out socketerror. If it is equal to socketerror. wouldblock indicates that the send buffer is full and the socket should be disconnected; otherwise, the overall speed should be affected, rather than again. Conversely, only socketerror is allowed. interrupted. You can repeat it once.

6Active disconnectionSocketMsdn says: If you are currently using a connection-oriented socket, you must call the shutdown method before closing the socket. This ensures that all data on the socket has been sent and received before the connected socket is closed. Therefore, the close function of the network library can encapsulate Shutdown (socketshutdown. Both) before calling close ().

7,SocketDisabled(Close)But cannot be disconnected at the other endScoket at one end has been disabled, but data can still be sent at the other end in a short time! This problem has not yet been resolved, but the cause is known, in the book Windows Network Programming Technology (P139-P140) said: passive shutdown, the application receives a fin package from the other party and responds with an ACK package. In this case, the socket of the application changes to the close_wait state. Because the other party has closed its socket, it cannot send data any more. But the application is different. It can send data until the socket of the other party is closed.

Related Article

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.