2017-05-28 20 views
0

今私が必要とするのは、1つの味を完了した後でもサーバーが接続されている場合です。strコマンド「time」を書くと、時間があり、サーバーに接続できます。 "if"文と "choice"入力の間にmakeを実行しますが、私はまだこの作業を行うことができません。私はいつも空白の応答を得てサーバから切り離されています。ソケットとクライアントの応答

私はこのコードを使用していくつかの助けが必要です

import socket 
import time 

server = socket.socket() 
server.bind(('127.0.0.1', 8820)) 
server.listen(1) 
(client_socket, client_address) = server.accept() 
client_rq = client_socket.recv(1024) 
b = time.ctime() 


if client_rq == "name": 
    client_socket.send('the server name is london project') 

if client_rq == "help": 
    client_socket.send('for help with this server connect @gmail.com') 

if client_rq == "time": 
    client_socket.send(b) 

server.close() 

と、これはクライアントです:サーバーで

import socket 
client = socket.socket() 
client.connect(('127.0.0.1', 8820)) 

x = raw_input("choice: ") 
client.send(x) 
data = client.recv(1024) 
print data 

client.close() 

答えて

0

ループ:

import socket 
import time 

server = socket.socket() 
server.bind(('127.0.0.1', 8820)) 
server.listen(1) 
while True: 
    client_socket, client_address = server.accept() 
    client_rq = client_socket.recv(1024) 
    b = time.ctime() 

    if client_rq == "name": 
     client_socket.send('the server name is london project') 

    if client_rq == "help": 
     client_socket.send('for help with this server connect @gmail.com') 

    if client_rq == "time": 
     client_socket.send(b) 

    if client_rq == "quit": 
     break # from the loop 

server.close() 

から "終了" を送信しますクライアントはループを解除してサーバーを終了します。

+0

私はクライアント上で作成する前にこれを完了できませんし、['name'、 'time'、 'exit']のような "command"のタプルを作成することはできません。 bla bla 'はサーバの応答を返す。生の入力は、client_rqが[abcd] –

+0

[a、b、c、d]でない場合に試した文字列を送信しない。カンマなしでは、すべての文字列が1つに連結されます。 – phd

関連する問題