Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constantly listening server

 
Martin Tschammer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one word : threads
 
Martin Tschammer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
Martin Tschammer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic