Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Client</strong> </p> <pre><code>public class TCPServer { //tcp port on local host port public static final int PORT = 3100; public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { //server socket, can also specify Host Address serverSocket = new ServerSocket(PORT); //start listening on port System.out.println("Listening for clients on port: " + PORT); } catch (IOException e) { System.err.println("Could not listen on port: " + PORT); System.err.println(e.getMessage()); System.exit(-1); } //create new thread pool ThreadPool threadPool = new ThreadPool(2); //call runnable method on thread pool threadPool.runTask(startServer(serverSocket)); //join thread pool threadPool.join(); //close server socket and destroy threadpool serverSocket.close(); threadPool.destroy(); } private static Runnable startServer(final ServerSocket socket) { return new Runnable() { @Override public void run() { //keep looping and looking for data while (true) try { //create new thread new TCPServerThread(socket.accept()).start(); } catch (IOException e) { System.out.println("Client got disconnected!" + "\nListening for clients on port: " + PORT); } } }; } } </code></pre> <p><strong>Server</strong></p> <pre><code>import java.io.BufferedInputStream; import java.io.IOException; import java.net.Socket; import javazoom.jl.decoder.JavaLayerException; import javazoom.jl.player.Player; public class TCPServerThread extends Thread { private Socket socket = null; //constructor public TCPServerThread(Socket socket) { this.socket = socket; } public void run() { try { //read data into buffered stream BufferedInputStream stream = new BufferedInputStream( socket.getInputStream()); //create music player object with buffered stream Player p = new Player(stream); //start playing p.play(); //close socket after done playing socket.close(); } catch (IOException e) { System.out.println("Client got disconnected!" + "\nListening for clients on port: " + TCPServer.PORT); } catch (JavaLayerException e) { System.out.println("Client got disconnected!" + "\nListening for clients on port: " + TCPServer.PORT); } } } </code></pre> <p><strong>Thread Pool</strong></p> <pre><code>import java.util.LinkedList; class ThreadPool extends ThreadGroup { private boolean isAlive; private LinkedList&lt;Runnable&gt; taskQueue; private int threadID; private static int threadPoolID; //constructor public ThreadPool(int numThreads) { super("ThreadPool-" + (threadPoolID++)); // Changes the daemon status of this thread group. setDaemon(true); isAlive = true; taskQueue = new LinkedList&lt;Runnable&gt;(); for (int i = 0; i &lt; numThreads; i++) { new PooledThread().start(); } } public synchronized void runTask(Runnable task) { if (!isAlive) { throw new IllegalStateException(); } if (task != null) { taskQueue.add(task); notify(); } } protected synchronized Runnable getTask() throws InterruptedException { while (taskQueue.size() == 0) { if (!isAlive) { return null; } wait(); } return (Runnable) taskQueue.removeFirst(); } public synchronized void close() { if (isAlive) { isAlive = false; taskQueue.clear(); interrupt(); } } public void join() { // notify all waiting threads that this ThreadPool is no // longer alive synchronized (this) { isAlive = false; notifyAll(); } // wait for all threads to finish Thread[] threads = new Thread[activeCount()]; int count = enumerate(threads); for (int i = 0; i &lt; count; i++) { try { threads[i].join(); } catch (InterruptedException ex) { } } } private class PooledThread extends Thread { public PooledThread() { super(ThreadPool.this, "PooledThread-" + (threadID++)); } public void run() { while (!isInterrupted()) { // get a task to run Runnable task = null; try { task = getTask(); } catch (InterruptedException ex) { } // if getTask() returned null or was interrupted, // close this thread by returning. if (task == null) { return; } // run the task, and eat any exceptions it throws try { task.run(); } catch (Throwable t) { uncaughtException(this, t); } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload