【发布时间】:2015-01-14 10:11:42
【问题描述】:
我正在尝试制作简单的客户端-服务器程序,但它仅在我在同一台计算机上运行两个脚本时才有效,当将其移动到另一台计算机时 - 它根本不建立连接。
服务器:
__author__ = 'user-pc'
import socket # Import socket module
s = socket.socket() # Create a socket object
host="0.0.0.0" # Bind with everyone
port = 13254 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close() # Close the connection
客户:
__author__ = 'user-pc'
import socket # Import socket module
s = socket.socket() # Create a socket object
host = "192.168.10.4" # Server Ip
port = 13254 # Reserve a port for your service.
s.connect((host, port))
print s.recv(1024)
s.close # Close the socket when done
你能帮我解决这个问题吗? 谢谢。
【问题讨论】:
-
服务器端口是否对客户端开放?
iptables、firewalls等等…… -
谷歌搜索“如何在 上打开端口?”
-
你的服务器和客户端在同一个网络吗?它们是如何连接到网络的?在运行您的程序之前,请尝试从客户端 ping 服务器以确保您已连接到服务器。如果您收到回复,则不是网络问题。现在运行你的程序,然后尝试 ping 13254 端口,如果没有得到回复,则可能是防火墙。
标签: python sockets client server