Take the 2-minute tour ×

I am writing 2 small programs (a server and a client) and whenever I run both, and have the client connect to the server, the server output says that I am connected on a port of which I didn't bind in the code. I binded both the server and the client socket to the localhost and port 8000, but every time the server is connected to by the client, it says that the client is connected on port 52304 or some other number larger than 50000, shouldn't it at least be a constant port number even if it isn't the one I bound it to? Also, I know, that if I run the server program more than once in the same terminal, even if I exited the program, the port is still taken, so I usually run the server, quit, then exit the terminal, which usually solves that problem. That is another note I should make, when I do run the server program the second time in the same terminal, it recognizes I am trying to bind to port 8000 and the program wont run, then when it does it chooses some random port.

Here is my server code:

import socket
import os

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind('', 8000)


while 1:
    client,addr = s.accept()
    print "Accepted a connection from: ", addr
    data = client.recv(1024)
    client.send("You said: " + data)

share|improve this question
The results you're seeing are perfectly expected and are what should be happening, and what you likely actually want to happen. Please post your client code. –  Omnifarious Apr 3 '11 at 20:27

3 Answers 3

up vote 2 down vote accepted

The port number it's reporting is the one the client connected from. And it will be a random port number. If (as your question seems to imply) you have a bind call in the client that looks just like the one in the server, then I'm surprised it's succeeding since the server has already bound itself to that port and only one thing can be bound to a given port at a time.

Please post your client code. Contrary to what your question implies, I don't think that you are binding to a port on the client side. I'm betting you're just connecting. Now, that, generally speaking, is what you're supposed to be doing. So the fact you're confused by the results just means that you don't really understand what's happening exactly. The results you're seeing are perfectly expected and normal.

Here is an explanation of what's going on:

A TCP connection is uniquely identified (globally unique, as in no other TCP connections in the entire world will have the same identifier (though this isn't really exactly true with NAT and private IP ranges)) by these 4 pieces of information:

  1. client ip
  2. client port #
  3. server ip
  4. server port #

When your server is reporting a connection, it's printing out the first two values because they are what is returned by the accept call. When you are doing a bind call in the server, you are specifying values 3 and 4. The OS generally picks values 1 and 2 for the client automatically when it does a connect call.

A client normally does not bind to a port (though it can). It normally lets the OS pick a port for it. The client's OS will pick a port number from a list of unused port numbers. In your connect call on the client side, you are giving values 3 and 4 (the values specified in the bind call on the server side). The OS should automatically assign your client values 1 and 2 for you.

Think about it like the sender and recipient address on an envelope. The accept call on the server side reports the sender address because presumably the server already knows its own address. The client is most concerned with the recipient address (the address of the server) and lets a clerk (the OS) just paste on a return address,

share|improve this answer

The port and socket that the server listens on is not the same socket that is used once the connection is established. The accept call creates a new socket when a client connects for sending and receiving data. Otherwise if it used the same socket...then no other clients would be able to connect.

You also need to properly close your socket so it does not hang around after your program terminates.

share|improve this answer
This does not answer the OPs question. The OP is clearly confused about exactly how TCP connections work, and this provides no real explanation of what's going on. –  Omnifarious Apr 3 '11 at 20:30
@Omnifarious No this does explain the confusion. He was confused as to why the listening socket had a different port# than the send and receive socket that is created from the accept call. He assumed that the connection socket was the same as the socket that is returned...which it is not. –  eat_a_lemon Apr 3 '11 at 20:46
No, I understand, thanks –  mcwillig Apr 3 '11 at 23:24

You can never bind the same port to more then one program, the port the server gives you is the port the client wants you to send the data over. I think its to avoid connection collisions.

So you don't have to worry about the ports if the connection is establish.

But if you want the server to be able to receive more the one connection take a look at this: Multiple simultaneous network connections - Telnet server, Python.

share|improve this answer
This does not answer the OPs question either, and instead sends the OP off on some barely relevant tangent. –  Omnifarious Apr 3 '11 at 20:30

Your Answer


