插座连接被拒绝[Errno 111]

时间:2016-03-05 17:24:49

标签: python sockets

我正在尝试使用C(服务器端)和Python(客户端)实现一个带套接字的简单ftp。编译并运行服务器代码时,用户输入端口号。然后客户端进入" localhost"编译时出于某种原因,当我运行代码时,我在客户端获得[Errno 111]。据说问题出在我的client.connect声明中。我尝试使用多个不同的端口号,它会抛出同样的错误:

flip1 ~/FTPClient 54% python ftpclientNew.py localhost 2500
Traceback (most recent call last):
  File "ftpclientNew.py", line 86, in <module>
    main()
  File "ftpclientNew.py", line 27, in main
    if client.connect((serverName, portNumber)) == None:
  File "<string>", line 1, in connect
socket.error: [Errno 111] Connection refused

另一个奇怪的事情是,几天前我运行相同的代码时没有发生这种连接错误。有没有人遇到过这样的问题?知道可能导致这种情况的原因吗?谢谢!

以下是客户端代码:

import sys, posix, string
from socket import *

def main():

    if len(sys.argv) < 3:
        print "\nFormat: 'localhost' <port number>!\n"
        return 0

    buffer = ""
    bufferSize = 500
    serverName = "localhost"
    fileBuffer = [10000]

    if sys.argv[1] != serverName: 
        print "Incorrect Server Name! \n"
        return 0

    portNumber = int(sys.argv[2])

    client = socket(AF_INET, SOCK_STREAM)

    if client < 0:
        print "Error Creating Socket!! \n"
        return 0

    if client.connect((serverName, portNumber)) == None:
        print "Client Socket Created...\n"
        print "Connecting to the server...\n"
        print "Connected!\n"
        ##clientName = raw_input("Enter a file name: ")

1 个答案:

答案 0 :(得分:-1)

尝试将serverName变量更改为127.0.0.1