I have been attempting to develop an embedded webserver within an application that I have created. Essentially we have our main process that creates a simple webserver (utilizing a ServerSocket) and then the main process (in theory) will go about it’s business.
Main(){
doingStuff();
WebServer server = new WebServer();
server.run();
doingMoreStuff();
}
public class WebServer implements Runnable{
ServerSocket inbound;
//constructor
WebServer(){
inbound = new ServerSocket(9687);
}
public void Run(){
Socket client;
while(true){
client = inbound.accept();
SomeClass threadedClassThatHandlesClientMessage = new SomeClass(client);
someclass.run();
}
}
}
In the preceding psuedo-code, I know that the thread with the ServerSocket accept() call blocks, ceasing the WebServer class thread as well as the main thread. Is there anything that I am missing?
I know that I can create the two programs that can run in tandem as separate processes, but I was hoping to avoid any IPC and shoot for a single process with the data processing in one thread and a generic serversocket receiving data in a separate thread. While I’m still new to all this, I’m fairly certain that this is one of those simple things that I have overlooked, but I would greatly appreciate any assistance you could provide.
Calling
.run()on aRunnabledoesn’t make it run in a new thread, it runs it in the current thread.Did you mean to do
new Thread (server).start ()?