wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Invoking socket.receive only when... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Invoking socket.receive only when..." Watch "Invoking socket.receive only when..." New topic
Author

Invoking socket.receive only when...

Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Hi,

I have a datagramsocket in a thread.
My confusion is this:

I want to only invoke it only when a packet is received.



Above snippet is inside a try catch block, but I just didn't show it.
The problem with the above snippet is that when this thread is run, and no packet is received it will just wait until a packet is received.
What I want is to only invoke the socket.receive(packet) if a packet is present, so the application can do other things.

Probably not put it in a thread? How then?




amerzil co-ed student<br />"Praise be the Code"
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
If I use setSoTimeout(x), this would still mean running the thread, and wait for x milliseconds then throws an exception if no packet is received.

I could then just have yield() in the catch block, and not close the socket.

But is there a way where it would just automatically call it if a packet is received?


Still confused.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8834
    
    7

The functionality you are looking for is in java.nio.channels.DatagramChannel. You can configure it to return immediately if there are no packets waiting to be read.
[ August 16, 2005: Message edited by: Joe Ess ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
I'd like to have some examples. Any links?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8834
    
    7

NIO is poorly documented, compared to the standard IO classes. Since it is not as widely used as standard IO, you won't get the same level of help in forums, either. If you are serious about using it, I recommend you study the existing documentation and buy a book.
If you don't want to risk it, create one listening thread per connection. Unless you have several thousand simultaneous connections you should be fine.
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
For now, that is exactly what I have. here's the pseudo-code:

Thread1:
Listen indefinitely until packet is received
Packet finally received, so process
yield so other thread can run


Thread2:
Listen indefinitely until packet is received
Packet finally received, so process
yield so other thread can run


The problem I see with the above "approach" is that it is not a good approach, for the resource to be tied up until a packet is received is such a waste.

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8834
    
    7

Originally posted by Karen Baog:

Thread1:
Listen indefinitely until packet is received
Packet finally received, so process
yield so other thread can run


You realize that since this code blocks on the call to receive(), that yield() doesn't get invoked until AFTER a packet is received? Being nice to other threads is a Good Thing, but since receive() blocks, another thread would get control immeditately if there were no data to be processed.

Originally posted by Karen Baog:

The problem I see with the above "approach" is that it is not a good approach, for the resource to be tied up until a packet is received is such a waste.

Like I said above, unless you have thousands of simultaneous connections, this won't be a problem. You could use a low-latency protocol where the client only connects when it has a request (HTTP works this way), to reduce the amount of time a client connection blocks a thread.
NIO's always an option. I haven't done much with it. There's an article on
Building Highly Scalable Servers with NIO but like I said before, if you run into trouble, there's not a lot of experience with NIO in the community.
dushantha Rathnayake
Ranch Hand

Joined: Feb 27, 2010
Posts: 103
HI,

I also new to NIO. I have to develop a host. in this case there can be 1000 connections in same time. So I tried with old ServerSocket but it can not handle it. So i tried with NIO. In this case the ServerSocketChannel get some time to give response. So i set the timeout in client side as 30000 milliseconds . But this not good. Because our system is a transport ticketing system. So customer can not wait 30 seconds as maximum in traffic hours. So are their any suggestions for do this? In initial step the system has 5000 terminals for issue tickets. My ServerSocketChannel code is just like the code inside of this link http://docs.oracle.com/javase/1.5.0/docs/guide/nio/example/NBTimeServer.java.

Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Invoking socket.receive only when...
 
Similar Threads
Datagram encoding help needed!
Communicate 2 android in 2 emulator
DNS query, protocol
Threading and collections problem.Pl help
Network assignment HELP NEEDED