http://stackoverflow.com/questions/15311450/my-chat-client-freezes-up-after-beginning-threads

Discussion in 'Python' started by Owatch, Mar 9, 2013.

  1. Owatch

    Owatch Guest

    I made a better chat client following help from people:

    They told me that if I didn't want to be blocked on .recv when waiting for messages, I would need to use threads, classes, functions, and queues to do so.

    So I followed some help a specific person gave me where I created a thread from a class and then defined a function that was supposed to read incoming messages and print them.

    I also created a function that allows you to enter stuff to be sent off.

    Thing is, when I run the program. Nothing happens.

    Can somebody help point out what is wrong? (I've asked questions and researched for 3 days, without getting anywhere, so I did try)



    from socket import *
    import threading
    import json
    import select

    print("Client Version 3")
    HOST = input("Connect to: ")
    PORT = int(input("On port: "))
    # Create Socket

    s = socket(AF_INET,SOCK_STREAM)
    s.connect((HOST,PORT))
    print("Connected to: ",HOST,)

    #-------------------Need 2 threads for handling incoming and outgoing messages--

    # 1: Create out_buffer:
    Buffer = []

    rlist,wlist,xlist = select.select(,Buffer,[])

    class Incoming(threading.Thread):
    # made a function a thread
    def Incoming_messages():
    while True:
    for i in rlist:
    data = i.recv(1024)
    if data:
    print(data.decode())

    # Now for outgoing data.
    def Outgoing():
    while True:
    user_input=("Your message: ")
    if user_input is True:
    Buffer += [user_input.encode()]
    for i in wlist:
    s.sendall(Buffer)
    Buffer = []


    Thanks for taking a look, thanks also to Tony The Lion for suggesting this

    If the code isnt done right, here's a link to view it, and download:

    View: http://i1267.photobucket.com/albums/jj554/owatch/PPP_zps4beb891b.png
    Download: http://www.mediafire.com/?u51a9b5axfffoxl
    Owatch, Mar 9, 2013
    #1
    1. Advertising

  2. Owatch

    Andrew Berg Guest

    On 2013.03.09 09:26, Owatch wrote:
    > Thing is, when I run the program. Nothing happens.
    >
    > Can somebody help point out what is wrong? (I've asked questions and researched for 3 days, without getting anywhere, so I did try)

    You defined a thread, but never created or started it. Also, why did you
    subclass threading.Thread? You also mentioned queues, but you didn't use
    them.

    Not tested, but shows the basics:

    import threading
    import queue
    import socket

    def process():
    while alive:
    thing = things_to_process.get()
    # do stuff with the thing here
    things_to_process.task_done()

    alive = True
    host = 'localhost'
    port = 9999
    things_to_process = queue.Queue()
    process_thread = threading.Thread(target=process)
    process_thread.start()
    sock = socket.socket()
    sock.connect((host, port))
    while alive:
    try:
    data = sock.recv()
    except Exception: # should probably do different things for different
    errors in real code
    alive = False
    sock.close()
    process_thread.join()
    raise
    else:
    things_to_process.put(data)

    --
    CPython 3.3.0 | Windows NT 6.2.9200 / FreeBSD 9.1
    Andrew Berg, Mar 9, 2013
    #2
    1. Advertising

  3. Owatch

    Owatch Guest

    On Mar 9, 6:35 pm, Andrew Berg <> wrote:
    > On 2013.03.09 09:26, Owatch wrote:> Thing is, when I run the program. Nothing happens.
    >
    > > Can somebody help point out what is wrong? (I've asked questions and researched for 3 days, without getting anywhere, so I did try)

    >
    > You defined a thread, but never created or started it. Also, why did you
    > subclass threading.Thread? You also mentioned queues, but you didn't use
    > them.
    >
    > Not tested, but shows the basics:
    >
    > import threading
    > import queue
    > import socket
    >
    > def process():
    >   while alive:
    >     thing = things_to_process.get()
    >     # do stuff with the thing here
    >     things_to_process.task_done()
    >
    > alive = True
    > host = 'localhost'
    > port = 9999
    > things_to_process = queue.Queue()
    > process_thread = threading.Thread(target=process)
    > process_thread.start()
    > sock = socket.socket()
    > sock.connect((host, port))
    > while alive:
    >   try:
    >     data = sock.recv()
    >   except Exception: # should probably do different things for different
    > errors in real code
    >     alive = False
    >     sock.close()
    >     process_thread.join()
    >     raise
    >   else:
    >     things_to_process.put(data)
    >
    > --
    > CPython 3.3.0 | Windows NT 6.2.9200 / FreeBSD 9.1


    Thanks.
    Owatch, Mar 9, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Meade

    Visual Studio: StackOverflow

    Rob Meade, Oct 26, 2005, in forum: ASP .Net
    Replies:
    23
    Views:
    8,284
    Rob Meade
    Nov 6, 2005
  2. JScoobyCed
    Replies:
    0
    Views:
    490
    JScoobyCed
    Aug 26, 2004
  3. Replies:
    0
    Views:
    2,191
  4. Forrest Sheng Bao
    Replies:
    4
    Views:
    4,380
    Michael
    Nov 9, 2009
  5. Jesse B.
    Replies:
    9
    Views:
    230
    Jesse B.
    Mar 27, 2010
Loading...

Share This Page