ServerSockets and Threads

Hello,

I am writing a program that is executed from the console. When executed, it opens a server socket and binds to a port. I then use a client socket to accept any incoming connections, when it does this, the entire program pauses and the command line is unusable as it is waiting for connections.

I want to be able write the program so that the listening takes place in the background and the rest of the program is still operable, if that's possible.

I thought I would be able to solve this problem with a simple thread. I ran a thread and put the accept() method in the threads run() method, but this yields the same result as above.

If anyone could explain to me what I need to do to finish my program, I would be very grateful. I would rather not see any code as I am learning and a solution theory would benefit me more.

Thanks, Daniel.

[883 byte] By [Daniel_1982a] at [2007-10-3 3:47:37]
# 1
You should read more about threads - never use run() function by youself. JVM itself will invoke this function after you call to start() function.
Michael.Nazarov@sun.coma at 2007-7-14 21:44:35 > top of Java-index,Core,Core APIs...
# 2
It seems that the op wants the command line to return and accept more commands once he start the server before the server application completes.In windows you can do this by giving the start command making the program start in a new consoleSTART java YourClass
LRMKa at 2007-7-14 21:44:35 > top of Java-index,Core,Core APIs...
# 3
No, he wants the rest of his application to keep running.OP you probably need to call Thread.start() instead of Thread.run().
ejpa at 2007-7-14 21:44:35 > top of Java-index,Core,Core APIs...
# 4
Hmm........I may be wrong... I think the OP is asking for a better way to listen for new connections without freezing his app. Try selectNow() or select(1000) to return from the select call with or without connections. Hopefully one of us answered your question right.Mike
Tigerfanga at 2007-7-14 21:44:35 > top of Java-index,Core,Core APIs...
# 5
or start a new thread for every accepted socket.
ejpa at 2007-7-14 21:44:35 > top of Java-index,Core,Core APIs...