2017-06-20 36 views
2

我建立了客戶機 - 服務器通信。Python中的客戶機 - 服務器通信

問題是我不能發送多個消息,我試圖修復它,我不知道什麼是錯的。

這是我的代碼:

**的服務器代碼和客戶端代碼在兩個不同的蟒蛇窗口中運行。

服務器:

import socket 

s = socket.socket()      # Create a socket object 
host = socket.gethostname()    # Get local machine name 

port = 12345        # Reserve a port for your service. 
s = socket.socket() 
s.bind((host, port))      # Bind to the port 

s.listen(5)        # Now wait for client connection. 

conn, addr = s.accept() 
print('Got connection from ', addr[0], '(', addr[1], ')') 

while True: 
    data = conn.recv(1024) 
    print(data.decode("utf-8")) 
    if not data: 
     break 
    conn.sendall(data) 

conn.close() 

print('Thank you for connecting') 

客戶端:

import socket       # Import socket module 

host = socket.gethostname()   # Get local machine name 
port = 12345      # Reserve a port for your service. 
conn = socket.socket()     # Create a socket object 

conn.connect((host, port)) 

conn.sendall(b'Connected. Wait for data...') 

intosend = input("message to send:") 
conn.sendall(bytes(intosend, 'utf-8')) 

data = conn.recv(1024) 
intosend= "no" 

while intosend != "quit": 
    intosend = input("message to send:") 
    conn.sendall(bytes(intosend, 'utf-8')) 



conn.close()         # Close the socket when done 


print(data.decode("utf-8")) 

有人可以幫忙嗎?

+1

您要在哪裏發送多條消息?您的意圖是客戶端向服務器發送一系列消息,服務器將其回送給客戶端? – pynewbie

+0

在編寫代碼時,客戶端程序在發送第一條消息後退出,這會關閉套接字連接。服務器抱怨。如果您希望能夠從客戶端發送多個消息,則您將在客戶端和服務器端都需要循環或事件結構。有關於這個如果你搜索互聯網或看看這裏很好的基本文章https://pymotw.com/2/socket/tcp.html –

+0

@pynewbie我打開兩個python窗口,在一個運行服務器代碼,在另一個客戶。當我建立連接時,我發送「嘿」,之後我想發送其他內容,但不能。 – hila

回答

1

服務器端添加了一點保護。也許這就是你需要的?客戶端進程完成後,服務器將繼續偵聽連接。嘗試通過啓動服務器,啓動客戶端,發送消息,再次啓動客戶端併發送另一條消息...

import socket 
import threading 
import sys 

s = socket.socket()      # Create a socket object 
host = socket.gethostname()    # Get local machine name 

port = 12345        # Reserve a port for your service. 
s = socket.socket() 
s.bind((host, port))      # Bind to the port 

s.listen(5)        # Now wait for client connection. 

def processMessages(conn, addr): 
    while True: 
     try: 
      data = conn.recv(1024) 
      if not data: 
       conn.close() 
      print(data.decode("utf-8")) 
      conn.sendall(bytes('Thank you for connecting', 'utf-8')) 
     except: 
      conn.close() 
      print("Connection closed by", addr) 
      # Quit the thread. 
      sys.exit() 


while True: 
    # Wait for connections 
    conn, addr = s.accept() 
    print('Got connection from ', addr[0], '(', addr[1], ')') 
    # Listen for messages on this connection 
    listener = threading.Thread(target=processMessages, args=(conn, addr)) 
    listener.start() 
1

您需要在客戶端循環接受輸入併發送它的代碼。

此代碼在客戶端爲我工作。

import socket       # Import socket module 

host = socket.gethostname()    # Get local machine name 
port = 12345       # Reserve a port for your service. 
conn = socket.socket()     # Create a socket object 

conn.connect((host, port)) 

conn.sendall(b'Connected. Wait for data...') 

while 1: 

    intosend = input("message to send:") 
    conn.sendall(intosend.encode('utf-8')) 
    #data received back from sever 
    data = conn.recv(1024) 
    print("Data: ", data.decode('utf-8')) 
conn.close()         # Close the socket when done 


print(data.decode("utf-8")) 
+0

糟糕,我意外地發佈了服務器代碼。固定。 –

+0

對不起,您正在使用Python 3.嘗試打印(「Data:」,data.decode('utf-8')) –