0

First as a disclaimer: I'm not the greatest at Python(or programming in general).

Having said that, I am having issues with Python Sockets. I am trying to build a simple chat client/server program, however, I am unable to have the server send the corresponding message(s) received from one client's socket, to the rest of the connected clients.

Here is the server code:

#!/usr/bin/python
import socket
import fnmatch
import thread

serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
## Allow socket to be reused by application - doesn't force timeout.
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host = socket.gethostname()
port = 9000
serverSocket.bind((host,port))

connectedUsers = []

serverSocket.listen(5)


def threadedClient(clientStream):
        while True:
                clientMessage = clientStream.recv(1024).decode()
                print clientMessage
                if "Username:" in clientMessage:
                        username = clientMessage.replace("Username:","")
                        print str(username) + " has connected!"
                        connectedUsers.append(clientAddress)
                        print str(username) + "" + str(clientAddress) + " has connected to server"
                        for users in connectedUsers:
                                clientStream.sendto(str(username) + " has connected!", users)
                if "Text:" in clientMessage:
                        receievedText = clientMessage.replace("Text:","")
                        for users in connectedUsers:
                                clientStream.sendto(receievedText.encode(), users)
                                print "Sending message " + str(receievedText) +" to:" + str(users)
                if not clientMessage:
                        break



while True:
        clientStream, clientAddress = serverSocket.accept()
        thread.start_new_thread(threadedClient,(clientStream,))

Here is the client code:

#!/usr/bin/python

import socket

clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 9000

username = raw_input("Please enter username: ")

clientSocket.connect((host, port))
clientSocket.send("Username:" + username)

def receiveServerMessage():
    serverMessage = clientSocket.recv(1024).decode()
    print serverMessage + '\n'


while True:
    receiveServerMessage()
    command = raw_input(username + " > ")
    if command != None:
        clientSocket.send("Text:" + str.encode(command))
    if command == str("q"):
        exit()
clientSocket.close()

The iteration seems to be awry when attempting to send the message to the other connected clients. I'm not sure if "sendto" is the proper way of handling this situation...especially since I believe it is UDP based. Any suggestions on how to handle socket stream correctly?

4
  • I suggest you to start using TCP instead of UDP since it is an easier approach. Commented Oct 7, 2015 at 21:57
  • 1
    @lilezek looks like tcp to me socket.socket(socket.AF_INET, socket.SOCK_STREAM) Commented Oct 7, 2015 at 22:00
  • The only reason I believe the command "sendto" is related to udp in some manner, is the fact that most of the tutorials/documentation found online are related towards UDP specifically. However, tcpdump on the machines show TCP communication only. Commented Oct 7, 2015 at 22:10
  • It looks like your connectedUsers object is shared across multiple threads. Given that, you need to serialize access to that object with a mutex or similar, otherwise you run the risk of one thread modifying the list while another one is reading from the list, which would cause unexpected behavior. Commented Oct 8, 2015 at 0:51

1 Answer 1

1

the problem is that the client is listening to the keyboard input before listening to the server socket , the best way to solve this is by using select() which similar to select() in c here is a good example https://pymotw.com/2/select/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.