The Little Guy Posted August 8, 2010 Share Posted August 8, 2010 I have been running into a small issue with my sockets. 99% of this works, what is supposed to happen is the server runs listens for incoming connections accept them and when a message is sent to the server, it sends back the same message to the client(s) connected. It works, but the client is in a loop, so it asks for an input message you input one and it sends it to the server and the server replies. If you try to do it a second time, nothing happens unless you restart the client program. Is this a problem in my server code or my client code or both, and what is the problem? The following is the server: #! /usr/bin/python import socket mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) mySocket.bind (('', 2727)) mySocket.listen(100) print 'Server started...' while True: client, details = mySocket.accept() try: info = client.recv(100) client.send(info) except: client.close() This is a test client: import socket mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mySocket.connect(('chat.gmserver.net', 2727)) while True: input = raw_input('Message: ') mySocket.send(input) print mySocket.recv(100) Quote Link to comment https://forums.phpfreaks.com/topic/210169-python-sockets/ Share on other sites More sharing options...
corbin Posted August 11, 2010 Share Posted August 11, 2010 The socket accept is accepting a new client every time the loop goes. You need: while True: client, details = mySocket.accept() try: while True: info = client.recv(100) client.send(info) except: client.close() Note that to support more than 1 client at a time, that code will have to be changed. Quote Link to comment https://forums.phpfreaks.com/topic/210169-python-sockets/#findComment-1097918 Share on other sites More sharing options...
The Little Guy Posted August 11, 2010 Author Share Posted August 11, 2010 Sweet thanks that worked! What do you mean it will have to be changed? What will I need to do? I changed it to run on threads. I don't know how correct this is. I would like to support more than one different chat at a time. #! /usr/bin/python import socket import threading import Queue class Chat(threading.Thread): def run ( self ): while True: client = clientPool.get() if client != None: while True: info = client[0].recv(100) client[0].send(info) else: client[0].send('message') clientPool = Queue.Queue(0) Chat().start() server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(('', 2727)) server.listen(5) while True: clientPool.put(server.accept()) Quote Link to comment https://forums.phpfreaks.com/topic/210169-python-sockets/#findComment-1097962 Share on other sites More sharing options...
trq Posted August 11, 2010 Share Posted August 11, 2010 You should take a look at the twisted library if your planning on making a half decent server. Allot of the grunt work will already be done for you this way. Quote Link to comment https://forums.phpfreaks.com/topic/210169-python-sockets/#findComment-1097969 Share on other sites More sharing options...
corbin Posted August 12, 2010 Share Posted August 12, 2010 Ooo Twisted looks pretty cool. Almost makes me want to learn Python. TLG, with your approach right now, the easiest way to do it is this (although I would probably go the Twisted route): Have the main thread listening for new client connections. Every time a new client connection is received, spawn a new thread. That thread's entire purpose of existence will be to handle one client connection. Basically it will be like: main thread: while(new client) { new thread for client } client threads: while(connection open) { read socket and process } The hardest part of that approach will be the synchronization so that you can act on more than 1 socket if needed. (For example, to send a message from a to b, you'd have to use a's input stream and b's output stream, meaning you will need a way to manage connections outside of threads. Basically a locking system.) I could show you some Java code if you wanted, but I don't know Python well enough to accomplish anything. thorpe's suggestion looks pretty cool too. You'd probably be better off using it than trying to reinvent it. Then again, it might be better to start low down then move to Twisted so you know what's going on better. Quote Link to comment https://forums.phpfreaks.com/topic/210169-python-sockets/#findComment-1098644 Share on other sites More sharing options...
The Little Guy Posted August 13, 2010 Author Share Posted August 13, 2010 Some java code would be ok, I probably will somewhat understand it. I did take a class... Thanks for any help though! Quote Link to comment https://forums.phpfreaks.com/topic/210169-python-sockets/#findComment-1098731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.