我遇到这些客户机和服务器代码有问题,我不断收到[Errno 10061]因为目标机器积极拒绝它Errno 10061:由于目标机器主动拒绝它(客户机 - 服务器),因此无法建立连接
我在Windows XP SP3的虚拟机上运行服务器,在Windows 7 64bit上运行客户机,我的python版本是2.7.3。我想知道的是如何编辑代码以在不同网络上使用客户端和服务器!谢谢!
服务器:
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = '0.0.0.0' # Get local machine name
port = 12345 # Reserve a port for your service.
print 'Server started!'
print 'Waiting for clients...'
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
while True:
msg = c.recv(1024)
print addr, ' >> ', msg
msg = raw_input('SERVER >> ')
c.send(msg);
#c.close() # Close the connection
客户端:
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
print 'Connecting to ', host, port
s.connect((host, port))
while True:
msg = raw_input('CLIENT >> ')
s.send(msg)
msg = s.recv(1024)
print 'SERVER >> ', msg
#s.close # Close the socket when done
PS:代码是从互联网上。
_actively refused it_暗示服务器机器的防火墙阻塞了连接,可能与代码无关。 –
防火墙已关闭:/仍然出现错误 – havox
您的VM guest虚拟机如何连接到网络?通过NAT +端口转发或桥接模式? –