Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   help with sockets, having a client recieve data as well as send. (http://www.programmingforums.org/showthread.php?t=4677)

cypherkronis Jun 30th, 2005 10:11 AM

help with sockets, having a client recieve data as well as send.
 
i have a semi-complex socket program that I just wrote ( first foray into sockets in python ) but for the purposes of figuring this out, I'll use a much simpler example.
:

#client.py

from socket import *

#host = "localhost"
host = "127.0.0.1"
#host = "70.107.93.83";
port = 21567
buf = 1024
addr = (host,port)

UDPSock = socket(AF_INET,SOCK_DGRAM)

login = 0;

while (1):
        data = raw_input('enter a message')
        if not data:
                break
        else:
            if(UDPSock.sendto(data,addr)):
                print "data", data, "sent";

UDPSock.close()

#server.py

from socket import *

host = "localhost"
port = 21567
buf = 1024
addr = (host,port)

UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)

while 1:
        data,addr = UDPSock.recvfrom(buf)
        if not data:
                print "Client has exited!"
                break
        else:
                print "\nReceived message '", data,"'"

UDPSock.close()


in these examples, client.py sends data to server.py. Its my understanding that server.py checks for incoming data because it has binded itself to the specified address. Beyond that, the two sockets seem identical. So, i wrote a method that has server.py use the same send command that client does to send a message to the client. this didnt work. I thought it was because I hadn't bound client to an address, so I did. However, i bound it to the same address as the server, so I changed the port (because theyre both running on the same computer). This didnt work, the program crashed, and good old python doesnt stay on to show you the run time errors, it just turns itself off. Can anyone modify the above example so that both can act as client/servers on the same computer? Thanks.

Cerulean Jun 30th, 2005 1:40 PM

The server is just the program that has bound itself to a port and will handle any data that is sent to that port. The client doesn't need to bind itself to anything, it just sends and receives data from the socket it's connected on. The simple socket example shows the client sending and receiving data from the server.

cypherkronis Jun 30th, 2005 5:18 PM

thanks for that clarification and the example : )

cypherkronis Jun 30th, 2005 5:26 PM

question: my example was of UDP sockets, ures is TCP. it seems TCP can't handle multiple clients on one port while UDP can. is there anyway to make change either so they can have both up and down communication as well multiple clients. or am i mistaken, and what you gave me is just the new way of doing what i was (ie, my way is deprecated). thanks...

edit: just to be concise, forget all i said about differences and what not, simply, is there any way for more than one client to connect to that particular server, cuz it seems that its only listening to one particular port and address ( one client ) at a time. thanks.

final edit ( and i shall leave you alone ) : is it possible to sort of wait for unique connections and keep them in a list, and later loop thru each waiting for incoming data making sure to keep them distinct. i'll try this but im not sure how to implement it. alternatively, i can have one thread in the server wait for incoming connections, and then maintain seperate threads for each client. it only occured to me now that the blasted accept() method waits till something actually connects. thats so odd, what if i dont know how many clients i'm expecting.

cypherkronis Jun 30th, 2005 6:05 PM

ok i think ive almost figured it out. thanks for your help man. im just gunna use a lot of threads, i hope python and my processor can handle it. not sure if there are smarter solutions. thanks. now how do i close a thread..?

Cerulean Jun 30th, 2005 7:54 PM

Quote:

Originally Posted by cypherkronis
question: my example was of UDP sockets, ures is TCP. it seems TCP can't handle multiple clients on one port while UDP can. is there anyway to make change either so they can have both up and down communication as well multiple clients. or am i mistaken, and what you gave me is just the new way of doing what i was (ie, my way is deprecated). thanks...

I think you need to read up on the difference between TCP and UDP. Both "can handle" multiple clients per se, so that should not be a worry of yours.

Quote:

edit: just to be concise, forget all i said about differences and what not, simply, is there any way for more than one client to connect to that particular server, cuz it seems that its only listening to one particular port and address ( one client ) at a time. thanks.
Read the below

Quote:

final edit ( and i shall leave you alone ) : is it possible to sort of wait for unique connections and keep them in a list, and later loop thru each waiting for incoming data making sure to keep them distinct.
I recommend you read the documentation on the members called in code samples, it will save you and us a lot of time.
This is precisely what happens when you call serversocket.listen(int). In between each call to serveroscket.accept(), it will keep that many connections waiting in line to be processed.

Quote:

alternatively, i can have one thread in the server wait for incoming connections, and then maintain seperate threads for each client. it only occured to me now that the blasted accept() method waits till something actually connects. thats so odd, what if i dont know how many clients i'm expecting.
How do you mean, precisely? Bear in mind you usually put the accept call in a loop, like so:
:

while True:
    s, c = serversocket.accept()
    # processing of the data read, then start again waiting for the next connection

You don't need to know how many clients you're accepting...

Quote:

ok i think ive almost figured it out. thanks for your help man. im just gunna use a lot of threads, i hope python and my processor can handle it. not sure if there are smarter solutions. thanks. now how do i close a thread..?
This is the route I tend to take, and i'm fairly sure your processor can handle it ;-). I have a WorkerThread class (that inherits from threading.Thread) that I pass the client connection object to, and then call start() on it to let it do its own work in the thread. This way, I keep the connection queue length down as the main server socket can respond pretty much immediately to each new connection, while the threads do the work.
As for stopping a thread, a thread automagically stops when the run() function stops, so you can easily do something like the following:
:

class MyThread(threading.Thread):
  def __init__(self):
    self.stop = False
    threading.Thread.__init__(self)
  def run(self)
    # when self.stop is no longer False, then loop will stop and the
    # thread will stop.
    while self.stop is False:
      # ... do whatever here

t = MyThread()
# Start the thread...
t.start()
# ... and after ten seconds...
time.sleep(10)
# stop it.
t.stop = True


Hope that makes sense. Ask if you need any clarification.

cypherkronis Jul 1st, 2005 5:49 PM

lol, i meant, how do i close a topic thread on this forum. thanks for ure help, i made a very hot solution ( im proud of it ) and im turning this cool pygame game into an online thing now. thanks.

Cerulean Jul 1st, 2005 5:59 PM

That's great to hear :-)
As for closing a thread, don't bother. You may just want to put (solved) in the title or something of the sort.


All times are GMT -5. The time now is 6:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC