Chat client error

Chat client error

Postby zarnav » Mon Aug 22, 2016 9:22 am

Hi Everyone. I have a server chat client set up on a Debian platform and is currently running fine. I can connect from a Linux platform to my server chat client and everything is fine. The issue comes in when I try to type in messages from a Windows platform.
Basically I have changed the sys.stdin function on my Windows client with socket.socket() which has helped in fixing any connection issues. The standing issue is now that after I will connect to the server from Windows Cmd I can see other people messages but I cannot type any messages of my own in there.

I will paste in the server chat client:
Code: Select all
# chat_server.py
 
import sys
import socket
import select

HOST = ''
SOCKET_LIST = []
RECV_BUFFER = 4096
PORT = 9009

def chat_server():

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind((HOST, PORT))
    server_socket.listen(10)
 
    # add server socket object to the list of readable connections
    SOCKET_LIST.append(server_socket)
 
    print "Chat server started on port " + str(PORT)
 
    while 1:

        # get the list sockets which are ready to be read through select
        # 4th arg, time_out  = 0 : poll and never block
        ready_to_read,ready_to_write,in_error = select.select(SOCKET_LIST,[],[],0)
     
        for sock in ready_to_read:
            # a new connection request recieved
            if sock == server_socket:
                sockfd, addr = server_socket.accept()
                SOCKET_LIST.append(sockfd)
                print "Client (%s, %s) connected" % addr
                 
                broadcast(server_socket, sockfd, "[%s:%s] entered our chatting room\n" % addr)
             
            # a message from a client, not a new connection
            else:
                # process data recieved from client,
                try:
                    # receiving data from the socket.
                    data = sock.recv(RECV_BUFFER)
                    if data:
                        # there is something in the socket
                        broadcast(server_socket, sock, "\r" + '[' + str(sock.getpeername()) + '] ' + data) 
                    else:
                        # remove the socket that's broken   
                        if sock in SOCKET_LIST:
                            SOCKET_LIST.remove(sock)

                        # at this stage, no data means probably the connection has been broken
                        broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)

                # exception
                except:
                    broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
                    continue

    server_socket.close()
   
# broadcast chat messages to all connected clients
def broadcast (server_socket, sock, message):
    for socket in SOCKET_LIST:
        # send the message only to peer
        if socket != server_socket and socket != sock :
            try :
                socket.send(message)
            except :
                # broken socket connection
                socket.close()
                # broken socket, remove it
                if socket in SOCKET_LIST:
                    SOCKET_LIST.remove(socket)
 
if __name__ == "__main__":

    sys.exit(chat_server())

And here please find the client chat code:
Code: Select all
# 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 = [socket.socket(), 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())

Any ideas will be much appreciated. Regards!
Last edited by Yoriz on Mon Aug 22, 2016 11:50 am, edited 1 time in total.
Reason: First post lock. Added code tags.
zarnav
 
Posts: 2
Joined: Mon Aug 22, 2016 9:15 am

Re: Chat client error

Postby VexisArcanum » Tue Aug 30, 2016 12:57 am

I have the same problems with console-based IO. The only solution I can think of is to have multiple windows or a GUI front-end. Here's a stackoverflow link that explains how to open multiple terminal windows using subprocess:
http://stackoverflow.com/questions/1947 ... gle-script

Or you could use something like tkinter to make GUI windows that can be updated independently. Your choice
Last edited by ichabod801 on Tue Aug 30, 2016 10:35 pm, edited 1 time in total.
Reason: First post lock
VexisArcanum
 
Posts: 4
Joined: Tue Aug 30, 2016 12:52 am

Re: Chat client error

Postby zarnav » Wed Aug 31, 2016 9:20 am

VexisArcanum wrote:I have the same problems with console-based IO. The only solution I can think of is to have multiple windows or a GUI front-end. Here's a stackoverflow link that explains how to open multiple terminal windows using subprocess:
http://stackoverflow.com/questions/1947 ... gle-script

Or you could use something like tkinter to make GUI windows that can be updated independently. Your choice



Hi VexisArcanum, thank you for your post.
I will check building individual GUIs with Tkinter hoping the issue will be resolved that way.
zarnav
 
Posts: 2
Joined: Mon Aug 22, 2016 9:15 am


Return to Networking

Who is online

Users browsing this forum: No registered users and 4 guests

cron