Jump to content

Python Sockets


Recommended Posts

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)

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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())

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.