2016-06-12 35 views
3

嗨我想我需要再次你的幫助:(的Python - socket通信,多個消息

我卡在這個socket通信,我到處找,但我還沒有找到答案尚未

該問題:我只可以從客戶端發送1條消息之前它要麼給我一個錯誤或結束腳本 我需要能夠將多個消息發送到服務器

服務器端(示出。下面)應該很好:

# Echo server program 
import socket 
import time 
import os 

#----------------------------------------------------------------------------------------------------------------------- 

today = time.strftime('%Y.%m.%d') 
logFileName = "log - " + today + ".txt" 


HOST = '10.0.0.16'               
PORT = 8080                 # Reserve a port for your service 
BUFFER_SIZE = 1024               
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)      # Create a socket object 
s.bind((HOST, PORT))              # Bind to the port 


def print_write(text): 
    log.write(time.strftime("%H:%M:%S") + " | " + text) 
    log.write("\n") 
    print text 


#----------------------------------------------------------------------------------------------------------------------- 


if os.path.isfile(logFileName) is True: 
    log = open(logFileName, 'a+') 
    print_write("[SERVER] Log for " + today + " already exists.") 
    print_write("[SERVER] Starting comms") 
else: 
    print "[SERVER] Log doesn't exist" 
    log = open(logFileName, 'a+')           # Create file -> log - %date%.txt 
    print_write("[SERVER] Log created") 


while True: 
    s.listen(1) 
    conn, addr = s.accept() 
    data = conn.recv(BUFFER_SIZE) 
    if data == "Comms Shutdown": 
     print_write("------ REMOTE SHUTDOWN ------") 
     conn.close() 
     raise SystemExit 
    else: 
     print_write("[COMMS] " + str(addr) + " says: " + data) 

log.close() 

對不起,如果它是非常混亂和混亂,但我沒有太多時間來完成這個項目,如果你有任何問題只是問。

對於客戶端,我沒有很多,但在這裏,我給你這一點:

import socket 

HOST = '10.0.0.16'   # The remote host 
PORT = 8080     # The same port as used by the server 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect((HOST, PORT)) 

while True: 
    msg = raw_input() 
    s.sendall(msg) 
    print msg 

我知道這是行不通的,它只是給你什麼,我需要一個想法。

預先感謝您。

+0

您的服務器代碼在再次循環前只調用recv一次,您應該使用while循環或類似的東西在打開新連接前多次調用'recv' .. – noteness

回答

3

問題是,您只能從每個打開的連接中讀取第一條消息,然後才能轉到下一個連接。 accept()方法等待新的連接,併爲新的連接提供所需的信息。另一方面,recv()方法從現有連接接收數據,並等待沒有數據。如果您希望從單個客戶端接收多條消息,則可以等待第一個連接,然後等待recv()的數據。這可能是這樣的:

s.listen(1) 
conn, addr = s.accept() 
while True: 
    data = conn.recv(BUFFER_SIZE) 
    if data == "Comms Shutdown": 
     print_write("------ REMOTE SHUTDOWN ------") 
     conn.close() 
     raise SystemExit 
    else: 
     print_write("[COMMS] " + str(addr) + " says: " + data) 

如果你希望能夠還管理多個客戶,你將不得不爲每個一個從while循環等待新的連接線程。這有點複雜:

def client_handler(conn): 
    while True: 
     data = conn.recv(BUFFER_SIZE) 
     if data == "Comms Shutdown": 
      print_write("------ REMOTE SHUTDOWN ------") 
      conn.close() 
      raise SystemExit 
      # this will kill the server (remove the line above if you don't want that) 
     else: 
      print_write("[COMMS] " + str(addr) + " says: " + data) 

while True: 
    s.listen(1) 
    conn, addr = s.accept() 
    recv_thread = threading.Thread(target=client_handler, args=(conn,)) 
    recv_thread.start() 

所有這些代碼是未經測試的。請注意,我省略了日誌記錄部分和套接字創建部分以及所有導入。

+0

正是我在找的!太好了,非常感謝。 – Pinco