aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Constantly listening server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Constantly listening server" Watch "Constantly listening server" New topic
Author

Constantly listening server

Martin Tschammer
Greenhorn

Joined: Apr 20, 2005
Posts: 5
Hi folks.

Alright, my problem is, that i need some way to make a server listen for connections constantly, but still being able to do other stuff at the same time. You know, make the server listen in the background, while the main program is able to work on other things. Is there any way to realize this? And if not, how about letting the server listen for connections once per second or so? That would be possible with timers i guess, but i wouldn't call it the best solution. If anyone knows a way to make the server listen for connections constantly, without interrupting or stopping the main program, that would be great.

Thanks in advance,
M.T.
[ April 20, 2005: Message edited by: Martin Tschammer ]
Venkatraman Kandaswamy
Ranch Hand

Joined: Jul 07, 2004
Posts: 120
one word : threads


--Venkatraman<br />SCJP 1.4<br /><a href="http://kvrlogs.blogspot.com" target="_blank" rel="nofollow">blog</a>
Martin Tschammer
Greenhorn

Joined: Apr 20, 2005
Posts: 5
Yeah, tried that, but kinda didn't get it to work... I'm pretty new to threads, so go easy on me.. I tried implementing Runnable (am already extending JFRame), and letting the program listen in the run() method, started by a thread somewhere in the program... but it still locks up when i tell it to listen for new connections, like this:



I need the program to listen ALL the time, that's why i do the while(true) stuff.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You have two options: put ServerSocket.accept() loop into its own thread or use NIO (new I/O) from JDK 1.4+. The latter allows you to use the alternate select() form from C that doesn't block while listening for connections.

Have you explored Sun's Java tutorials? They have examples for both methods that should get you started. Then you can come here and ask questions (and post code) to handle issues that come up.
Martin Tschammer
Greenhorn

Joined: Apr 20, 2005
Posts: 5
Thanks for the answer. Heh, yeah, i looked at NIO yesterday (or rather today), but i was pretty tired, as the clock was 3 or 4 am. I will try to re-read the tutorials again later today, when i am more rested. You mentioned select()... i will check that out first
Thanks again, i'll be back later.
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Martin,

One tip: don't think about the code that is doing ServerSocket.accept() as your thread.... think about the code that is going to handle an incoming connection as a thread. So you define essentially a "request handler" class that implements Runnable, and the run() method should take the input from the socket and "handle" it.

Once you get something from your ServerSocket.accept(), you get the info you need from the input, and kick off a thread to handle that request. So the only time you are blocking is while you are setting up that thread, and then your main program can go back to listening for the next request with another call to accept().

Hope that sends you in the right direction.
-- Jon
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
So here's how small this part of your server can be:

The "executor" is the thread pool in JDK 5. In 1.4 I used to do:

new Thread ( new handler(...) ).start()


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Martin Tschammer
Greenhorn

Joined: Apr 20, 2005
Posts: 5
Alright, i got it working now, using NIO. I also tried the thread way, but NIO just seemed smoother imho. Thanks guys, see ya later.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Excellent! Could you post a little bit of it? I haven't tried NIO at all yet, but thought about replacing that little loop I showed just to learn it.
Martin Tschammer
Greenhorn

Joined: Apr 20, 2005
Posts: 5
Sorry for the late reply, i wasn't really online the past two days. About the code part, well, my code is a bit obfuscated, because i paired it with a timer, but i have some other, very well written and documented examples for you (also the ones i used to understand NIO):

http://java.sun.com/j2se/1.4.2/docs/guide/nio/example/index.html
especially http://java.sun.com/j2se/1.4.2/docs/guide/nio/example/TimeQuery.java
and
http://java.sun.com/j2se/1.4.2/docs/guide/nio/example/NBTimeServer.java

[ April 28, 2005: Message edited by: Martin Tschammer ]
[ April 28, 2005: Message edited by: Martin Tschammer ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Constantly listening server