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

Author Topic: Blocking/unblocking sockets  (Read 2604 times)

0 Members and 1 Guest are viewing this topic.

Daffern

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Blocking/unblocking sockets
« on: July 16, 2013, 07:35:51 pm »
I have been trying to set up a tcp and udp network, and i want to use nonblocking sockets. However i noticed that when i'm using nonblocking sockets the return code for udpSocket.bind() and tcpSocket.connect() doesnt return sf::Socket::Done even if the operation was successful. My workaround was doing this:

tcpSocket.setBlocking(true);
if (tcpSocket.connect(ip,port, sf::seconds(3) ) != sf::TcpSocket::Done){
   std::cout<<"Failed to connect TCP"<<std::endl;
   tcpSocket.setBlocking(false);
}

Is this intended and is this a good or a bad way to do it?

EDIT: tcpSocket.receive() also returns sf::Socket::Done when it doesn't receive anything in nonblocking mode
« Last Edit: July 16, 2013, 07:45:54 pm by Daffern »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Blocking/unblocking sockets
« Reply #1 on: July 16, 2013, 08:10:58 pm »
Yes, this is a good strategy. Note that non-blocking connect will be fixed in SFML 2.1.

Quote
tcpSocket.receive() also returns sf::Socket::Done when it doesn't receive anything in nonblocking mode
This one is strange. Can you provide a complete and minimal code that reproduces the problem?
Laurent Gomila - SFML developer

Daffern

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Blocking/unblocking sockets
« Reply #2 on: July 17, 2013, 01:27:24 am »
Thanks for the reply!

I just tested it and found out it was my bad, the tcpSocket does return the right status code. I forgot to set every new tcp socket to nonblocking mode after it had connected so that the program froze when it received something.

Also i want to thank the people working with sfml, i find it extremely fun to code with sfml and c++  :D

 

anything