/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package containing; import java.io.DataInputStream; import java.io.PrintStream; import java.io.IOException; import java.net.Socket; import java.net.ServerSocket; /* * A chat server that delivers public and private messages. */ public class EchoServer { // The server socket. private static ServerSocket serverSocket = null; // The client socket. private static Socket clientSocket = null; // This chat server can accept up to maxClientsCount clients' connections. private static final int maxClientsCount = 3; private static final clientThread[] threads = new clientThread[maxClientsCount]; public static void main(String args[]) { //public EchoServer(){ // The default port number. int portNumber = 2222; if (args.length < 1) { //if (true) { System.out.println("Server opened on Port <" + portNumber + ">"); } else { portNumber = Integer.valueOf(args[0]).intValue(); } /* * Open a server socket on the portNumber (default 2222). Note that we can * not choose a port less than 1023 if we are not privileged users (root). */ try { serverSocket = new ServerSocket(portNumber); } catch (IOException e) { System.out.println(e); } /* * Create a client socket for each connection and pass it to a new client * thread. */ while (true) { try { clientSocket = serverSocket.accept(); int i = 0; for (i = 0; i < maxClientsCount; i++) { if (threads[i] == null) { (threads[i] = new clientThread(clientSocket, threads)).start(); break; } } if (i == maxClientsCount) { PrintStream os = new PrintStream(clientSocket.getOutputStream()); os.println("Error Too many Connections!!"); os.close(); clientSocket.close(); } } catch (IOException e) { System.out.println(e); } } } } /* * The chat client thread. This client thread opens the input and the output * streams for a particular client, ask the client's name, informs all the * clients connected to the server about the fact that a new client has joined * the chat room, and as long as it receive data, echos that data back to all * other clients. When a client leaves the chat room this thread informs also * all the clients about that and terminates. */ class clientThread extends Thread { private DataInputStream is = null; private PrintStream os = null; private Socket clientSocket = null; private final clientThread[] threads; private int maxClientsCount; public clientThread(Socket clientSocket, clientThread[] threads) { this.clientSocket = clientSocket; this.threads = threads; maxClientsCount = threads.length; //System.out.println("Constructor zegt JAH!!"); } @Override public void run() { int maxClientsCount = this.maxClientsCount; clientThread[] threads = this.threads; //System.out.println("JA DOET HET"); try { /* * Create input and output streams for this client. */ is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); //os.println("Enter your name."); String name = is.readLine().trim(); os.println("Connection Succesfull " + name); for (int i = 0; i < maxClientsCount; i++) { if (threads[i] != null && threads[i] != this) { threads[i].os.println("Connection with" + name + " established"); } } while (true) { String line = is.readLine(); if (line.startsWith("/quit")) { System.exit(1); break; } for (int i = 0; i < maxClientsCount; i++) { if (threads[i] != null) { threads[i].os.println("<" + name + ">" + line); } } } for (int i = 0; i < maxClientsCount; i++) { if (threads[i] != null && threads[i] != this) { threads[i].os.println("Connection Established with: " + name); } } os.println("Connection Lost with: " + name); /* * Clean up. Set the current thread variable to null so that a new client * could be accepted by the server. */ for (int i = 0; i < maxClientsCount; i++) { if (threads[i] == this) { threads[i] = null; } } /* * Close the output stream, close the input stream, close the socket. */ is.close(); os.close(); clientSocket.close(); } catch (IOException e) { } } }