Home > Software engineering >  I the BIND function why do wrong
I the BIND function why do wrong

Time:11-18

I BIND the SOCKET server side code in the CPP file to run without any problems, but not a in MFC as button function, which is a great god can tell me what the problem ah, thank you very much, the code is as follows:
Void CsocketserverDlg: : OnBnClickedButton1 ()
{
WSADATA WSD.
The SOCKET server.
The SOCKET sClient;
Int retVal;
Char buf [64].
//initialize the Socket dynamic database
If (WSAStartup (MAKEWORD (2, 2), & amp; WSD)!=0)
{
AfxMessageBox (_T (" WSAStartup failed! \ n "));
return ;
}
//create listens for Socket
Server=socket (AF_INET SOCK_STREAM, IPPROTO_TCP);
If (INVALID_SOCKET==server)
{
AfxMessageBox (_T (" socket failed! \ n "));
WSACleanup ();
return ;
}
//setting up a server Socket address
SOCKADDR_IN addrServ;
AddrServ. Sin_family=AF_INET;
AddrServ. Sin_port=htons (9990);
AddrServ. Sin_addr. S_un. S_addr=htonl (INADDR_ANY);
//bind socket
RetVal=bind (server, (const sockaddr *) & amp; AddrServ, sizeof (SOCKADDR_IN));
If (SOCKET_ERROR==retVal)
{
AfxMessageBox (_T (" bind failed! \ n "));
Closesocket (server);
WSACleanup ();
return ;
}
//start listening
RetVal=listen (server, 1);
If (SOCKET_ERROR==retVal)
{
AfxMessageBox (_T (" litsen failed! \ n "));
Closesocket (server);
WSACleanup ();
return ;
}

//TODO: add the control notification handler code
}

CodePudding user response:

You want to go into the thread, listening is a blocking process, the main program in your button resistance is dead

CodePudding user response:

reference 1/f, solitary guest tianya response:
you want to go into the thread, listening is a blocking process, the main program in your button resistance dead
why is binding address failure... Bind failure, rather than the program card

CodePudding user response:

After a failed WSAGetLastError () to see the error code, and then concrete analysis

CodePudding user response:

The return value is WSAGetLastError ()?

CodePudding user response:

The
reference 4 floor 7-eleven's response:
WSAGetLastError (), the return value is what?
10048

CodePudding user response:

https://blog.csdn.net/qllwenhua/article/details/2419208

CodePudding user response:

Port is occupied? Under the Telnet port
Is likely to be not normal exit before debugging process causes

CodePudding user response:

1 account is not correct, bind not blocked, you can also be placed on the main thread, the as long as the receiving/received on a child thread, bind failure, please check the return value, usually the current port has been binding/uninitialized reasons, such as port can be set up reuse,
  • Related