Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Server Socket from a servlet

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,
I want to start a server( using serversocket) from init() method of a servlet. But the problem is this call(to accept) blocks and the init method never completes.
If I make the the class creating the server itself as a thread then the int method throws null pointer exception
e.g
========================
public class XServlet extends HttpServlet{
public void init()
{
new AServer().start();
}

}
==========================
public class AServer extends Thread{
public void run() {
ServerSocket serverSocket = null;
boolean serverON = true;
try {
serverSocket = new ServerSocket(8000);
} catch (IOException e) {
System.out.println("Could not listen on port: " + listenerPort);
e.printStackTrace();
}
while (serverON) {
System.out.println("Server waiting for document on Port: 8000");
try {
System.out.println("Blocking for a client Request");
Socket client = serverSocket.accept();
System.out.println("Accepted a client Request:" + client);
DocumentServerThread clientThread =
new DocumentServerThread(client);
clientThread.start();
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
serverSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Can anybody tell me a possible solution??
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This duplicates a post in the servlet topic.
If you want help around here - post in the most appropriate place only.
Bill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic