Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I am generating a simple chat application using sockets (python) in which a client can send a message to the server and the server simply distributes the message to all other clients except the one who has sent it.

Client has two threads, which are running forever

send: Send simply sends the cleints message to server.

receive: Receive the message from the server.

Server also has two threads, which are running forever

accept_cleint: To accept the incoming connection from the client.

broadcast_usr: Accepts the message from the client and just broadcast it to all other clients.

But I'm getting the inaccurate output. Kindly check the codes below:

Below is the code.

chat_client.py

import socket, threading

def send():

    while True:

        msg = raw_input('\nMe > ')

        cli_sock.send(msg)

def receive():

    while True:

        sen_name = cli_sock.recv(1024)

        data = cli_sock.recv(1024)

        print('\n' + str(sen_name) + ' > ' + str(data))

if __name__ == "__main__":   

    # socket

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

    # connect

    HOST = 'localhost'

    PORT = 5023

    cli_sock.connect((HOST, PORT))     

    print('Connected to remote host...')

    uname = raw_input('Enter your name to enter the chat > ')

    cli_sock.send(uname)

    thread_send = threading.Thread(target = send)

    thread_send.start()

    thread_receive = threading.Thread(target = receive)

    thread_receive.start()

chat_server.py

import socket, threading

def accept_client():

    while True:

        #accept    

        cli_sock, cli_add = ser_sock.accept()

        uname = cli_sock.recv(1024)

        CONNECTION_LIST.append((uname, cli_sock))

        print('%s is now connected' %uname)

def broadcast_usr():

    while True:

        for i in range(len(CONNECTION_LIST)):

            try:

                data = CONNECTION_LIST[i][1].recv(1024)

                if data:

                    b_usr(CONNECTION_LIST[i][1], CONNECTION_LIST[i][0], data)

            except Exception as x:

                print(x.message)

                break

def b_usr(cs_sock, sen_name, msg):

    for i in range(len(CONNECTION_LIST)):

        if (CONNECTION_LIST[i][1] != cs_sock):

            CONNECTION_LIST[i][1].send(sen_name)

            CONNECTION_LIST[i][1].send(msg)

if __name__ == "__main__":    

    CONNECTION_LIST = []

    # socket

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

    # bind

    HOST = 'localhost'

    PORT = 5023

    ser_sock.bind((HOST, PORT))

    # listen    

    ser_sock.listen(1)

    print('Chat server started on port : ' + str(PORT))

    thread_ac = threading.Thread(target = accept_client)

    thread_ac.start()

    thread_bs = threading.Thread(target = broadcast_usr)

    thread_bs.start()

1 Answer

0 votes
by (108k points)

The problem mainly is in the broadcast_usr() function on the server. It is getting obstructed in the recv() method and blocking all but the currently chosen user from chatting at a single time as it grows through the for a loop. To fix this, I  have modified the server.py program to produce a new broadcast_usr thread for each client connection that it accepts. I hope this helps:

import socket, threading

def accept_client():

    while True:

        #accept    

        cli_sock, cli_add = ser_sock.accept()

        uname = cli_sock.recv(1024)

        CONNECTION_LIST.append((uname, cli_sock))

        print('%s is now connected' %uname)

        thread_client = threading.Thread(target = broadcast_usr, args=[uname, cli_sock])

        thread_client.start()

def broadcast_usr(uname, cli_sock):

    while True:

        try:

            data = cli_sock.recv(1024)

            if data:

                print "{0} spoke".format(uname)

                b_usr(cli_sock, uname, data)

        except Exception as x:

            print(x.message)

            break

def b_usr(cs_sock, sen_name, msg):

    for client in CONNECTION_LIST:

        if client[1] != cs_sock:

            client[1].send(sen_name)

            client[1].send(msg)

if __name__ == "__main__":    

    CONNECTION_LIST = []

    # socket

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

    # bind

    HOST = 'localhost'

    PORT = 5023

    ser_sock.bind((HOST, PORT))

    # listen    

    ser_sock.listen(1)

    print('Chat server started on port : ' + str(PORT))

    thread_ac = threading.Thread(target = accept_client)

    thread_ac.start()

    #thread_bs = threading.Thread(target = broadcast_usr)

    #thread_bs.start()

Interested in learning Python? Enroll in our Python online Course now!

Browse Categories

...