简体   繁体   中英

Establishing a socket connection between computers?

I was learning about networking and I have some trouble understanding what went wrong.

I created a Client and a Server script:

Server:

import socket 

s = socket.socket()
host = socket.gethostname()
port = 12345 
s.bind((host,port))  

s.listen(5)
while True: 
    c, addr = s.accept() 
    print ("Got connection from: " ,addr) 
    c.send("Thanks".encode('utf-8'))
    # c.sendto(("Thank you for connection").encode('utf-8'), addr)
    c.close()

and Client:

import socket 

s=socket.socket()
host=socket.gethostname()
port = 12345

s.connect((host,port))
c=s.recv(1024)
print (c)

s.close

When I am trying to run from my computer I have no problem (both scripts) But when I am running the Client from another Computer, the following error pops up for the Client: ConnectionRefuseError: WinError10061 No connection could be made because the target machine actively refused it .

Got any idea what could fix this?

The problem was that I wasn't referring to the server IP when running from another computer, I fixed it by passing the server IP, in the client script, like this host = "10.xxx"

Sorry for creating a useless question!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM