Home > Back-end >  C socket recv function question
C socket recv function question

Time:11-15

const int bufLen=25600;
Char buffer [bufLen];
The string result="";
Int resultLen=0;
ResultLen=recv (sclient, buffer, bufLen, 0);



As above, through recv receive data in the socket, whether must receive buflen so multiple bytes of data to finish the data reception?

CodePudding user response:

Int recv (SOCKET s, char FAR * buf, int len, int flags);

Both the client and server applications are received from the other end of the TCP connection using the recv function data,

(1) the first parameter specifies the receiving end socket descriptor;

(2) the second parameter specifies a buffer, the buffer used to store the recv function to receive data;

(3) the third argument specifies the buf length;

(4) the fourth parameter generally 0,

Here only describe synchronization Socket recv function execution process, when the application calls recv function, wait for s recv sends the data in the buffer is transferred by agreement, if agreement in transmission s send network errors in the data in the buffer, then the recv () function returns SOCKET_ERROR, if there are no data or data in a s send buffer by agreement is sent successfully, recv check Socket s receive buffer, if no data in the receive buffer s or agreement is receiving data, then recv has been waiting for, only to receive the data, when the agreement after receiving the data, the recv function s receiving data in a buffer copy in buf (note agreement to receive the data may be greater than the length of the buf, so in this case several times to call recv function to put the s after receiving the copy of data in buffer, the recv function is just copy the data, the real receiving data is agreement to complete), recv () function returns the actual number of bytes to copy. If the recv error when the copy, it returns SOCKET_ERROR; If the recv function waiting for agreement when receiving data network is down, it returns 0,

CodePudding user response:

Webmaster answer is too long, bad review,
This sentence: resultLen=recv (sclient, buffer, bufLen, 0); If must receive bufLen long data to return, what is the meaning of the resultLen existence? And from your name of it, you will have the answer,
  • Related