Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] SocketTCP only accepting one connection  (Read 2817 times)

0 Members and 1 Guest are viewing this topic.

Stickmoose

  • Newbie
  • *
  • Posts: 3
    • View Profile
[SOLVED] SocketTCP only accepting one connection
« 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;
}
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] SocketTCP only accepting one connection
« Reply #1 on: April 21, 2010, 07:11:23 pm »
Can you show the full code, or a complete and minimal one that reproduces the problem?
Laurent Gomila - SFML developer

Stickmoose

  • Newbie
  • *
  • Posts: 3
    • View Profile
[SOLVED] SocketTCP only accepting one connection
« Reply #2 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  :?

Stickmoose

  • Newbie
  • *
  • Posts: 3
    • View Profile
[SOLVED] SocketTCP only accepting one connection
« Reply #3 on: April 22, 2010, 12:36:01 am »
Fixed my problem, I had forgot to set the client socket to non-blocking  :D

 

anything