Frequently asked questions about Socket.receive () and send ()

Source: Internet
Author: User
Socket.receive Method (Byte (), Int32, Int32, SocketFlags)

Receives the specified number of bytes from the bound Socket, using the specified SocketFlags, in the specified offset position of the receive buffer.

public int Receive (
	byte[] buffer,
	int offset,
	int size,
	socketflags socketflags
)

Parameters
Buffer
Type: System.Byte ()
An array of type Byte, which is where the data is stored.
Offset
Type: System.Int32
The location in the buffer where the received data is stored.
Size
Type: System.Int32
The number of bytes to receive.
SocketFlags
Type: System.Net.Sockets.SocketFlags
A bitwise combination of socketflags values.
return value
Type: System.Int32
The number of bytes received.

(part of the above is from MSDN)
The following is my summary of experience:

1,socket The actual number of bytes received and the expected number of bytes to receive

We can clearly know from the return value above that, although we have defined a length for buffer and a size to receive, the number of bytes received by the socket (that is, the return value) does not necessarily equal the size

So when we do this, we need to determine whether the actual number of bytes received is equal to the number of bytes to receive. This is very important.

Define cache to receive data
 byte[] BODY = new byte[1024];
 The actual data received for the first time flag
 int flag = socket. Receive (body, 0, body. Length, socketflags.none);
 If no fixed-length data is received, the loop receives the while
 (flag!= body. Length)
 {
    flag + = socket. Receive (body, flag, body. Length-flag, socketflags.none);
 

Therefore, the socket receive data should be the process of this.

2, the server sends many times, the client receives the question at once.

Of course, in the actual programming is rarely the case, but I write this is to illustrate a problem: if the client wants to send data over the server more than once, the length of the client-defined cache body is equal to the length of the data sent by the server multiple times, and the client begins to receive it once after the The client is sure to receive all the data sent by the server, and will not receive only some of the data that the server sends over.

My story sucks. For example, if the server is divided three times to send "Hell", "0", "World". This way the client will be able to accept "HelloWorld". Not "Hell" or anything else.

3,socket.send ()

Send synchronizes the data to the remote host specified in the Connect or Accept method and returns the number of bytes that were successfully sent. Send applies to both connection-oriented and connectionless protocols.

This overload requires a buffer to contain the data to be sent. The default value for SocketFlags is 0, the buffer offset defaults to 0, and the default value for sending bytes is the size of the buffer.

If you are using a connectionless protocol, you must first call connect to call this method, or send will cause socketexception.

If you are using a connection-oriented protocol, you must use connect to establish a remote host connection or use Accept to accept incoming connections.

If you are using a connectionless protocol and you intend to send data to several different hosts, you should use the SendTo method. If you do not use the SendTo method, you must call Connect before each call to Send. You can use SendTo even if you have established a default remote host with Connect. By calling Connect separately, you can also change the default remote host before calling Send.

If you are using a connection-oriented protocol, send will remain blocked until all bytes in the buffer are sent, unless the timeout value is set using Socket.sendtimeout. If the timeout value is exceeded, the Send call throws a socketexception. In non-blocking mode, send may complete successfully, even if it sends less than the number of bytes in the buffer. It is up to your application to keep track of the number of bytes sent and retry the operation until the application sends the number of bytes in the buffer. There is no guarantee that the data sent will immediately appear on the network. To improve network efficiency, the underlying system may delay transmission until enough outgoing data has been collected before the delivery begins. The successful completion of the Send method means that the underlying system has the space to buffer the data for the network to send.

4, summary

The Send () method simply presses the cached data into the underlying system's controls. The receive () method simply reads data from the underlying system's space after the data reaches the machine over the network.

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.