使用套接字编程将服务器接收的消息发送到python 2.7中的多个客户端

时间:2016-02-25 22:23:24

标签: python sockets server client chat

所以我为客户端和服务器创建了一个套接字程序作为基本聊天。我做了它,所以服务器接受多个带线程的客户端,所以这不是问题。我无法向连接到服务器的每个客户端发送消息。我不是试图让服务器发送它创建的消息,而是让client1通过服务器向client2发送消息。出于某种原因,它只会将其发回client1

例如,client1会打招呼,服务器会将相同的消息发送回client1,但不会发送回client2。我通过确保客户端没有收到自己的消息但client2仍然没有收到client1的消息来解决这个问题。

任何帮助将不胜感激。

我尝试了多项更改,但似乎没有任何效果。您可以查看我的代码,了解我的工作方式,但询问是否有任何问题。

此外,还有一个问题,有人问过这是类似的,我认为它会给我一个答案但是答案停止了,并且解决方案从未完全给出,所以请不要只是引用我这个问题。位于此处:Python 3: Socket server send to multiple clients with sendto() function

以下是代码:

客户端:     导入套接字     导入系统     导入线程

#Create a socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#Enter username to identify self to others
name = raw_input("Enter username: ") + ": "

#Connect socket to ip and port
host = socket.gethostname()
#host = '192.168.1.10'
server_address = (host, 4441)
sock.connect(server_address)

#function waiting to receive and print a message
def receive(nothing):

    while True:

        data = sock.recv(1024)
        if message != data:
            print data

# Send messages
while True:
    #arbitrary variable allowing us to have a thread
    nothing = (0, 1)

    message = name + raw_input("> ")
    sock.sendall(message)

    #thread to receive a message
    thread.start_new_thread(receive, (nothing,))

服务器

import socket
import sys
import thread

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
host = socket.gethostname()
server_address = (host, 4441)
sock.bind(server_address)

#Listen for incoming connections
sock.listen(5)
print "Waiting for connection..."

#Variable for the number of connections
numbOfConn = 0

#Name of list used for connections
addressList = []

#Function that continuosly searches for connections
def clients(connection, addressList):

    while True:

        message = connection.recv(1024)
        print message

        #connection.sendall(message)
        #for loop to send message to each
        for i in range(0,numbOfConn - 1):
            connection.sendto(message, addressList[i])

    connection.close()

while True:
    #accept a connection
    connection, address = sock.accept()
    print 'Got connection from', address
    numbOfConn += 1
    addressList.append((address))

    #Thread that calls the function: clients and stores them in a tuple called connection
    thread.start_new_thread(clients, (connection, addressList))


sock.close()

如果可以,请帮助我!

修改

我能够在一定程度上修复它。它仍然是一个小小的车,但我现在能够来回发送消息。我需要指定连接套接字以及地址。这是更新后的代码:

服务器

import socket
import sys
import thread

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
host = socket.gethostname()
server_address = (host, 4441)
sock.bind(server_address)

#Listen for incoming connections
sock.listen(5)
print "Waiting for connection..."

#Variable for the number of connections
numbOfConn = 0

#Name of list used for connections
addressList = []
connectionList = []

#Function that continuosly searches for connections
def clients(connectionList, addressList):

    while True:
        for j in range(0,numbOfConn):
            message = connectionList[j].recv(1024)
            print message

            #for loop to send message to each
            for i in range(0,numbOfConn):
                connectionList[i].sendto(message, addressList[i])

    connection.close()

while True:
    #accept a connection
    connection, address = sock.accept()
    print 'Got connection from', address

    numbOfConn += 1
    addressList.append((address))
    connectionList.append((connection))

    #Thread that calls the function: clients and stores them in a tuple called connection
    thread.start_new_thread(clients, (connectionList, addressList))


sock.close()

0 个答案:

没有答案