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

Author Topic: Managing multiple connections to the same port  (Read 4205 times)

0 Members and 1 Guest are viewing this topic.

Charsmud

  • Newbie
  • *
  • Posts: 30
    • View Profile
Managing multiple connections to the same port
« on: June 24, 2014, 08:14:36 pm »
I am implementing a heartbeat interface for my server, and have run across the following problem.  I am managing the heartbeat on a specific port number.  When a user connects, the server establishes a heartbeat object for that client, and in that class a "connection" is established with the client.  However, because I am handling it on one specific port, if another user trys to connect, the heartbeat cannot be created and tells me that the port is in use, as it should.  I'm stuck on trying to figure out if there is a way to allow for multiple sockets to connect to the same port.  If this is simply not possible, how else could I implement the heartbeat?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Managing multiple connections to the same port
« Reply #1 on: June 24, 2014, 08:19:16 pm »
You can have a listening socket on a specific server port (say 1234). When a client connects and you accept() that connection, the result is a unique socket for that client. New clients can still connect to 1234 and get their own unique sockets. You then listen for data for all your unique sockets (and the listening socket) and service them when there's data or other significant events on them.

Charsmud

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Managing multiple connections to the same port
« Reply #2 on: June 24, 2014, 08:32:08 pm »
Thanks!  I'm a little confused by the listening socket though, and how hat interacts with the unique sockets....  So, from what I understand, one socket listens for connections on the port (1234).  However, I'm a bit confused on what occurs when a client connects, and how I would create the unique socket for the client... It might also be good to note that I'm using UDP.  Should I use TCP for my heartbeats and UDP for the actual game?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Managing multiple connections to the same port
« Reply #3 on: June 24, 2014, 08:39:04 pm »
For this piece of the puzzle, UDP vs TCP doesn't matter.
You listen on some port for new connections.
When a new connection happens and you accept(); it, a unique socket is formed from 4 components: server IP, server port, client IP, client port - an identifier for this unique socket is what accept returns. Your listening socket is not affected, it just keeps listening for new connections for you to accept.

Charsmud

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Managing multiple connections to the same port
« Reply #4 on: June 24, 2014, 08:46:42 pm »
Ok, I think I'm grasping this a bit better.  So, this pseudo-code seems like it would work?

sf::UdpSocket accept(sf::Packet packet)

//Takes data from the packet (the client IP, client port, server IP)
//finds an open port on the server
//creates a new socket