0

I'm building an RPC Server in golang that uses msgpack. The client is built in python using the mprpc library (msgpack over TCP with gevent).

My issue is, being an absolute noob in networking, I discovered that I can't use the same address/port with multiple clients running at once on the same computer (socket already bound i guess, it just stalls and timeouts).

I have looked around quite a bit but I'm not sure what I should be doing to be able to have multiple clients on the same machine talk to a server (msgpack back and forth). Is this a case where I need to use ZeroMQ ? Or requests over HTTP ?

Thanks !

Tuft
  • 27
  • 8

2 Answers2

0

TCP is a connection-oriented protocol. This means that only the server needs to have a fixed, known port. The client can use any port it wants, because nobody is making a connection to the client.

So, how does the server know how to talk to the client? Whenever it accepts a connection, it's told who the connection is from. But usually, you don't even need that, because the socket keeps track of who the connection is from. Just recv and send on that socket, and you're talking to the right client.


You should probably read the Socket Programming HOWTO in the Python docs, or some other tutorial, but briefly…

A server starts like this:

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', 12345))
sock.listen(5)
while True:
    csock, addr = sock.accept()

It binds a port and listens and loops around accepting connections and doing something with them.

A client, on the other hand, just does this:

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 12345))

… or, equivalently:

sock = socket.create_connection(('localhost', 12345))

It doesn't call bind, it just creates a connection, letting the sockets library pick an arbitrary port on the appropriate interface for that connection. Unless you've got thousands of sockets already open, it should always be able to find a free port for you.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Thank you very much for your answer ! So does this mean, on the same computer, i should be able to call socket.create_connection(('localhost', 12345)) multiple times from different processes ? – Tuft Jul 25 '18 at 22:01
  • @Tuft Yes, you should be able to run hundreds of programs (or the same program run hundreds of times in parallel) that all call `socket.create_connection(('localhost', 12345))`, and they'll all work (each one automatically getting a unique ports to connect from). – abarnert Jul 25 '18 at 22:57
0

If you want to have two way connection, then HTTP is not suitable for this. Because HTTP is designed in a way that the server only responds to a request, which prevents server to issue a request itself. There are other solutions that provide two way connection(server to client and client to server in same time).

WebSocket is the first thing that comes to my mind. Of course ZeroMQ also can do this.

ramazan polat
  • 7,111
  • 1
  • 48
  • 76
  • Thanks you ! I'm only looking to send a request to the server, and then get back the response, not really two-way i guess ? – Tuft Jul 25 '18 at 22:07