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

Author Topic: Socket try connect multiple times  (Read 2673 times)

0 Members and 1 Guest are viewing this topic.

Naralas

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Socket try connect multiple times
« on: April 27, 2015, 11:22:22 am »
Hello,

I have a project where I need to check in the network if there's already a server. I'm trying to connect a socket and the first time I call the connect method the socket is able to connect but otherwise it can't. Like the socket can only connect if the adress of the server is the first one to be tested.

for(int i = first ; i <= last; i++)
{ // net_id -> something like "192.168.140", first and last define the set of adresses
    test = net_id;
    test.append(".");
    test.append(std::to_string(i));  // 192.168.140.1, 192.168.140.2, ...
    sf::Socket::Status status = s_test.connect(test,port);
}

Naralas

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Socket try connect multiple times
« Reply #1 on: April 27, 2015, 03:48:39 pm »
I forgot to mention that the socket is non-blocking and I have a few things more after that.

Naralas

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Socket try connect multiple times
« Reply #2 on: May 04, 2015, 02:02:00 pm »
Ok so, I understood my mistake. I was trying to do some kind of broadcasting (because I wanted to check if there was a server on the network), but with a TCP Socket. I now use the UDP Socket to do this and it works fine.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Socket try connect multiple times
« Reply #3 on: May 04, 2015, 02:23:52 pm »
TCP doesn't support broadcasts, plus UDP is better suited for this anyways.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Naralas

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Socket try connect multiple times
« Reply #4 on: May 08, 2015, 01:58:50 pm »
Okay, so in fact I have another problem but related I guess.

I have my udp socket and I can check and find the server on the network and it works fine. But I can't connect with TCP to the server after that. The clients seems to be able to connect but the server does not react. If I don't send data with UDP before, I can connect to the tcplistener, but otherwise not. The TCP port and the UDP are different.

Here's the server side function.
    if(selector.wait())
    {
        if(selector.isReady(listener))  // If there's a pending connection
        {
           // SFML DOC LIKE
        }

        if(selector.isReady(udp_socket))
        {
            sf::Socket::Status st = udp_socket.receive(pack,sender_ip,sender_port)
            // if what I received is correct I answer something to the client who sent the pack
            udp_socket.send(pack,sender_ip,sender_port);
         }
}

And here is where I try to connect with the client.

 
    for(int i = first_addr; i < last_addr;i++)
             udp_socket.send(pack,test,udp_port);  // Sent on every adress between first and last (test is a string which contains the full adress)
   
    udp_socket.bind(udp_port);
    udp_socket.setBlocking(false);
    udp_socket.receive(pack,server,server_port);
    udp_socket.unbind();
 

After that, I get the adress of the server who sent the pack and I try to connect to with the TCP port (which is not the same as the UDP one).
I also tested the TCP connection directly on the server with a simple program and it works.

I don't get why it does this.
Edit: Removed a lot of not related code
« Last Edit: May 08, 2015, 02:17:01 pm by Naralas »