Kyle Sweet
Kyle Sweet

Reputation: 315

Why can't I connect two sockets?

I am writing client code that deals with data streams coming from two servers A & B. All three nodes are on the same local subnet. I'm trying to create two sockets defined as described below.

Socket description= [local/client IP]:[local/client port]:[remote/server IP]:[remote/server port]

Socket to connect to A: [192.168.40.10]:[50002]:[192.168.40.60]:[50002]

Socket to connect to B: [192.168.40.10]:[50002]:[192.168.40.61]:[50002]

When I attempt to open the second socket, I get an exception that "each socket address a.k.a. protocol/network address/port can be used only once". I thought I could define a different socket since the remote IP address is different even though the local IP address and port number are the same. Do I have no choice but to consume both data streams through the same socket?

Upvotes: 0

Views: 772

Answers (2)

Rahul
Rahul

Reputation: 77846

You are using the same TCP port [50002] to connect to make two different connection which is not possible. You will have to use different port to have two separate socket connection.

When you talk about identifying two connection uniquely, it goes by IP + PORT

Upvotes: 1

Bastian Thiede
Bastian Thiede

Reputation: 468

You cannot use the same local port of your machine twice at the same time. You could open another socket (with another port number, e.g. 50003) at you local machine, and connect to remote socket on [192.168.40.61]:[50002].

Upvotes: 2

Related Questions