Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Dec 1st, 2005, 3:52 PM   #1
monkijunki2
Newbie
 
Join Date: Nov 2005
Posts: 7
Rep Power: 0 monkijunki2 is on a distinguished road
help with threads in python

Hi, I'm creating a simple client and server using threads in the client. My thread receives data from the server while the rest of my program keeps sending data as much as it wants. The problem is when I'm done sending data, the thread hangs. This is my first program with a thread. I tried putting a condition in so that when I finish sending, it would change the condition to false, but my thread did not respond to that. Any suggestion?

class getAck(threading.Thread):
    def run(self):
         while (1):
            s.recv(line)
            print line

s = socket(AF_INET, SOCK_DGRAM)

b = getAck()
b.start()

while( line != None ):   #line.next returns None if no more data
    s.sendto(line, addr)
    line = line.next()
monkijunki2 is offline   Reply With Quote
Old Dec 1st, 2005, 5:38 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
By default, sockets in python are blocking. This means that if there is no data waiting to be received on the socket, they will keep waiting until there is some.

If you were using a TCP stream, then I believe when the other side closed down their connection, your socket would be closed also. (It's late, so I won't bother looking up the details). TCP is akin to a telephone call; it's clear when the other party hangs up.

You, however, are using a stateless lossy UDP packet stream (with SOCK_DGRAM), which akin to sending postcards. Unlike a telephone call, I can't tell when the person I'm communicating with will stop sending postcards, unless I work out some sort of agreed system beforehand.

For instance, the system for ending the communication's channel could be:

If other person sends "QUIT", then reply with "ACK QUIT" (standing for; I've received your quit signal and am quitting), then quit.
OR
If no data is received for 60 seconds, then quit.

Timeouts on sockets are pretty easy to set up:
s = socket(AF_INET, SOCK_DGRAM)
s.settimeout(60.0)   # 60 second timeout
Out of interest, why are you using UDP as opposed to TCP? If you merely want to send streams of data, then TCP would seem the way to go. UDP is generally only used for real-time applications, such as VoIP or internet gaming.
Arevos is offline   Reply With Quote
Old Dec 1st, 2005, 6:10 PM   #3
monkijunki2
Newbie
 
Join Date: Nov 2005
Posts: 7
Rep Power: 0 monkijunki2 is on a distinguished road
I'm suppose to write a program that uses UDP and takes care of reliable data transfer for one of my cs lab.
monkijunki2 is offline   Reply With Quote
Old Dec 2nd, 2005, 1:56 AM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
In which case, you'll need to have a system which acknowledges packets that have been sent, and a system which can retain the order the packets have been sent by. UDP doesn't guarentee that packets will get to their destination, nor in the right order.

A communications channel could look something like this (S = send, R = received):
S: CONNECT 1
R: ACK 1
S: MESSAGE 2 Hello there
S: MESSAGE 3 How are you?
R: ACK 3
R: ACK 2
R: MESSAGE 1 I hear you loud
S: ACK 1
R: MESSAGE 2 And clear
S: ACK 2
S: QUIT 3
(( no ACK in 6 seconds; packet must have been lost ))
S: QUIT 3
R: ACK 3
Notice that I've put in acknowledging packets, to check that a packet has been send (ACK packets), and for each packet send by a client, I've given in a unique number. This ensures the packets arrive in order. So "MESSAGE 2 Hello there" is numbered 2, and the server acknowlegdes its received this with "ACK 2".
Arevos is offline   Reply With Quote
Old Dec 2nd, 2005, 6:37 AM   #5
monkijunki2
Newbie
 
Join Date: Nov 2005
Posts: 7
Rep Power: 0 monkijunki2 is on a distinguished road
Yeah, I understood that. It turned out I didn't understand what a thread was. I thought my thread was hanging, but it was just because my recv function was blocking. I threw in a condition to fix that though. If you're curious to the program I'm writing, here's the link: http://www.cs.ucr.edu/cs164/assignments/project.pdf
I'm 90% done with it. Just need the last part.

Thanks for the replies. Your first reply made me realize the problem =D
-Huy
monkijunki2 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 4:53 PM.

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