2016-03-05 1122 views
-1

我想用C(服務器端)和Python(客戶端)的套接字實現一個簡單的ftp。當服務器代碼編譯並運行時,用戶輸入一個端口號。客戶端在編譯時輸入「localhost」。出於某種原因,當我運行代碼時,我在客戶端獲得了[Errno 111]。這是說,這個問題是與我的client.connect聲明。我曾使用多個不同的端口號審判,它拋出同樣的錯誤:Socket Connection Refused [Errno 111]

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

確保使用'netstat'打開端口,嘗試另一個客戶端或服務器,使用'nc'和'tcpdump'進行調試。 –

+0

111意味着沒有服務器在端口上偵聽,或者服務器主動拒絕了連接。如果你的服務器設置了它可能沒有的「逗留」選項,那麼第二個只會是真的。這是Linux/MAC?有人建議你看看用'netstat'打開了哪些端口。這是個好主意。你的代碼有點混亂(它需要你輸入一個主機名,但是如果它不是'localhost'則會出錯),但是應該工作。它的服務器端,這是一個問題。您可以添加'print gethostbyname('localhost')'以確保您沒有系統配置問題。 – tdelaney

回答

-1

嘗試將serverName變量更改爲127.0.0.1

+0

@sjrd這產生了與上面相同的錯誤。 –

相關問題