0

i made a chat server and chat client python script the server runs on a port and as many as i like clients can connect and chat like an irc chatroom, works fine on linux, but not running on windows

here is the chat client code

import sys
import socket
import select

def chat_client():
    if(len(sys.argv) < 3) :
        print 'Usage : python chat_client.py hostname port'
        sys.exit()

    host = sys.argv[1]
    port = int(sys.argv[2])

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

    # connect to remote host
    try :
        s.connect((host, port))
    except :
        print 'Unable to connect'
        sys.exit()

    print 'Connected to remote host. You can start sending messages'
    sys.stdout.write('[Me] '); sys.stdout.flush()

    while 1:
        socket_list = [sys.stdin, s]

        # Get the list sockets which are readable
        ready_to_read,ready_to_write,in_error = select.select(socket_list , [], [])

        for sock in ready_to_read:             
            if sock == s:
                # incoming message from remote server, s
                data = sock.recv(4096)
                if not data :
                    print '\nDisconnected from chat server'
                    sys.exit()
                else :
                    #print data
                    sys.stdout.write(data)
                    sys.stdout.write('[Me] '); sys.stdout.flush()     

            else :
                # user entered a message
                msg = sys.stdin.readline()
                s.send(msg)
                sys.stdout.write('[Me] '); sys.stdout.flush() 

if __name__ == "__main__":

    sys.exit(chat_client())

the problem lies in that windows does not view sockets like files as linux do so the select function not working (I think)

problem starts from this line

ready_to_read,ready_to_write,in_error = select.select(socket_list , [], [])

can i port this script to windows ?

Shantanu Bedajna
  • 559
  • 10
  • 34
  • 2
    The sockets are not the problem. In Python, you can use Windows sockets with select. But you cannot use Windows' `stdin`/`stdout`/`stderr` with select. See the [first paragraph in the documentation of the select module](https://docs.python.org/3/library/select.html). – blubberdiblub May 07 '17 at 12:37
  • can you suggest an workaround ? – Shantanu Bedajna May 07 '17 at 12:42

0 Answers0