![]() |
[Python] Simple chat program (EDIT: Now a TELNET Question)
Okay basically I need to write a simple chat program. So far I have the following for the server and client code:
Code:
# server Code:
# client I have no idea to allow it to accept connections from multiple clients. I need it to basically keep checking for a new connection. After that is done, I can already tell I'm going to have an issue with being able to receive messages from other users while I am sitting at the prompt to send a message- since that will be holding up the program. Anyone have some experience with this? |
The basic solution (not python specific) is to fork or create a thread for each incoming connection. Somewhere after you do the accept , create a thread and handle all communication from that connection in the thread. The server while loop would then go back to listening for connections and forking/creating threads as it needs.
|
Well I modified the server code a bit and got it to work somewhat but the issue was still the client would "stall" while waiting on input before it would display anything the other users would input.
One way around this I found was to use Telnet as a client... only problem is as I type each letter, it is being sent to the server; other than that, it works great! Anyone have an idea of the cause of this? And is there a way to go around it? Thanks! |
Actually, I'm not sure how good Python's support of threads or shared memory is.
(which if you are forking, and want to send data between connections, you need some way of sharing memory) So what I would suggest is to use Python's select class, and you just sit there waiting on select, when something happens in the select, if it is your listening socket, you accept a new connection, add it to the list of sockets you are waiting on and go back into the loop. If you get something from one of the "clients" you loop through your connections (except the listening socket) and send the message out to each one. I'm not too familar with Python, but I found this guide by searching on google, and it seems to be pretty good. And it even recommends using threads, so there's another way to do it: http://www.amk.ca/python/howto/sockets/ |
Code:
# client |
All times are GMT -8. The time now is 06:15 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project