Sign in to follow this  
Thrust

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this