Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package chatServer;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- //import java.util.Queue;
- /**
- * @author Patch
- *
- */
- public class ChatServer extends Thread{
- private int id = -1;
- private static int port = 5689;
- private static boolean debug;
- private Socket clientSocket = null;
- private static ServerSocket serverSocket = null;
- private static final int MAX_CONNECTIONS = 10000;
- private static ChatServer[] serverThread = new ChatServer[MAX_CONNECTIONS];
- //TODO: private [] freeSlots;
- //private static Queue serverThreads = null; - An other possibility..
- private static int clientCount = 0;
- private BufferedReader in = null;
- private PrintWriter out = null;
- public ChatServer(Socket client, boolean debug, int port, int id) {
- this.clientSocket = client;
- this.debug = debug;
- this.port = port;
- this.id = id;
- }
- public void run() {
- System.out.println("Starting Server Thread!");
- try {
- this.in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- this.out = new PrintWriter(clientSocket.getOutputStream(),true);
- String inputLine;
- while ((inputLine = in.readLine()) != null) {
- if (debug) {
- System.out.println("Debug: " + inputLine);
- }
- synchronized (serverThread) {
- for(int i=0; i < clientCount; i++) {
- if(serverThread[i] != null && serverThread[i].out != null && serverThread[i]!= this) {
- serverThread[i].out.println(inputLine);
- }
- }
- }
- }
- synchronized(this) {
- in.close();
- out.close();
- clientSocket.close();
- serverThread[this.id] = null;
- System.out.println("Client Disconnected");
- }
- }
- catch (IOException e) {
- System.err.print("Unbekannter Fehler: Kein Fehler!");
- e.printStackTrace();
- }
- }
- private static void parseArgs(String[] args) {
- switch (args.length) {
- case 0: debug = false; port = 5678; break;
- case 1: if (args[0].equalsIgnoreCase("-debug"))
- {debug = true; port = 5678;}
- if (args[0].matches("[-+]?\\d*\\.?\\d+") && 1023 <= Integer.parseInt(args[0]) && Integer.parseInt(args[0]) <= 49151)
- {port = Integer.parseInt(args[0]);} break;
- case 2: if(args[0].equalsIgnoreCase("-debug"))
- {debug = true;}
- if(1023 <= Integer.parseInt(args[1]) && Integer.parseInt(args[1]) <= 49151)
- {port = Integer.parseInt(args[1]);}break;
- default: System.err.println("Usage: java EchoServer [-debug][<port>]"); System.exit(1); break;
- }
- }
- private static void send(Socket client,String msg) throws IOException {
- PrintWriter out = new PrintWriter(client.getOutputStream());
- out.println(msg);
- out.close();
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- parseArgs(args);
- try {serverSocket = new ServerSocket(port);
- System.out.println("Server running on port " + port +". Debug: " + debug);
- while (true) {
- System.out.println("Wait for client connection to accept. (Connected Clients: " + clientCount + ")");
- Socket client = null;
- //synchronized (serverThread[clientCount]) {
- if(clientCount < MAX_CONNECTIONS) {
- client = serverSocket.accept(); //TODO
- System.out.println("Client accepted");
- synchronized(serverThread) {
- (serverThread[clientCount] = new ChatServer(client, debug, port, clientCount)).start();
- clientCount++;
- }
- }
- else {
- System.err.println("Maximum Connection reached!");
- send(client,"Server buys!");
- }
- }
- //}
- }
- catch (IOException e) {
- System.out.println("Exception caught when trying to listen on port "
- + port + " or listening for a connection");
- System.err.println(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement