Haven't developed the socket before application, also didn't how to develop a console application, now into two unfamiliar fields at the same time,
I this program, the server and the client is I wrote, the client to the server application data, the service side often have to wait for a period of time after receipt of a request to reply, so I use the block type, the client and the server are set up to send and receive timeout,
70% chance of program is run by my expectations, the client request - & gt; After you receive the service side, sometimes data feedback, sometimes no data feedback - & gt; The client receives the data into the next request cycle; If not receive, return, overtime request to enter the next cycle,
But sometimes don't know where is the card, but this time, if you use the mouse to select the console on some output text information above, then click the right, the program can continue to run again, don't understand, I missed what is this piece of knowledge?
For the convenience of the program has cut below
The client is so write:
SOCKET SCK=SOCKET (AF_INET SOCK_STREAM, IPPROTO_TCP);
If (SCK==INVALID_SOCKET)
{
Return SCK;
}
DWORD a TimeOut=1000;//set to send timeout
1 SECIf (setsockopt (SCK, SOL_SOCKET, SO_SNDTIMEO, (char *) & amp; The TimeOut, sizeof (TimeOut))==SOCKET_ERROR)
{
Cout & lt; <"Set up to send timeout failure \ n: \ \ & gt;";
}
TimeOut=6000;//set to receive a timeout
6 secondsIf (setsockopt (SCK, SOL_SOCKET, SO_RCVTIMEO, (char *) & amp; The TimeOut, sizeof (TimeOut))==SOCKET_ERROR)
{
Cout & lt; <"Set up to receive timeout failure \ n: \ \ & gt;";
}
The server is written so:
SOCKADDR_IN addrSrv;
The SOCKET sockSrv=SOCKET (AF_INET, SOCK_STREAM, 0);
AddrSrv. Sin_addr. S_un. S_addr=htonl (INADDR_ANY);
AddrSrv. Sin_family=AF_INET;
AddrSrv. Sin_port=htons (5100);
Bind (sockSrv, (SOCKADDR *) & amp; AddrSrv, sizeof (SOCKADDR));
Listen (sockSrv, 1);
Char msgBuf [256]={0};
SocketDataBin sockData;
While (waitting)
{
The SOCKET sockConn=accept (sockSrv, (SOCKADDR *) & amp; AddrClient, & amp; Len);
DWORD a TimeOut=1000;//set to send timeout
1 SECThe setsockopt (sockSrv, SOL_SOCKET, SO_SNDTIMEO, (char *) & amp; The TimeOut, sizeof (TimeOut));
TimeOut=1000;//set to receive a timeout
1 SECThe setsockopt (sockSrv, SOL_SOCKET, SO_RCVTIMEO, (char *) & amp; The TimeOut, sizeof (TimeOut));
Recvsize=recv (sockConn, (char *) & amp; SockData, headsize, 0);
CreateThread (NULL, 0, tradeThread sockConn, 0, NULL);
//the server after receiving the connection to create a new thread for processing, mainly recvsize=recv (sockConn, (char *) & amp; RecvData, headsize, 0);
}
CodePudding user response:
In the beginning of the program and the two words:HANDLE HANDLE=GetConsoleWindow ();
SetConsoleMode (handle, ENABLE_EXTENDED_FLAGS);
CodePudding user response:
Thank you for your advice, but I this program also allows interactive command, can't set the mode, I wonder, why can right click to activate program? Seems to be a socket timeout failed, but into the editor and reactivate the timeout?CodePudding user response: