Jump to content
  • Advertisement
Sign in to follow this  
Thrust

How to change a nonblocking server/client into a blocking one?

This topic is 4872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Can someone tell me what in this code makes it a nonblocking server, and what I can change to make it into a blocking one?
//--------------------------------------------//
//Onliene Functions//
//--------------------------------------------//

//Hosts a game
void host()
{
  int error;
  //loads winsock
  WSADATA wsaData;
  error = WSAStartup(MAKEWORD(2, 0), &wsaData);
  
  //error handler
  if (error != 0)
  {
    cout << "Couldn't Initialize Winsock: 1" << endl;
  // unload WinSock
  WSACleanup ();
  return;
  }
  
  cout << "Winsock initialized..." << endl;
  
  //creates a socket
  connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  
  //error handler
  if (connectSocket == INVALID_SOCKET)
  {
    cout << "Couldnt create socket: 2" << endl;
  // unload WinSock
  WSACleanup ();
  return; 
  }
  
  cout << "Socket created..." << endl;
  
  //This is the port it connects to, we are using 12521
  int port = 12521; 
  // the address structure for a TCP socket
  sockaddr_in addr;

  addr.sin_family = AF_INET;      // Address family Internet
  addr.sin_port = htons (port);   // Assign port to this socket
  addr.sin_addr.s_addr = htonl (INADDR_ANY);   // No destination
  
  if (bind(connectSocket, (LPSOCKADDR)&addr, sizeof(struct sockaddr)) == SOCKET_ERROR)
  { 
  cout << "Couldn't bind socket: 3" << endl;
  // unload WinSock
  WSACleanup ();
  return;
  }
  
  cout << "Socket binded..." << endl;
  
  //Waits for the connection 
  cout << "Waiting for connection from other player..." << endl;
  SFont_Write(Connectmenu, Font, 5,145,"Waiting for connection from other player...");
  SDL_Flip(Connectmenu);
  listen(connectSocket, 1);
  
  //Socket for connection
  SOCKET connectPlayer;
  
  connectPlayer = accept(connectSocket, NULL, NULL);
  cout << "Client accepted... SUCCESS!" << endl;
  
  p1.connected = true;
  SFont_Write(Connectmenu, Font, 5,160,"CONNECTED!!!");
  SDL_Flip(Connectmenu);
}


//Joins a game
void join()
{
  int error;
  bool isRunning = true;
  Uint8* keys;
  getInput();
  ipAddress = text;
  text = "";
  cout<<"Woot!"<<endl;
  
  //loads winsock
  WSADATA wsaData;
  error = WSAStartup(MAKEWORD(2, 0), &wsaData);
  
  //error handler
  if (error != 0)
  {
    cout << "Couldn't Initialize Winsock: 1" << endl;
    // unload WinSock
    WSACleanup ();
    return;
  }
  
  cout << "Winsock initialized..." << endl;
  
  //Stores information about the hosting player
  LPHOSTENT hostEntry;
  in_addr iaHost;
  //Hosts IP address
  iaHost.s_addr = inet_addr(ipAddress.c_str());
  //Gathers information using the IP address
  hostEntry = gethostbyaddr((char *)&iaHost, sizeof(in_addr), AF_INET);
  //error handling
  if (!hostEntry)
  { 
    cout << "Error gathering data..." << endl;
    WSACleanup ();
    return;
  }
    
  
  
  //Creates socket
  connectSocket = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
  
  //error handler
  if (connectSocket == INVALID_SOCKET)
  {
    cout << "Couldnt create socket: 2" << endl;
  // unload WinSock
  WSACleanup ();
  return;
  }
  
  cout << "Socket created..." << endl;
  
  //This is the port it connects to, we are using 12521
  const int port = 12521; 
  // the address structure for a TCP socket
  sockaddr_in serverInfo;

  // Address family Internet
  serverInfo.sin_family = AF_INET;
  
  serverInfo.sin_port = htons (port);   // Assign port to this socket
  
  serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);   // Hosts IP address
  
  
  //Connects to the host
  error = connect(connectSocket,(LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));
  
  //error handler
  if (error != 0)
  { 
  cout << "Couldn't connect to host: 3" << endl;
  // unload WinSock
  WSACleanup ();
  return;
  }
  
  cout << "Connected" << endl;
  p1.connected = true;
  SFont_Write(Connectmenu, Font, 5,160,"CONNECTED!");
  SDL_Flip(Connectmenu);
  
}

Share this post


Link to post
Share on other sites
Advertisement
From what I can see, I don't see anywhere in your code where your socket was set to blocking mode., Newly created sockets are blocking by default. you can change the I/0 mode with ioctlsocket.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!