By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,799 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,799 IT Pros & Developers. It's quick & easy.

Python Chat Client getting stuck after entering message

P: 1
Alright, let me explain my issue to you:

So I made a chat client in Python following some tutorials, but I quickly was limited with it. The problem is that because of this process called blocking, when it came time for the client to receive a message, the whole process would stop until a reply did come through.

So I went along asking some questions here and there, and looking for a way to make the client work so that the client could write as many messages as they wanted without having to wait for a reply first.

So I wrote this:
Expand|Select|Wrap|Line Numbers
  1. from socket import *
  2. import select
  3. import sys #because why not?
  4. import threading
  5. import queue
  6.  
  7. print("New Chat Client Using Select Module")
  8.  
  9. HOST = input("Host: ")
  10. PORT = int(input("Port: "))
  11.  
  12. s = socket(AF_INET,SOCK_STREAM)
  13.  
  14. print("Trying to connect....")
  15. s.connect((HOST,PORT))
  16. s.setblocking(0)
  17. # Not including setblocking(0) because select handles that. 
  18. print("You just connected to",HOST,)
  19.  
  20. # Lets now try to handle the client a different way!
  21.  
  22. while True: 
  23.     Incoming_data = [s]
  24.     Exportable_data = []
  25.     Exceptions = []
  26.  
  27.     User_input = input("Your message: ")
  28.  
  29.     rlist,wlist,xlist = select.select(Incoming_data,Exportable_data,Exceptions)
  30.  
  31.     if User_input == True:
  32.         Exportable_data += [User_input]
  33.         print(Exportable_data)
  34.  
  35.     if wlist:
  36.         if Exportable_data is True:
  37.             print(Exportable_data)
  38.             i.send(Exportable_data)
  39.  
  40.     if rlist:
  41.         data = i.recv(1024)
  42.         if data == "":
  43.             continue
  44.  
Trouble is when I run this, it just gets stuck after you type in your input, I added some print commands in the proceeding if functions to see if it could complete any of them, but it doesn't. It seems to get stuck right away.
Mar 8 '13 #1
Share this Question
Share on Google+
1 Reply

kudos
Expert 100+
P: 127
Have you had a look at the select function? (Which are found in most operating systems?)
Jun 2 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.