0

Socket port - python

I understand that a client needs to connect to a port that the server listens to. But does the server respond to the client with the same port he listens to?

22nd Mar 2021, 6:13 PM
Yahel
Yahel - avatar
4 Answers
+ 1
Yahel wrote, "...But does the server respond to you on the same port?" Response: Let's think of it like phone numbers. You as a client need to know what number to call to reach the server. The server needs to own that phone number. Calling the server doesn't require your client phone to be registered with the same phone number, though. This is why I'm saying "no" to your question. The client and server become 2 sides of a connection. Each almost always have different port numbers. If this was an HTTP server, the port would often by 80. The client port would often be several thousand.
23rd Mar 2021, 7:55 AM
Josh Greig
Josh Greig - avatar
+ 1
It sounds like you're wondering if the TCP/IP or HTTP server needs to make use of the same port number to respond to a client. No, it doesn't. The client needs to know what number to find the server. The server can respond to a client on any dynamically selected client port. The server can even be multithreaded and respond to multiple clients in parallel.
23rd Mar 2021, 7:13 AM
Josh Greig
Josh Greig - avatar
0
Josh Greig but when you set up a client you do that: client_socket.connect((server ip, port)). And when you set up a server you do: server_socket.bind((ip, port)). The port number should be the same in order to make a connection, right? Meaning that you send data to the server through the specific port. But does the server respond to you on the same port?
23rd Mar 2021, 7:42 AM
Yahel
Yahel - avatar
0
Josh Greig Ohhh, I get it. Thanks!
23rd Mar 2021, 8:44 AM
Yahel
Yahel - avatar