2017-06-20 18 views
2

私はクライアントとサーバー間の通信を確立しました。Pythonでのクライアントとサーバーの通信

問題は、複数のメッセージを送信できないため、問題を解決しようとしていて、何が間違っているのかわかりません。

これは私のコードです:

**サーバコードとクライアントコードは、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私は2つのpythonウィンドウを開き、1つはサーバーコードを実行し、もう1つはクライアント。私は接続を確立するときに私は "ちょっと"を送信し、その後、私は他の何かを送信したいが、傾ける。 – hila

答えて

1

少しの保護が追加されたサーバー側。おそらくこれがあなたが必要とするものですか?サーバーは、クライアント・プロセスが終了した後も引き続き接続をlistenします。サーバを起動し、クライアントを起動し、メッセージを送信し、クライアントを再起動して別のメッセージを送信してみてください...

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を使用しています。print( "Data:"、data.decode( 'utf-8')) –

関連する問題