我的服务器TCP代码:
import socket
import sys
HOST = ''
PORT = 8031
s = socket.socket()
class BoServer:
def __init__(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error,msg:
print "Unable to create socket"
sys.exit()
print "Socket created."
def bind(self):
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind((HOST,PORT))
except socket.error,msg:
print "Bind failed. Closing..."
sys.exit()
print "Socket bound."
def run(self):
s.listen(10)
print "Socket Listening"
conn, addr = s.accept()
print "Connected to %s:%s"%(addr[0],addr[1])
while True:
income = conn.recv(4096)
if income != "":
print income
def main():
serv = BoServer()
serv.bind()
serv.run()
if __name__ == "__main__":
main()
我的客户端TCP代码:
import socket
import sys
def main():
host = ""
port = 8031
message = "Hello World!"
host = raw_input("Enter IP: ")
#Create Socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print "Failed to create socket. Error code: %s Error Message: %s"%(str(msg[0]),msg[1])
sys.exit()
print "Socket created"
#Connect to server
s.connect((host,port))
while message != "/e":
#Send Data
message = raw_input("Send >> ")
try:
s.sendall(message)
except socket.error, msg:
print "ERROR %s"%(msg[1])
print "Failed to send."
s.close()
main()
我希望人们在客户端上编写我的公共ip xxx.xxx.xxx.xx并在服务器上进行连接。
当我运行服务器时,其他人无法连接到我的服务器。我只能使用localhost或本地IP访问服务器。
每个尝试连接的人都会收到此错误:
Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time.
我做错了什么?