获取python tcp服务器发送和回复一旦发送的消息?

时间:2016-10-05 23:47:33

标签: python sockets tcp concurrency

我一直在尝试使用python进行网络编程。为了教我自己如何做到这一点,我一直在玩多个版本的TCP聊天服务器和客户端。我最新的尝试让我想知道我刚刚编写的代码有什么问题。只有当我刚刚创建的客户端发送了一条消息时,其他消息才会进入。我不确定为什么会发生这种情况。我一直在网上搜索,我无法弄清楚它为什么会发生。我所知道的是,这不是服务器的错误。我确信问题出在我刚创建的客户端上。

import socket, thread, threading, os

def sendMsg():
    Message = raw_input('[-]You:')
    s.send(Message)

def recvMsg():
    data = s.recv(buff)
    print(data)

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

host = raw_input('[-]Target Ip-> ')
port = 5000
buff = 1024

try:
    s.connect((host, port))

except:
    print('[-]Failed to Connect')
    s.close()

loop = True
threads = []
while loop == True:
    try:
        t1 = threading.Thread(target=sendMsg())
        threads.append(t1)
        t1.start()

        t2 = threading.Thread(target=recvMsg())
        threads.append(t2)
        t2.start()

    except KeyboardInterrupt:
        print('\n')
        break


s.close()
os.system('clear')

服务器代码

# Tcp Chat server

import socket, select, os, time

def broadcast_data (sock, message):
    for socket in CONNECTION_LIST:
            if socket != server_socket and socket != sock :
            try :
                socket.send(message)
            except :
                socket.close()
                CONNECTION_LIST.remove(socket)

if __name__ == "__main__":

        CONNECTION_LIST = []
    RECV_BUFFER = 4096
    IP_MNG = ''
    PORT = 5000     
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind((IP_MNG, PORT))

    server_socket.listen(10)
    CONNECTION_LIST.append(server_socket)

    print "[-]Connected To " + str(PORT)

    start = True 
    while start == True:
        try:
            read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])

            for sock in read_sockets:
                    if sock == server_socket:
                    sockfd, addr = server_socket.accept()
                    CONNECTION_LIST.append(sockfd)
                    print "Client (%s, %s) connected" % addr
                    broadcast_data(sockfd, "[-][%s:%s] entered room\n" % addr)


                else:

                        try:
                        data = sock.recv(RECV_BUFFER)
                        if data:
                            broadcast_data(sock, "\r" + '<' + str(sock.getpeername()) + '> ' + data)                

                    except:
                        broadcast_data(sock, "Client (%s, %s) is offline" % addr)
                        print "Client (%s, %s) is offline" % addr
                        sock.close()
                        CONNECTION_LIST.remove(sock)
                        continue

        except KeyboardInterrupt:
            print('[-]Server Stopped')
            start = False


server_socket.close()
time.sleep(1)
os.system('clear')

1 个答案:

答案 0 :(得分:0)

我对套接字编程很新,所以这可能是darK中的一个镜头:

  • 客户端正在生成大量线程,从阻塞套接字读取和写入。看起来一次只能执行一个操作,一个线程在写入时阻塞或线程在读取时阻塞,因为只有一个套接字,所有线程都在该单个资源上运行。你可以让你生成的每个线程打开它自己的套接字连接写入它的读取,这应该解决问题。
  • 对于客户端,套接字是否读/写线程安全? Yes
  • 在服务器上,accept ed连接是否应标记为非阻塞?

    sockfd, addr = server_socket.accept()

    sockfd.setblocking(0)

  • 在服务器上,您是否需要管理套接字何时可写?它看起来像套接字,因为它们变得可读。我想在读取调用期间会socket.send阻塞,因此在此期间不会处理新连接。 Python module of the week有非常明显的例子,将select用于非阻止服务器