简体   繁体   English

错误 111:仅在 python IDLE 中拒绝连接

[英]Errno 111: Connection refused only in python IDLE

When I try to execute Python server socket program and client socket program (both running in same linux machine) in Linux terminal I don't get any error , server accepts connection and sends data to client successfully.当我尝试在 Linux 终端中执行 Python 服务器套接字程序和客户端套接字程序(都运行在同一台 linux 机器上)时,我没有收到任何错误,服务器接受连接并将数据成功发送到客户端。

But when I execute the same programs in python IDLE I get " [Errno 111] Connection refused " error.但是当我在 python IDLE 中执行相同的程序时,我得到“ [Errno 111] Connection denied ”错误。

What is the difference in both execution?两者执行有什么区别?

I'm using serversock.bind(('',port#)) in server and in client i'm using clientsock.connect(('localhost',port#))我在服务器和客户端中使用serversock.bind(('',port#))我正在使用serversock.bind(('',port#)) clientsock.connect(('localhost',port#))

Server.py服务器.py

import socket

serversock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = ''
print host
port = 5800
serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversock.bind((host,port))
serversock.listen(2)

try:
    while True:
        csock,addr = serversock.accept()
        print 'Recieved address from %s '% str(addr)
        csock.send('hello')
        csock.close()

except Exception,e:
    print e

client.py客户端.py

import socket

 try:
   c = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
   host = 'localhost'
   port = 5800
   c.connect((host,port))
   data = c.recv(1024)
   print data

except Exception,e:
   print e

finally:
   c.close()

Server side you must use:您必须使用的服务器端:

serversock.bind(('',port#))    # binds to port
serversock.listen(5)           # actually listen to port and allow 5 incoming calls
conv = serversock.accept()     # accept first incoming call

The connection will only be accepted after the listen call, before it, you have only bound a socket, but have not declared that you were ready to accept incoming calls, so they are refused.连接只会在listen调用之后被接受,在它之前,你只绑定了一个socket,但没有声明你准备好接受来电,所以他们被拒绝了。


With added code, another possible error cause is that you close connection (server side) immediately after sending data.添加代码后,另一个可能的错误原因是您在发送数据后立即关闭连接(服务器端)。 That is bad: the close condition can destroy the socket before the data has actually been sent.这很糟糕:关闭条件可能会在数据实际发送之前破坏套接字。

You should use a graceful shutdown :您应该使用正常关机

  • server side:服务器端:

     csock.send('hello') csock.shutdown(socket.SHUT_WR) # shutdown the socket csock.read() # wait the close from peer csock.close()
  • client side: you can leave the code as is in your use case you do not send anything to server, so when the client has finished reading it can safely close the socket客户端:您可以将代码保持原样,在您的用例中您不向服务器发送任何内容,因此当客户端完成阅读时,它可以安全地关闭套接字

Anyway you should close serversock when everything is finished无论如何你应该在一切完成后关闭serversock

try:
    ...
except ...:
    ...
finally:
    serversock.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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