The accept function is not blocked? Program to accept should not stop waiting connection? Why this application directly to the past, I got an incorrect socket return values,
# include & lt; Stdio. H>
# include & lt; stdlib.h>
# include & lt; WinSock2. H>
1024 * 2 # define MAXSIZE
# pragma comment (lib, "Ws2_32. Lib")
Int main ()
{
WSADATA WSADATA;
If (WSAStartup (MAKEWORD (2, 2), & amp; WsaData)!=0)
{
Printf (" start the error ");
}
int len;
SOCKADDR_IN serv_addr.
SOCKADDR_IN l_addr;
Char the recvmsg [MAXSIZE];
The SOCKET sServer AcceptSocket;
SServer=socket (PF_INET SOCK_STREAM, 0).
If (sServer & lt; 0 {
Printf (" socket error ");
return -1;
}
Memset (& amp; Serv_addr, 0, sizeof (serv_addr));
Serv_addr. Sin_family=AF_INET;
Serv_addr. The sin_port=htons (2016);
Serv_addr. The sin_addr. S_addr=htonl (INADDR_ANY);
If (bind (sServer (SOCKADDR *) & amp; Serv_addr, sizeof (serv_addr)) & lt; 0)
{
Printf (" bind err ");
return -1;
}
If (listen (sServer, 5)==SOCKET_ERROR)
{
Printf (" Listen error! \n");
return -1;
}
Memset (& amp; L_addr, 0, sizeof (l_addr));
AcceptSocket=accept (sServer (SOCKADDR *) & amp; L_addr, & amp; Len);
While (1) {
If (AcceptSocket & gt;=0 & amp; & AcceptSocket!=SOCKET_ERROR)
{
Printf (" Now the link from the then connected. \ n ");
Memset (the recvmsg, 0, MAXSIZE);
SYSTEMTIME sys.
GetLocalTime (& amp; Sys);
Sprintf (the recvmsg, 02 02 "% 4 d % d % d - 02 02 02 % d % d % d", sys. WYear, sys. WMonth, sys. WDay, sys. WHour, sys. WMinute, sys. WSecond);
if (! Send (AcceptSocket, the recvmsg, sizeof (the recvmsg), 0))
{
Printf (" send err! \n");
break;
}
}
The else
{
Printf (" accept the error! \n");
continue;
}
Sleep (40);
}
Closesocket (sServer);
WSACleanup ();
return 0;
}
CodePudding user response:
Check the return value please use! Format=correct valueCodePudding user response: