2

I have one simple question can i run two tcp Socket listeners but every each of them to listen on a different port like one server but 2 port's to be listen? Because i have dilemma with the tcp programming and at all socket programming and this is something that i never read about on the net .

Thanks this is Schema of what i want to do This is The Diagram of the Connection We have One IP and Two Different connection ports

i hope this information is full for the problem and will give you my idea on a ease way to understand . Thanks again

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mojiko0007
  • 31
  • 5
  • Simple answer: yes :) In fact you will even have to use different ports for each listener, this is how your services are addressed on your server – Sebastian Aug 30 '16 at 12:54

2 Answers2

1

can i run two tcp Socket listeners but every each of them to listen on a different port like one server but 2 port's to be listen?

Yes you can. Just declare 2 different IPEndPoint

IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 8888);
IPEndPoint localEndPoint2 = new IPEndPoint(ipAddress, 8880);

and bind the respective Socket to it

Socket s1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

Socket s2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

s1.Bind(localEndPoint);
s2.Bind(localEndPoint2);
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
0

It is possible.

To quote @BuckCherry

..two clients can connect to same server port because for each client we can assign a different socket (as client IP will definitely differ). Same client can also have two sockets connecting to same server port - since such sockets differ by SRC-PORT...

First remember below two rules:

  1. Primary key of a socket: A socket is identified by {SRC-IP, SRC-PORT, DEST-IP, DEST-PORT, PROTOCOL} not by {SRC-IP, SRC-PORT, DEST-IP, DEST-PORT} - Protocol is an important part of a socket's definition.

  2. OS Process & Socket mapping: A process can be associated with (can open/can listen to) multiple sockets which might be obvious to many readers.

Example 1: Two clients connecting to same server port means: socket1 {SRC-A, 100, DEST-X,80, TCP} and socket2{SRC-B, 100, DEST-X,80, TCP}. This means host A connects to server X's port 80 and another host B also connects to same server X to the same port 80. Now, how the server handles these two sockets depends on if the server is single threaded or multiple threaded (I'll explain this later). What is important is that one server can listen to multiple sockets simultaneously.

More info here

Community
  • 1
  • 1
tinonetic
  • 7,751
  • 11
  • 54
  • 79