Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 30th, 2005, 10:11 AM   #1
cypherkronis
Newbie
 
Join Date: Jun 2005
Posts: 12
Rep Power: 0 cypherkronis is on a distinguished road
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.
cypherkronis is offline   Reply With Quote
Old Jun 30th, 2005, 1:40 PM   #2
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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.
Cerulean is offline   Reply With Quote
Old Jun 30th, 2005, 5:18 PM   #3
cypherkronis
Newbie
 
Join Date: Jun 2005
Posts: 12
Rep Power: 0 cypherkronis is on a distinguished road
thanks for that clarification and the example : )
cypherkronis is offline   Reply With Quote
Old Jun 30th, 2005, 5:26 PM   #4
cypherkronis
Newbie
 
Join Date: Jun 2005
Posts: 12
Rep Power: 0 cypherkronis is on a distinguished road
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.

Last edited by cypherkronis; Jun 30th, 2005 at 5:39 PM.
cypherkronis is offline   Reply With Quote
Old Jun 30th, 2005, 6:05 PM   #5
cypherkronis
Newbie
 
Join Date: Jun 2005
Posts: 12
Rep Power: 0 cypherkronis is on a distinguished road
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..?
cypherkronis is offline   Reply With Quote
Old Jun 30th, 2005, 7:54 PM   #6
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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.
Cerulean is offline   Reply With Quote
Old Jul 1st, 2005, 5:49 PM   #7
cypherkronis
Newbie
 
Join Date: Jun 2005
Posts: 12
Rep Power: 0 cypherkronis is on a distinguished road
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.
cypherkronis is offline   Reply With Quote
Old Jul 1st, 2005, 5:59 PM   #8
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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.
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:08 PM.

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