SFML community forums

Help => Network => Topic started by: Stickmoose on April 21, 2010, 03:54:55 pm

Title: [SOLVED] SocketTCP only accepting one connection
Post by: Stickmoose on April 21, 2010, 03:54:55 pm
This code works fine for 1 connection but when I try more it just queues them up and process them after I close the initial one.  What am I doing wrong?  I am using non-blocking sockets.
Code: [Select]
sf::Packet PacketRecv;
ss::Packet Packet;
sf::SocketTCP Client;
unsigned short Port = 27015;
sf::IPAddress Sender;
int AcceptStatus = Socket.Accept(Client);

if(AcceptStatus==sf::Socket::Done){
std::cout<<"socket accepted"<<std::endl;
sf::Packet PacketSend;
ss::Packet PacketSendParsed = {0,0,0,0,"loL"};
PacketSend<<PacketSendParsed;
Client.Send(PacketSend);
Clients.push_back(Client);
} else if(AcceptStatus==sf::Socket::Error){
std::cout<<"socket error"<<std::endl;
}
for(int i=0;i<Clients.size();i++){
int RecvStatus = Clients[i].Receive(PacketRecv);
ss::Packet PacketRecvParsed;
if(RecvStatus==sf::Socket::Done){
PacketRecv>>PacketRecvParsed;
std::cout<<PacketRecvParsed.Message;
}
}
Title: [SOLVED] SocketTCP only accepting one connection
Post by: Laurent on April 21, 2010, 07:11:23 pm
Can you show the full code, or a complete and minimal one that reproduces the problem?
Title: [SOLVED] SocketTCP only accepting one connection
Post by: Stickmoose on April 21, 2010, 08:17:41 pm
Code: [Select]
std::vector<sf::SocketTCP> Clients;
sf::Packet PacketRecv;
sf::SocketTCP Socket,Client;
Socket.SetBlocking(false);
Socket.Listen(27015);

while(Socket.IsValid()){
int AcceptStatus = Socket.Accept(Client);

if(AcceptStatus==sf::Socket::Done){
std::cout<<"socket accepted"<<std::endl;
Clients.push_back(Client);
}
for(int i=0;i<Clients.size();i++){
int RecvStatus = Clients[i].Receive(PacketRecv);
}
}

It works fine until I added/looped through the vector of clients.  I tried using simple arrays and it was no different :(  If I comment out the line which calls Receive it works fine  :?
Title: [SOLVED] SocketTCP only accepting one connection
Post by: Stickmoose on April 22, 2010, 12:36:01 am
Fixed my problem, I had forgot to set the client socket to non-blocking  :D