Results 1 to 6 of 6
- 08-29-2009, 07:42 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 3
- Rep Power
- 0
best way to signal a thread that's blocking on a socket receive
Hi,
I've poked around but can't find anyone talking about this specifically; maybe I don't know the right keywords. At any rate...
I have a thread (I call it the broker thread) that needs to be able to listen for incoming data on a socket stream,and also be able to notice a signal from the managing thread for special tasks (like close the socket, or whatever).
I have a solution that works. Here's some quick pseudo code:
socket.set_so_timeout(100milliseconds)
while (not interrupted by manager thread) {
try{
socket.receive
} catch (sockettimeoutexception) {
//do nothing here...just to break the block on receive
}
}
check and handle message that manager signalled to pickup...
But I'm not satisfied with this solution for several reasons:
1. I don't like the latency that this introduces to responding to the interrupt, up to 'x'milliseconds. I would like the thread to respond immediately.
2. I don't like throwing an exception every iteration; they're expensive
3. If I decrease the timeout, then I'm spending more CPU time looping/exception throwing, if I increase it, then the response time to the interrupt from the manager gets even more sluggish.
That's it in a nutshell. I'm hoping someone can offer some suggestions of a better way to implement this.
As an aside, I'm assuming sockets are thread safe? I've considered running two threads; one that always waits on messages from the manager thread, and the other that's always sitting on the socket receive. If I tried this then sometimes we may get simultaneous read and write on the socket, but I've heard that's ok...don't know for sure though.
Any insight greatly appreciated:)
- 08-30-2009, 03:56 AM #2
i would suggest looking into non-blocking sockets. Instead of having a thread per connection you can have a single thread looking at all sockets.
My Hobby Project: LegacyClone
- 08-30-2009, 09:51 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 3
- Rep Power
- 0
I'll look into it.
Hi,
Thanks for the suggestion. I haven't done anything w/ non-blocking sockets so I don't know much at this point, but I'm looking into it. Sounds like it might be just what I need.
- 09-05-2009, 02:20 AM #4
Non-blocking sockets is the way to go. The only difference is that your reads will often return 0 bytes, which means nothing was there. You will be responsible for making the thread sleep. Don't interrupt the thread, as this could break the socket connection if the interrupt happens at just the wrong time. Use a volatile flag(s) instead to send instructions. Since you control the loop, check the flag(s) each time around, along with doing a read.
- 09-07-2009, 10:01 AM #5
Member
- Join Date
- Aug 2009
- Posts
- 3
- Rep Power
- 0
Hi, thanks. What you're suggesting makes sense to me, and I've looked into non-blocking sockets and it seems like a good fit! I have a couple of questions: Currently I'm pushing objects across a stream. When using the objectinputstream It blocks until it receives the whole object and then returns the object, but w/ non-blocking sockets and using buffers, it looks like I need to handle the added complexity of identifying when I've received the entire object? As far as I can tell, the non-blocking receive will grab what's there, which isn't guaranteed to be the entire object, so I would need to have a temporary storage where I keep appending the buffer contents (as they come into the socket) until I find the end of the object, at which point I can take the object and use it, then empty the temporary storage. Is this sounding correct? Are there any other things I need to consider that I'm missing? What is the best way to identify when I've received the entire object?
- 09-08-2009, 04:47 PM #6
Similar Threads
-
append response to the request from Socket and write to another socket
By vaibhav_singh_vs@yahoo.co in forum NetworkingReplies: 3Last Post: 04-17-2009, 07:02 PM -
Using an EMG signal to create a mouse click
By cmc419 in forum New To JavaReplies: 1Last Post: 03-27-2009, 05:38 PM -
Java code that allows me to make and receive calls, send and receive sms
By nareshbabu@live.in in forum NetworkingReplies: 0Last Post: 12-02-2008, 10:55 AM -
WiFi signal strength
By islamfunny in forum CLDC and MIDPReplies: 1Last Post: 10-02-2008, 08:53 AM -
freezing when use socket.accept() inside of a thread
By tamayo in forum Advanced JavaReplies: 0Last Post: 07-23-2007, 11:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks