2011-11-24 79 views
4

我一直在爲此工作2天,但仍然無法使其正常工作。Python:2個套接字,從A發送到B,從B發送到A

我想寫一個使用2個插槽,在中間

該介質的介質是這個腳本應該從的SocketA讀寫SocketB和SocketB讀取和寫入的SocketA的應用程序。

但是,看來我不能釘它。

我的腳本運行時接受連接,但它不會允許我在telnet屏幕上輸入內容。

我使用套接字之間的2個共享列表來傳遞數據。

#!/usr/bin/env python 
    import sys 
    import arduinoReadThread 
    import arduinoWriteThread 
    import socket 
    import thread 



    bolt = 0 
    socketArray=list() 
    HOST ="" 
    HOST2="" 
    PORT1 =50115 
    PORT2 =50125 



    s1=socket.socket(socket.AF_INET, socket.SOCK_STREAM) #create an INET, STREAMing socket 
    s1.bind((HOST,PORT1)) #bind to that port 
    s1.listen(2) #listen for user input and accept 1 connection at a time. 
    socketArray.append(s1) 
    s2=socket.socket(socket.AF_INET, socket.SOCK_STREAM) #create an INET, STREAMing socket 
    s2.bind((HOST2,PORT2)) #bind to that port 
    s2.listen(2) #listen for user input and accept 1 connection at a time. 
    socketArray.append(s2) 
    print "sockets set up" 

    s1ToWriteList = list() 


    s2ToWriteList = list() 


    def socketFunctionWrite1(): 
      while(bolt == 0): 
       client, address = s1.accept() 

       print "Writing connections" 
       if len(s1ToWriteList) > 0: 
        client.send(s1ToWriteList.pop(0)) 


    def socketFunctionRead1(): 
      while(bolt == 0): 
       client2, address = s2.accept() 

       f = client2.recv(1024) 

       print "reading connection" 
       s1ToWriteList.append(f) 
       print len(s1ToWriteList) 

    def socketFunctionWrite2(): 
      while(bolt == 0): 
       client2, address = s2.accept() 
       print "Writing connections" 
       if len(s2ToWriteList) > 0: 
        client2.send(s2ToWriteList.pop(0)) 



    def socketFunctionRead2(): 
      while(bolt == 0): 
       client, address = s1.accept() 
       f = client.recv(1024) 
      print "reading connection" 
      s2ToWriteList.append(f) 
      print len(s2ToWriteList) 




def shutDown(): 
     test = raw_input("Quit ?") 
     if(test =="y"): 
      bolt = 1 
     else: 
      shutDown() 

def spreadSockets(): 


     thread.start_new_thread(socketFunctionRead1,()) 
     print "launch 1" 
     thread.start_new_thread(socketFunctionRead2,()) 
     print "launch 2" 
     thread.start_new_thread(socketFunctionWrite1,()) 
     print "launch 3" 
     thread.start_new_thread(socketFunctionWrite2,()) 

     print "launch 4" 




spreadSockets() 
while(True): 
     pass 

回答

2

使用您的確切代碼,它爲我工作。我認爲你可能做錯了是通過telnet到錯誤的IP。不要使用'localhost'或127.0.0.1,你需要使用你的盒子的實際(內部)IP。

如果在linux上,你可以看到是否在ifconfig -aipconfig /all上的窗口。

運行你的代碼完全,無修改(除了移除頂部的2個未知進口):

推出腳本:

[ 15:01 [email protected] ~/SO/python ]$ ./sock.py 
sockets set up 
launch 1 
launch 2 
launch 3 
launch 4 
Writing connections 
Writing connections 
^CTraceback (most recent call last): 
    File "./sock.py", line 93, in <module> 
    time.sleep(1) 
KeyboardInterrupt 

然後telnet'd:

[ 15:01 [email protected] ~ ]$ telnet 10.10.1.11 50115 
Trying 10.10.1.11... 
Connected to 10.10.1.11. 
Escape character is '^]'. 
Hello, World! 
Hello 2 
^] 
telnet> quit 
Connection closed. 
[ 15:02 [email protected] ~ ]$ telnet 10.10.1.11 50125 
Trying 10.10.1.11... 
Connected to 10.10.1.11. 
Escape character is '^]'. 
Hello 50125! 
Hi! 
^] 
telnet> quit 
Connection closed. 
[ 15:02 [email protected] ~ ]$ 

我的內部接口配置(inet addr:10.10.1.11):

[ 15:07 jon[email protected] ~/SO/python ]$ ifconfig eth0 
eth0  Link encap:Ethernet HWaddr **:**:**:**:**:** 
      inet addr:10.10.1.11 Bcast:10.10.1.255 Mask:255.255.255.0 
      ... 
+0

必須是我做錯了,然後:/ –

+0

@L請務必不要使用本地主機,因爲套接字通常只有當你通過一個真正的IP連接到他們的eth eth連接到它們。 – chown

+0

另外,改變:while(True):pass' this:'while(True):time.sleep(0.25)'這樣你的cpu不會爆炸成火熱的熔化硬件。 – chown

相關問題