服务器不会向客户端发送数据

时间:2016-04-01 12:59:33

标签: sockets python-3.x networking network-programming python-sockets

我有这段代码供服务器处理客户端。它正确地接收数据,但当我想将收到的数据发送给客户时没有任何反应。

服务器

import socket
from _thread import *


class GameServer:

    def __init__(self):

        # Game parameters
        board = [None] * 9
        turn = 1

        # TCP parameters specifying
        self.tcp_ip = socket.gethostname()
        self.tcp_port = 9999
        self.buffer_size = 2048

        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        try:
            self.s.bind((self.tcp_ip, self.tcp_port))
        except:
            print("socket error, Please try again! ")

        self.s.listen(5)
        print('Waiting for a connection...')

    def messaging(self, conn):

        while True:
            data = conn.recv(self.buffer_size)
            if not data:
                break

            print("This data from client:", data)
            conn.send(data)

    def thread_run(self):
        while True:
            conn, addr = self.s.accept()
            print('connected to: ' + addr[0] + " : " + str(addr[1]))
            start_new_thread(self.messaging, (conn,))


def main():
    gameserver = GameServer()
    gameserver.thread_run()

if __name__ == '__main__':
    main()
'

我想如果收到的数据完全发送给客户,则检索发件人的地址并通过conn.send()将其发送给其他客户,但似乎没有办法用' send() '方法

客户端代码 '

def receive_parser(self):
        global turn
        rcv_data = self.s.recv(4096)
        rcv_data.decode()
        if rcv_data[:2] == 'c2':
            message = rcv_data[2:]
            if message[:3] == 'trn':
                temp = message[3]
                if temp == 2:
                    turn = -1
                elif temp ==1:
                    turn = 1

            elif message[:3] == 'num':
                self.set_text(message[3])

            elif message[:3] == 'txt':
                self.plainTextEdit_4.appendPlainText('client1: ' + message[3:])
        else:
            print(rcv_data)
'

接收方法不接收任何数据。

1 个答案:

答案 0 :(得分:0)

我修改了你的代码(因为我有python 2.7),conn.send()似乎工作正常。您也可以尝试conn.sendall()。这是我运行的代码:

服务器代码:

import socket
from thread import *


class GameServer:

    def __init__(self):

        # Game parameters
        board = [None] * 9
        turn = 1

        # TCP parameters specifying
        self.tcp_ip = "127.0.0.1"#socket.gethostname()
        self.tcp_port = 9999
        self.buffer_size = 2048

        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        try:
            self.s.bind((self.tcp_ip, self.tcp_port))
        except:
            print("socket error, Please try again! ")

        self.s.listen(5)
        print('Waiting for a connection...')

    def messaging(self, conn):

        while True:
            data = conn.recv(self.buffer_size)
            if not data:
                break

            print("This data from client:", data)
            conn.send(data)

    def thread_run(self):
        while True:
            conn, addr = self.s.accept()
            print('connected to: ' + addr[0] + " : " + str(addr[1]))
            start_new_thread(self.messaging, (conn,))


def main():
    gameserver = GameServer()
    gameserver.thread_run()

main()

客户代码:

import socket

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 9999))

def receive_parser():
        #global turn
    s.sendall("hello world")
    rcv_data = s.recv(4096)
        # rcv_data.decode()
        # if rcv_data[:2] == 'c2':
        #     message = rcv_data[2:]
        #     if message[:3] == 'trn':
        #         temp = message[3]
        #         if temp == 2:
        #             turn = -1
        #         elif temp ==1:
        #             turn = 1

        #     elif message[:3] == 'num':
        #         self.set_text(message[3])

        #     elif message[:3] == 'txt':
        #         self.plainTextEdit_4.appendPlainText('client1: ' + message[3:])
        # else:
    print(rcv_data)

receive_parser()