我遇到這些客戶機和服務器代碼有問題,我不斷收到[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 +端口轉發或橋接模式? –