-1

I run multiple bonjour client using pidgin, A, B, and C.
when B and C talk to A , I find A uses the same port (with wireshark I can see the packets) for MDNS and communication,
but B and C, each has two different ports one for MDNS ,one for socket connection.
how does A work, why it can work with only one port? how can one port provides multiple connections?
Attention: if it is multithread ,then when it accepts a connection it will create a new socket with another free port, but I saw the packets from wireshark, client A did just use the same port for communication and MDNS.

seaguest
  • 2,510
  • 5
  • 27
  • 45

1 Answers1

7

A TCP connection is actually identified by the tuple: (source_address, source_port, destination_address, destination_port). So as long as one of these is different there is no problem.

In practice, what you say happens when a program listens for connections in a given port: any new connection is created with the same server port (but different client port or address).

For exmample, in my Linux machine, where I have a web server listening at port 80:

$ telnet localhost 80 &
$ telnet localhost 80 &
$ lsof -n -i TCP
...
TCP 127.0.0.1:45601->127.0.0.1:80
TCP 127.0.0.1:45602->127.0.0.1:80
rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • thanks, but when I try to use port 5298 to connect to several other ports in the same machine with java socket, there is the exception "Address already in use"? you said if any element changes in the tuple, it should be OK, how could this happen? then I I tried another specified local port 5555, I got " Connection refused"? Can't we specify the local port? or should I do something to the local specified port before creating the socket? I tried to enbale this port in IPTABLE, but it doesn't work! – seaguest Oct 15 '11 at 08:50
  • 4
    Well, I said that if any element changes in the tuple, it would be ok regarding to the TCP protocol. But the socket API has additional restrictions: that you cannot bind to the same local address/port more than once (unless you set the SO_REUSEADDR option, but this has other even more subtle effects). Thus the "Address already in use" error. The "Connection refused" error happens when you try to connect to a destination address/port and there is nothing listening in that port. – rodrigo Oct 16 '11 at 18:48
  • many thanks for your answers ! in fact I solved this problem, because I didn't know that MDNS use the default port 5353, and I configure a wrong port which I used for socket for that. so now it works very well! Thank you for enthusiastic answer again! – seaguest Oct 19 '11 at 16:29