0

My server is starting normal, but when I run my client on that port, it's not working. This is my error code from the client:

OSError: [WinError 10038] An operation was attempted on something that is not a socket

This is the script of the client:

chat_client.py

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())
  • Possible duplicate of [Can select() be used with files in Python under Windows?](https://stackoverflow.com/questions/10842428/can-select-be-used-with-files-in-python-under-windows) – Steffen Ullrich Feb 26 '18 at 09:49
  • Not working for me. – Yoeri thor Feb 26 '18 at 10:44
  • The question I refer to describes clearly that `sys.stdin` can not be used within `select` on windows - but this is what you are doing. – Steffen Ullrich Feb 26 '18 at 10:46
  • But how should I write it then? – Yoeri thor Feb 26 '18 at 10:47
  • This does not seem to be trivial. But see [Using sys.stdin in select.select on Windows](https://stackoverflow.com/questions/12499523/using-sys-stdin-in-select-select-on-windows) for more information. – Steffen Ullrich Feb 26 '18 at 10:54
  • I don't understand that article, and I don't have some of that code in my script – Yoeri thor Feb 26 '18 at 11:13
  • You are using `sys.stdin` in `select` and that's what this is about. If you don't understand it - sorry for you. – Steffen Ullrich Feb 26 '18 at 11:19
  • Okay, I get it. But what should I use instead? – Yoeri thor Feb 26 '18 at 11:26
  • Do I need to use "select"? – Yoeri thor Feb 26 '18 at 11:28
  • *"Do I need to use "select"?"* - Do you even understand what you are trying to achieve with the code or did you just copy the code from somewhere but don't understand it? – Steffen Ullrich Feb 26 '18 at 12:16
  • I am experementing with my friend, on his apple laptop it is working. We want to make some automatic pyton programs for our raspberry pi's. It's a chat for on the local internet. – Yoeri thor Feb 26 '18 at 12:18
  • Given that you don't address the main aspect of my question I'm assuming that you don't understand the code yet. Especially since it looks pretty much like a sample chat program one can find for example at https://github.com/keyan/python-socket-chat/blob/master/chat_client.py. In short: you need `select` but it will not work with `sys.stdin` on Windows. But it will work on Mac, Linux etc , i.e. on your target platform. Thus to have it easy don't use Windows for developing in this case. – Steffen Ullrich Feb 26 '18 at 12:29

0 Answers0