Manage a pool of threads for clients : Server « Network Protocol « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Network Protocol » ServerScreenshots 
Manage a pool of threads for clients

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class PoolServerBase extends Thread {
  protected Socket socket;

  static PoolServerBase head = null;

  protected PoolServerBase next = null;

  static protected boolean init(Class clobj, int threads) {
    try {
      for (int i = 0; i < threads; i++) {
        PoolServerBase thread = (PoolServerBaseclobj.newInstance();
        thread.next = head;
        head = thread;
        thread.start();
      }
    catch (Exception e) {
      return false;
    }
    return true;
  }

  static synchronized protected void addToList(PoolServerBase me) {
    me.next = head;
    head = me;
  }

  synchronized protected void waitForSignal() {
    try {
      wait();
    catch (InterruptedException e) {
    }
  }

  public void run() {
    while (true) {
      waitForSignal();
      doClientProcessing();
      addToList(this);
    }
  }

  synchronized protected void handleClient(Socket s) {
    socket = s;
    notify();
  }

  protected void doClientProcessing() {
    try {
      String s = "Server.";
      s += "Thread: " this.toString();
      socket.getOutputStream().write(s.getBytes());
      sleep(10000)// simulate processing
      s = "Complete";
      socket.getOutputStream().write(s.getBytes());
      socket.close();
    catch (Exception e) {
      System.out.println(e);
    }
  }

  static synchronized protected boolean listEmpty() {
    return head == null;
  }

  static protected void assignThread(Socket sock) {
    PoolServerBase t = head;
    head = head.next;
    t.socket = sock;
    synchronized (t) {
      t.notify();
    }
  }

  static public void startServer(int port) {
    ServerSocket ssock;
    Socket sock;
    try {
      ssock = new ServerSocket(port);
      while (true) {
        Socket esock = null;
        try {
          esock = ssock.accept();
          while (listEmpty())
            yield();
          assignThread(esock);
        catch (Exception e) {
          try {
            esock.close();
          catch (Exception ec) {
          }
        }
      }
    catch (IOException e) {
    }
  }

  static public void main(String args[]) {
    init(PoolServerBase.class, 3);
    System.out.println("Starting server on port 808");
    startServer(808);
  }
}
           
       
Related examples in the same category
1. A generic framework for a flexible, multi-threaded server
2. Server allows connections on socket 6123Server allows connections on socket 6123
3. This server displays messages to a single clientThis server displays messages to a single client
4. The client can specify information to control the output of a serverThe client can specify information to control the output of a server
5. A server can use specialized streams to deliver typed dataA server can use specialized streams to deliver typed data
6. Serve entire objects using ObjectOutputStreamServe entire objects using ObjectOutputStream
7. A multithreaded serverA multithreaded server
8. Base class to build multithreaded servers easily
9. Client estimates the speed of the network connection to the server
10. Socket Openeration Test
11. URL Connection Test URL Connection Test
12. This server retrieves the time using the RFC867 protocol.This server retrieves the time using the RFC867 protocol.
13. Quote Server
14. Client and Server Demo
15. Reflector
w___w_w.__j___a_v_a__2__s._com__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.