   SFML multiple client
  Hello I am trying to do networking with SFML. The game is tic tac toe.I wrote a server program and a client program. I was testing by running two instances of the client from my computer so the following function gets called twice, the second time Connect fails though. Is there a problem with connecting to the same port and address twice?  int Port = 2435;bool Client::initNetwork(){// Ask for server addresssf::IPAddress ServerAddress("192.168.0.1");// Connect to the specified serverif(socketClient.Connect(Port, ServerAddress) != sf::Socket::Done){return false;}return true;}Thanks.   
  There isn't a problem connecting to the same address and port more than once, but a single socket can only handle one connection at a time. Therefore the server needs to hand of new clients to separate sockets as they connect which should then be handled separately.My guess is that your server code at the moment is only capable of handling one client at the time, which causes your second client to be refused. You'd have to post the server code too if you need further help than that though.   ;  I am still trying to understand how the whole thing works ^_^'. Either way, here's my server code.The code has comments explaining what each part does. When the server is run initServer is called once, then on each update updateServer is called. The code tries to wait start reading and sending data to clients until atleast 2 clients are trying to connect. //Called oncebool Server::initServer(){// Listen to a port for incoming connectionsif(!serverSocket.Listen(Port))return false;Selector.Add(serverSocket);return true;}//Called on each updatebool Server::updateServer(){unsigned int NbSockets = Selector.Wait();//Wait until there are atleast 2 clients in selectorif(NbSockets > 2){for (unsigned int i = 0; i < NbSockets; ++i){sf::SocketTCP Socket = Selector.GetSocketReady(i);if (Socket == serverSocket){sf::SocketTCP client;sf::IPAddress clientAddress;// If the listening socket is ready, it means that we can accept a new connectionserverSocket.Accept(client, &clientAddress);std::cout << "Client connected ! (" << clientAddress << ")" << std::endl;// Add it to the selectorSelector.Add(client);}else{// Else, it is a client socket so we can read the data he sentsf::Packet Packet;if (Socket.Receive(Packet) == sf::Socket::Done){// Extract the message and display itint x,y;Packet >> x >> y;std::cout << "Recieved : " << x << ',' << y;for(int u = 0 ; u < NbSockets; u++){sf::SocketTCP Socket2 = Selector.GetSocketReady(u);if(Socket2 != serverSocket && Socket2 != Socket){Socket2.Send(Packet);}}}else{// Error : we'd better remove the socket from the selectorSelector.Remove(Socket);}}}}return true;//board.update(this);};Quote:Original post by LuctusThere isn't a problem connecting to the same address and port more than once, but a single socket can only handle one connection at a time. Therefore the server needs to hand of new clients to separate sockets as they connect which should then be handled separately.Not quite. A single listening socket is perfectly capable of accepting as many connections as needed. The likely cause of the problem is that after accepting a new connection using the listening socket, Antonym's code initiates a blocking receive operation that will halt the execution of the program until the newly accepted client sends something. In this state, the server is unable to accept another connection. In fact, the listening socket's accept operation is also blocking. To resolve this problem, one usually turns to a polling function (select() is common in BSD Sockets/Winsock, but I'm sure SFML provides an equivalent function) to "ask" the listening sockets and its clients if any of them need attention.EDIT: Your second post beat me to it. I see you're already using a selector. No time to look at it now though, sorry.   ;Quote:Original post by AntonymThe code tries to wait start reading and sending data to clients until atleast 2 clients are trying to connect.unsigned int NbSockets = Selector.Wait();//Wait until there are atleast 2 clients in selectorif(NbSockets > 2){// ...serverSocket.Accept(client, &clientAddress);std::cout << "Client connected ! (" << clientAddress << ")" << std::endl;// Add it to the selectorSelector.Add(client);// ...}Your problem lies here. Selector contains a set of sockets that it is currently monitoring and Selector.Wait() returns how many of those that are ready for reading.The problem is that Selector can never contain more than exactly one socket at that point (the server socket) since you don't add any more sockets until after the condition (NbSockets > 2). Thus, the condition will never be true and the call to serverSocket.Accept will never happen.When a client connects to the serverSocket and serverSocket.Accept isn't called, any further connection attempts to the listening socket will be denied until the client currently waiting to be accepted have been handled.// Edit: Also, you're actually checking for more than 2 ready sockets, not at least 2 as your comment says.   ;Quote:Original post by WindryderQuote:Original post by LuctusThere isn't a problem connecting to the same address and port more than once, but a single socket can only handle one connection at a time. Therefore the server needs to hand of new clients to separate sockets as they connect which should then be handled separately.Not quite. A single listening socket is perfectly capable of accepting as many connections as needed.  I didn't mean to say that a listening socket can only accept a single connection. What I meant was that each call to accept will spawn a new socket, each of which will handle a single connection to a client. Each socket would then need to be handled separately in order to not block each other. Looks like I did quite a terrible job of explaining that though. [smile]   