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

Author Topic: Question about Socket Selector and UDP sockets  (Read 3220 times)

0 Members and 1 Guest are viewing this topic.

Pandaman407

  • Newbie
  • *
  • Posts: 3
    • View Profile
Question about Socket Selector and UDP sockets
« on: October 24, 2015, 02:43:33 am »
I've started diving into the magic world of networking in SFML. All is fine, I got a nice UDP server and client to run. However, I read on the sfml tutorials about the socket selector class, and I started to ask myself questions. Does my server, which is one UDP socket, have to store each client as a seperate socket, or would I store the IP addresses to know who is who?

Server code (receiving and sending. very simple atm)
while (running)
        {
                if (selector.wait())
                {
                        if (selector.isReady(socket))
                        {
                                sf::IpAddress rec;
                                unsigned short rPort;
                                sf::Packet packet;

                                socket.receive(packet, rec, rPort);

                                std::cout << "Recieved a packet from: " << rec.toString() << " " << rPort << std::endl;
                                std::string x;
                                packet >> x;
                                std::cout << "Packet:[ " << x << " ]" << std::endl;
                        }

                }
        }
as you can see, im using socket selector, but I'm thinking it's pointless.

FleshyOverlord

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Question about Socket Selector and UDP sockets
« Reply #1 on: June 10, 2018, 11:28:09 pm »
From my basic understanding of sfml's networking library and networking in general, TCP requires a connection while UDP does not. When you call the send command for TCP it tells the other end that there is a packet coming, waits for a reply, then sends the package, waits for conformation that the package was received, and then continues with it's business. Due to TCP requiring a connection between the client and server you often need to have multiple sockets (usually one for each client).

UDP, on the other hand, often just sends the packet without notifying the receiver that it is going to send a packet. Due to this lack of connection between the server and client UDP only requires one socket to receive and send data.

The reason socket Selectors exist is to prevent socket blocking (mainly with TCP) from becoming a problem. With the socket selector you can poll the status of and receive from many sockets without having to wait on any one socket to receive data. Using UDP with a socket selector would be pointless because there's only one socket (the receiving and sending socket) meaning there aren't any other sockets to block. Because of this, you should probably just store the IP address of each client since you can't make a socket for each client with UDP, you can only make one for the receiving and sending end of the server.

TLDR you should just store the IP's of the clients.
« Last Edit: June 10, 2018, 11:42:39 pm by FleshyOverlord »