Home > other >  After vs2015 MFC socket send many times continuously, recv data problems
After vs2015 MFC socket send many times continuously, recv data problems

Time:09-27

Everyone a great god, please give advice or comments!
VS2015, MFC, c + +, with AfxSocketInit () function to load the socket, use socket () function creates a socket, TCP form
After confirm the server and the client connection is successful, the server sends data to the client, the following experiments:

Experiment 1:
Step 1: client perform recv (SockClient recvtext1, 100, 0) function, after execution, blocks,
Step 2: the server perform int abc1=send (sockConn sendtext1, strlen (sendtext1) + 1, 0) function, after execution, the recv function is unblocked in step 1, recvtext1 content from the server to send data consistent,
Step 3: client perform recv (SockClient recvtext2, 100, 0) function, after execution, blocks,
Step 4: the server perform int revised=send (sockConn sendtext2, strlen (sendtext2) + 1, 0) function, after execution, the recv function is unblocked in step 3, recvtext2 content from the server to send data consistent,
Results: the data is normal,

Experiment 2:
Step 1: the server to perform int abc1=send (sockConn sendtext1, strlen (sendtext1) + 1, 0) function, after execution, abc1 is 5, and send the data quantity is consistent, said to send success,
Step 2: the client perform recv (SockClient recvtext1, 100, 0) function, after execution, recvtext1 content from the server to send data consistent,
Step 3: the server to perform int revised=send (sockConn sendtext2, strlen (sendtext2) + 1, 0) function, after execution, revised to 5, and send the data quantity is consistent, said to send success,
Step 4: the client perform recv (SockClient recvtext2, 100, 0) function, after execution, recvtext2 content from the server to send data consistent,
Results: the data is normal,

Experiment 3:
Step 1: the server to perform int abc1=send (sockConn sendtext1, strlen (sendtext1) + 1, 0) function, after execution, abc1 is 5, and send the data quantity is consistent, said to send success,
Step 2: the server perform int revised=send (sockConn sendtext2, strlen (sendtext2) + 1, 0) function, after execution, revised to 5, and send the data quantity is consistent, said to send success,
Step 3: client perform recv (SockClient recvtext1, 100, 0) function, after execution, recvtext1 content for the first time the server sends data, namely sendtext1,
Question: why not sendtext1 + sendtext2 received content? Why sendtext2 was rejected? In which link out the problem, if want to receive sendtext1 in step 3 + sendtext2 recv (namely the send many times, 1 time) when how should change?

CodePudding user response:

If you send the number of bytes (the third parameter) consistent with sending content, won't appear the problem in experiment 3, two recv again after send, receive data is the sum of the first two; If you send the number of bytes sent (the third parameter) is greater than the content, can appear the phenomenon of experiment 3! Great god, please give directions! Grateful!

CodePudding user response:

Just try once, if the sender wrote:
Char sendtext1 []="se \ 0 nd1";
Int abc1=send (sockConn sendtext1, strlen (sendtext1) + 1, 0).
Then perform here, after the send abc1 is 3, that is, to zero is considered over! Behind the nd1 rejected, can you tell me the great god how can I send the string is complete?
  • Related