c++ - Select function in non blocking sockets -


I am creating an online game client and when I try to connect to an offline server, my The client freezes, so I wanted to use non-blocking socket which is suitable for games because other tasks are required to connect to the server.

When using non-blocking sockets, the connect function always returns the same value, regardless of the result, so people can find the result of the connection request here Choose the function is recommended.

(Setting block that prevents connection before)

  u_long iMode = 1; Ioctlsocket (hSocket, FIONBIO, & amp; iMode);   

(setting up a socket set)

  FD_ZERO (and write); FD_ZERO (& amp; amp; nbsp;); FD_SET (HSOC, End Script); FD_SET (HSOC, and error);   
  Timewell timeout; Int Timeout = 10; Timeout // Timeout after 10 time. Tv_SC = Timeout SEC; Timeout.tv_usec = 0; Int iResult = Select (0, // unseen tap, // read & (client.Write), // write check & amp; (client.Err), // error checking and timeout); If (iResult) {} other {message_login ("error", "Can not connect to server"); }   

Select function always returns -1, why?

when select () return -1 ( SOCKET_ERROR ), then use the WSAGetLastError () to find out why this failed.

If the socket is set to error , when selects () , then the of Hokkopet (SOL_SOCKET, SO_ERROR) Use to get the socket error code to tell you why connect () fails.

If (iResult) evaluates as correct for any non-zero value, including 1, you should be if (iResult> gt; 0) iResult will need to report the number of sockets indicated in any ,

  U_long iMode = 1; Fd_set , 0 on timeout, and -1 on failure  

If (ioctlsocket (HSOC, FanBO, and IMode) == SOCKET_ERROR) {int errCode = WSAGetLastError (); // Use errCode as necessary ... message_login ("Error", "Socket can not be set to non-blocking, error: ..."); // However, if you provide a variable value in your message ...} if (connect (client.hSocket, ...) == SOCKET_ERROR) {int errCode = WSAGetLastError (); If (errCode! = WSAEWOULDBLOCK) {// Use errCode as necessary ... message_login ("error", "Can not connect to the server, error: ..."); // Although you supply a variable value ...} else {// In this case you can now select () to wait for connecting ...}}

  Timewell timeout; Int Timeout = 10; Timeout // Timeout after 10 time. Tv_SC = Timeout SEC; Timeout.tv_usec = 0; Int iResult = Select (0, // unseen tap, // read & (client.Write), // write check & amp; (client.Err), // error checking and timeout); If (iResult> gt; 0) {if (FD_ISSET (client.hSocket, & amp; (client.Err))} {DWORD errCode = 0; Int len ​​= sizeof (errCode); If (Holocaq (Client HSocket, SOILOKKET, SO_ERROR, (four *) and Erecod; & amp; len) == 0) {// eReadcode to be used ... message_login ("error", "do not connect Server, error: ... "); // though you supply a variable value in your message ...} else message_login ("error", "can not connect to server, unknown reason"); } And message_login ("success", "connect to server"); } And if (iResult == 0) {message_login ("Error", "Server Connect Timeout"); } And {int errCode = WSAGetLastError (); // Use errCode as necessary ... message_login ("error", "can not connect to the server, error: ..."); // though you provide a variable value in your message ...}    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -