2013-03-25 137 views
0

我最近從休假回來,現在我的基本python 2套接字服務器無法通過局域網與客戶端進行通信。服務器在Mac上,客戶端是我的樹莓派或我的Windows 7機器。我在這裏簡化了服務器和客戶端的代碼來舉個例子:Python:無法通過局域網連接的套接字

服務器

import socket 
from thread import * 

HOST = socket.gethostname() 

print HOST 

PORT = input ("Enter the PORT number (1 - 10,000)") 



s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
print "Socket Created" 


s.bind((HOST, PORT)) 

print "Socket Bind Complete" 

s.listen(10) 
print "Socket now listening" 


    #Sending message to connected client 
    #This only takes strings (words 


while True: 
    #Wait to accept a connection - blocking call 
    connection, addr = s.accept() 
    print "Connection Established!" 

    connection.send("Welcome to the server. Type something and hit enter\n") 

    #loop so that function does not terminate and the thread does not end 
    while True: 

     #Receiving from client 
     data = connection.recv(1024) 
     if not data: 
      break 
     connection.sendall(data) 
     print data 
    connection.close() 
s.close() 

客戶

import socket #for sockets 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
print "Socket Created" 

#Get host and port info to connect 
host = raw_input("HOST >>> ") 
port = 2468 
s.connect((host, port)) 


while True: 
    #Send some data to the remote server 
    message = raw_input(">>> ") 

    #set the whole string 
    s.sendall(message) 


    reply = s.recv(1024) 
    print reply 

問題

這到底是怎麼回事?我正在獲取本地IP,但腳本仍無法通信。這可能是操作系統的問題嗎?


MORE INFO

  1. 執行ping

    一個。我能夠從我的Mac終端ping P1:

    PING raspberrypi (67.63.55.3): 56 data bytes 
    64 bytes from 67.63.55.3: icmp_seq=0 ttl=240 time=17.434 ms 
    64 bytes from 67.63.55.3: icmp_seq=1 ttl=240 time=18.180 ms 
    64 bytes from 67.63.55.3: icmp_seq=2 ttl=240 time=22.046 ms 
    64 bytes from 67.63.55.3: icmp_seq=3 ttl=240 time=25.124 ms 
    64 bytes from 67.63.55.3: icmp_seq=4 ttl=240 time=31.773 ms 
    

    b。我的PI無法找到Mac作爲主機。我會看看我能做些什麼來彌補這一點。

    c。我的電腦能夠PING我的Mac。我的Mac能夠ping通我的電腦

  2. 防火牆

我的Mac的防火牆關閉。我將檢查[Raspberry Pi Stackexchange Site]以查看PI是否有防火牆。

我會添加更多的信息,一旦我測試我的Windows機器

+0

你可以ping其他機器嗎? SSH他們?他們的IP地址是靜態的還是動態的? – jozzas 2013-03-25 05:53:40

+0

@jozzas不知道IP的(我知道我的外部是動態的)Pinging信息被添加。 – xxmbabanexx 2013-03-27 19:18:03

+0

在Linux下,如果zou不是超級用戶,禁止綁定到低於1024的端口。 – User 2014-01-16 16:55:04

回答

0

本地運行兩個腳本,它們管理連接和我的機器上進行通信。您正面臨網絡問題,應該很容易調試。

  1. 錯誤的綁定。 在服務器上,打印您獲得的主機。如果服務器有多個IP,您可能會嘗試綁定錯誤的IP。您也可以將其更改爲'0.0.0.0'(僅在服務器端),然後查看是否有效。

  2. 防火牆。 任何一方都可能在操作系統級別阻止tcp通信。調試通過Windows上的Wireshark和unix上的tcpdump完成。開始嗅探,運行你的代碼,看看出了什麼問題。很有可能你會看到客戶端發送一個SYN數據包,但服務器將無法回答SYN|ACK數據包。如果看到SYN數據包到達服務器,請嘗試完全關閉服務器的防火牆並重試。如果不是,客戶端禁止傳出通信(不太可能),並且您需要關閉其防火牆。

  3. 正在使用的端口。 嘗試刪除SO_REUSEADDR以進行調試,並查看是否有更改。

  4. 例外。 確保您不會忽略套接字中的任何異常。

+0

感謝您的幫助。我嘗試了你所建議的一切,我仍然有問題。我的新信息在上面。 – xxmbabanexx 2013-03-27 19:17:20

+0

你可以發佈你的tcpdump輸出嗎? – Ohad 2013-03-29 16:05:04

+0

嗯......那是什麼?我會找到一種方法來做到這一點...你是否需要它爲我的所有電腦 – xxmbabanexx 2013-03-29 16:23:48

0

你的代碼工作正常,我雖然做了一些小的修改,並在代碼中的註釋解釋他們:

SERVER:

import socket 
from thread import * 

# 1.Gets the local ip/ip over LAN. 
HOST =socket.gethostbyname(socket.gethostname()) 

print HOST 

# 2.Use port no. above 1800 so it does not interfere with ports already in use. 
PORT =input ("Enter the PORT number (1 - 10,000)") 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
print "Socket Created" 
s.bind((HOST, PORT)) 
print "Socket Bind Complete" 
s.listen(10) 
print "Socket now listening" 
while True: 
    connection, addr = s.accept() 
    print "Connection Established!" 
    connection.send("Welcome to the server. Type something and hit enter\n") 
    while True: 
     data = connection.recv(1024) 
     if not data: 
      break 
     connection.sendall(data) 
     print data 
     connection.close() 
s.close() 

客戶:

import socket 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
print "Socket Created" 
host = raw_input("HOST >>> ") 

# 3. Use the same port no. entered on server.py as the client binds to the 
# same port 
port = input ("Enter the PORT number (1 - 10,000)") 
s.connect((host, port)) 

while True: 
    message = raw_input(">>> ")  
    s.sendall(message) 
    reply = s.recv(1024) 
    print reply 

T他上面的代碼對我來說工作得很好,我相信它也適用於你,因爲我遇到過同樣的麻煩。發現的錯誤我已經把它們放在代碼裏面的註釋中看一看。

乾杯....!

+0

很快回復,如果可以的話(我敢肯定它會是)....! – 2013-09-23 17:31:06