Why am I getting the error “connection refused” in Python? (Sockets)

后端 未结 7 1421
轻奢々
轻奢々 2020-12-01 02:48

I\'m new to Sockets, please excuse my complete lack of understanding.

I have a server script(server.py):

#!/usr/bin/python

import socket #import t         


        
7条回答
  •  一生所求
    2020-12-01 03:21

    Assume s = socket.socket() The server can be bound by following methods: Method 1:

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

    Method 2:

    host = socket.gethostbyname("localhost")  #Note the extra letters "by"
    s.bind((host, port))
    

    Method 3:

    host = socket.gethostbyname("192.168.1.48")
    s.bind((host, port))
    

    If you do not exactly use same method on the client side, you will get the error: socket.error errno 111 connection refused.

    So, you have to use on the client side exactly same method to get the host, as you do on the server. For example, in case of client, you will correspondingly use following methods:

    Method 1:

    host = socket.gethostname() 
    s.connect((host, port))
    

    Method 2:

    host = socket.gethostbyname("localhost") # Get local machine name
    s.connect((host, port))
    

    Method 3:

    host = socket.gethostbyname("192.168.1.48") # Get local machine name
    s.connect((host, port))
    

    Hope that resolves the problem.

提交回复
热议问题