Springer - An Introduction to Network Programming with Java_2

Page 79

Multithreading and Multiplexing

69

{ private static ServerSocket servSocket; private static final int PORT = 1234; public static void main(String[] args) throws IOException { try { servSocket = new ServerSocket(PORT); } catch (IOException e) { System.out.println("\nUnable to set up port!"); System.exit(1); } //Create a Resource object with //a starting resource level of 1... Resource item = new Resource(1); //Create //to the //thread Producer

a Producer thread, passing a reference Resource object as an argument to the constructor... producer = new Producer(item);

//Start the Producer thread running... producer.start(); do { //Wait for a client to make connection... Socket client = servSocket.accept(); System.out.println("\nNew client accepted.\n"); //Create a ClientThread thread to handle all //subsequent dialogue with the client, passing //references to both the client's socket and //the Resource object... ClientThread handler = new ClientThread(client,item); //Start the ClientThread thread running... handler.start(); }while (true); //Server will run indefinitely. } }


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.