Home > other >  Send function block non-blocking and the problem of the cache area, solving
Send function block non-blocking and the problem of the cache area, solving

Time:10-08

Client in blocking and non-blocking mode cycle respectively sent ten times the size of 1.8 million and 18 million the size of the char array, the server will be after the accept sleep30s receiving data in call recv function,
Problems come, I output every time send function return values, is copied to the data size of the cache area, and every time I send 1.8 million or 18 million return is sent, the size of the cache area have so big?
Second, is also in non-blocking mode, circulation data sent ten times 180 and 18 million, accept sleep30 seconds after recv, send function will copy the data into the buffer is returned, so why send function are only two cases cycle twice, the third time will send failure, that is why, a 3600 one thousand 3.6 million difference is very big,
Results on the chart and code below figure:
Blocking mode every time there will be a delay after sending the final can finish all hair:

CodePudding user response:

Non-blocking mode:

CodePudding user response:

When the client sends, the receiver is after the accept sleep30s receive call recv function circulation again,

CodePudding user response:

The client code:
Send data definition has become a global variable, the following is the main function inside the
WORD wVersionRequested;
WSADATA WSADATA;
Int err;

WVersionRequested=MAKEWORD (1, 1);

Err=WSAStartup (wVersionRequested, & amp; WsaData);
If (err!=0)
{
return 0;
}

If (LOBYTE (wsaData wVersion)!=1 | |
HIBYTE (wsaData wVersion)!
=1){
WSACleanup ();
return 0;
}
The SOCKET sockClient=SOCKET (AF_INET, SOCK_STREAM, 0);
If (sockClient==INVALID_SOCKET)
{
Cout & lt; <"The client failed to create a socket" & lt; /* wprintf (L "socketfailedwitherror: % ld \ n", WSAGetLastError ());
WSACleanup ();
return 0; */
}
The else
Cout & lt; <"The client to create a socket success \ n";


U_long mode=1;
The ioctlsocket (sockClient FIONBIO, & amp; Mode);
//control for blocking mode (0),


Unsigned int uiRcvBuf;
Int uiRcvBufLen=sizeof (uiRcvBuf);
Getsockopt (sockClient, SOL_SOCKET, SO_SNDBUF, (char *) & amp; UiRcvBuf, & amp; UiRcvBufLen);
/* if (SOCKET_ERROR==nErrCode)
{
return;

} */
Cout & lt; <"Send the cache" & lt; /* getsockopt (sockClient, SOL_SOCKET, SO_RCVBUF, (char *) & amp; UiRcvBuf, & amp; UiRcvBufLen);
Cout & lt; <"Accept the cache" & lt;

SOCKADDR_IN addrSrv;
Inet_pton (AF_INET, "121.248.51.26," (void *) & amp; AddrSrv. Sin_addr. S_un. S_addr);
//addrSrv sin_addr. S_un. S_addr=inet_pton (" 127.0.0.1 ");
AddrSrv. Sin_family=AF_INET;
AddrSrv. Sin_port=htons (6000);
Int nErrCode=connect (sockClient, (SOCKADDR *) & amp; AddrSrv, sizeof (SOCKADDR));
If (SOCKET_ERROR==nErrCode)
{
Cout & lt; <"Client lianjie failure" & lt; }
The else
Cout & lt; <"Clientlianjie success \ n";
Memset (recvBuf, '0', 9000000);
Memset (recvBuf + 9000000, '1', 9000000);
RecvBuf [18000000]='\ 0';

Int jj=10;
While (jj)
{

NErrCode=send (sockClient, recvBuf, 18000000 + 1, 0).
If (SOCKET_ERROR==nErrCode)
{
Cout & lt; <"The client send failure:" & lt; }
The else
Cout & lt; }
Closesocket (sockClient);
WSACleanup ();