This is the server program
# Echo server program
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
Now, I was wondering if the port i'm using is let us say is 50007 and my friend who is on a uni computer wants to use the client program in order to connect to me. Then does he have to have port 50007 open as well? You know, in order for him to connect to me.
Here is the client program BTW:
import socket
HOST = 'daring.cwi.nl' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
s.close()
print('Received', repr(data)
-
yes, any two machine communicating at a specific port must have a port open for Listen and sending data respectively in the firewallavasal– avasal2012年09月03日 03:57:06 +00:00Commented Sep 3, 2012 at 3:57
-
The client don't necessarily needs to open the port number 50007 on its side, it can use any other port number too to connect to the server. The only things is that the server has to open port no. 50007 on its side. Read this post for details. stackoverflow.com/questions/21827790/…ρss– ρss2014年02月20日 21:24:14 +00:00Commented Feb 20, 2014 at 21:24
2 Answers 2
uni network should allow outgoing tcp connections to port 50007. Your network should allow incoming tcp connections on port 50007.
Comments
This will work (from a python sockets perspective). When your friend connects to your socket (which is 'open' by virtue of the the .accept() call), that connection will stay open until specifically closed, allowing you to send your message back.
Now, the problem could be the university firewall, which is almost certainly configured to not allow outbound (from him to you) connections on port 50007. If this is the case, unless you have administrator access for the uni firewall you won't be able to connect.