MultiServer.java 986 Bytes
Newer Older
Michele Fiori's avatar
Michele Fiori committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package sums;

import java.io.*;
import java.net.*;

class MultiServer {
    public static void main(String argv[]) throws Exception {
        int portService;

        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Please, insert the service port number:");
        portService = Integer.parseInt(inFromUser.readLine());

        // create a "listening socket" on the specified port
        ServerSocket welcomeSocket = new ServerSocket(portService);

        while(true) {
            Socket connectionSocket = welcomeSocket.accept();
            System.out.print("Client address: " + connectionSocket.getInetAddress() + ", port: " + connectionSocket.getPort());

            // thread creation passing the established socket as arg
            ServerThread theThread = new ServerThread(connectionSocket);

            // start of the thread
            theThread.start();
        }
    }
}