0

私は現在、最初の基本的なソケットサーバーをコーディングしています.サーバーがクライアントと通信するための2つの異なる形式に出くわしました.クライアントとサーバー間で通信する別の方法。2つの違いは何ですか?また、どのように使用するのが最適ですか?connchannels

これが要求されたコードです-私のN00binessを許してください:)

接続/スレッド

def clientthread(conn):

    #Sending message to connected client
    #This only takes strings (words)
    conn.send("Welcome to the server. Type something and hit enter\n")

    #loop so that function does not terminate and the thread does not end
    while True:

        #Receiving from client
        data = conn.recv(1024)
        if not data:
            break
        conn.sendall(data)
        print data


    #To close the connection
    conn.close()

while True:

    #Wait to accept a connection - blocking call
    conn, addr = s.accept()
    #display client information (IP address)
    print 'Connected with ' + addr[0] + ':' + str(addr[1])

    #Start new thread takees 1st argument as a function name to be run, second
    #is the tuple of arguments to the function

    start_new_thread(clientthread ,(conn,))

チャンネル

while True:

    #Wait to accept a connection - blocking call
    channel, details = s.accept()
    #display client information (IP address)
    print 'Connected with ', details

    #Start new thread takees 1st argument as a function name to be run, second
    #is the tuple of arguments to the function

    #Sending message to connected client
    #This only takes strings (words)
    intro = "Welcome to the server. Type something and hit enter"
    print channel.recv(len(intro))

    channel.send(intro)

    #loop so that function does not terminate and the thread does not end
    while True:

        #Receiving from client
        data = channel.recv(1024)
        if not data:
            break
        channel.sendall(data)
        print data

channel1 つが を使用し、もう1 つが を使用することを除いて、それらはまったく同じことを行いますconn

4

0 に答える 0