![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 12
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Posts: 12
Rep Power: 0
![]() |
thanks for that clarification and the example : )
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Posts: 12
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jun 2005
Posts: 12
Rep Power: 0
![]() |
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..?
|
|
|
|
|
|
#6 | |||||
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
Quote:
Quote:
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:
while True:
s, c = serversocket.accept()
# processing of the data read, then start again waiting for the next connectionQuote:
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 = TrueHope that makes sense. Ask if you need any clarification. |
|||||
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jun 2005
Posts: 12
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|