View difference between Paste ID: jMEzLjx2 and Q2ANEKzQ
SHOW: | | - or go back to the newest paste.
1
/*Server.java*/
2-
import java.io.*;//importing io classes
2+
import java.io.*; //importing io classes
3-
import java.net.*;//importing networking classes
3+
import java.net.*; //importing networking classes
4
class Server {
5-
    private static Client clients[];//array storing clients
5+
    private static Client clients[]; //array storing clients
6-
	private static Client clientsTmp[];
6+
    private static Client clientsTmp[];
7-
	private static int noOfClients = 0;
7+
    private static int noOfClients = 0;
8-
	private static ServerSocket server;
8+
    private static ServerSocket server;
9-
	private static Socket client;
9+
    private static Socket client;
10-
	private static Sender sender;
10+
    private static Sender sender;
11-
	private static PrintWriter outStream;
11+
    private static PrintWriter outStream;
12-
	private static BufferedReader buffer;
12+
    private static BufferedReader buffer;
13-
	private static String keyboardInput;
13+
    private static String keyboardInput;
14
    public static void main(String argc[]) {
15-
		try {
15+
        try {
16-
		    server = new ServerSocket(2000);
16+
            server = new ServerSocket(2000);
17-
			sender = new Sender();
17+
            sender = new Sender();
18-
		    while(true) {
18+
            while (true) {
19-
				System.out.println("New client connected...\nClients connected - "+noOfClients);
19+
                System.out.println("New client connected...\nClients connected - " + noOfClients);
20
                client = server.accept();
21-
				addClient(client);
21+
                addClient(client);
22-
			}
22+
            }
23-
		} catch(Exception e) {
23+
        } catch (Exception e) {
24-
		    System.out.println("Could not create server..."+e);
24+
            System.out.println("Could not create server..." + e);
25-
			System.exit(-1);
25+
            System.exit(-1);
26-
		}
26+
        }
27-
	}
27+
    }
28-
	static void addClient(Socket client) {//adds new client
28+
    static void addClient(Socket client) { //adds new client
29-
	    try {
29+
        try {
30-
	        clientsTmp = new Client[++noOfClients];
30+
            clientsTmp = new Client[++noOfClients];
31-
			int i = 0;
31+
            int i = 0;
32-
			while(i<noOfClients - 1) {
32+
            while (i < noOfClients - 1) {
33-
		       clientsTmp[i] = clients[i];
33+
                clientsTmp[i] = clients[i];
34-
			   i++;
34+
                i++;
35-
			}
35+
            }
36-
		    clientsTmp[noOfClients - 1] = new Client(client);
36+
            clientsTmp[noOfClients - 1] = new Client(client);
37
            clients = clientsTmp.clone();
38-
        } catch(Exception e) {
38+
        } catch (Exception e) {
39-
		    System.out.println("Some problem occured " + e);
39+
            System.out.println("Some problem occured " + e);
40-
        }		
40+
        }
41-
	}
41+
    }
42-
	static class Client {
42+
    static class Client {
43-
	    Socket client;
43+
        Socket client;
44-
		PrintWriter outputStream;
44+
        PrintWriter outputStream;
45-
		Client(Socket clientSocket) {
45+
        Client(Socket clientSocket) {
46-
		    client = clientSocket;
46+
            client = clientSocket;
47-
			try {
47+
            try {
48-
			    outputStream = new PrintWriter(client.getOutputStream());
48+
                outputStream = new PrintWriter(client.getOutputStream());
49-
			} catch(Exception e) {
49+
            } catch (Exception e) {
50-
			    System.out.println("Could not initialise client output stream");
50+
                System.out.println("Could not initialise client output stream");
51-
			}
51+
            }
52-
		}
52+
        }
53-
		void print(String str) {
53+
        void print(String str) {
54-
		    try {
54+
            try {
55-
		        outputStream.println(str);
55+
                outputStream.println(str);
56-
		        outputStream.flush();
56+
                outputStream.flush();
57-
				System.out.println("Sent");
57+
                System.out.println("Sent");
58-
			} catch(Exception e) {
58+
            } catch (Exception e) {
59-
			     System.out.println("Could not send to client" + e);
59+
                System.out.println("Could not send to client" + e);
60-
			}
60+
            }
61-
			
61+
62-
		}
62+
        }
63-
		void close() {
63+
        void close() {
64-
		    try {
64+
            try {
65-
		        client.close();
65+
                client.close();
66-
			    outputStream.close();
66+
                outputStream.close();
67-
			} catch(Exception e) {
67+
            } catch (Exception e) {
68-
			    System.out.println("Could not close client");
68+
                System.out.println("Could not close client");
69-
			}
69+
            }
70-
		}
70+
        }
71-
	}
71+
    }
72-
	static class Sender extends Thread{
72+
    static class Sender extends Thread {
73-
	    Sender() {
73+
        Sender() {
74-
		     super("Sender Thread");
74+
            super("Sender Thread");
75-
			 try {
75+
            try {
76-
			     buffer = new BufferedReader(new InputStreamReader(System.in));
76+
                buffer = new BufferedReader(new InputStreamReader(System. in ));
77-
				 System.out.println("Starting broadcast...");
77+
                System.out.println("Starting broadcast...");
78-
				 start();
78+
                start();
79-
			 } catch(Exception e) {
79+
            } catch (Exception e) {
80-
			    System.out.println("Could not start sender thread");
80+
                System.out.println("Could not start sender thread");
81-
			    System.exit(-1);
81+
                System.exit(-1);
82-
			 }
82+
            }
83-
		}
83+
        }
84-
		public void run() {
84+
        public void run() {
85-
		    try {
85+
            try {
86-
			    int i = 0;
86+
                int i = 0;
87-
			    while(!(keyboardInput = buffer.readLine()).equals("stop")) {
87+
                while (!(keyboardInput = buffer.readLine()).equals("stop")) {
88-
				    while(i < noOfClients) {
88+
                    while (i < noOfClients) {
89-
					    System.out.println("Sending to client "+i);
89+
                        System.out.println("Sending to client " + i);
90-
					    clients[i++].print(keyboardInput);
90+
                        clients[i++].print(keyboardInput);
91-
                    }	
91+
                    }
92-
                    System.out.println("Packets containing "+ keyboardInput +"sent.Waiting for next broadcast...");					
92+
                    System.out.println("Packets containing " + keyboardInput + "sent.Waiting for next broadcast...");
93-
				}
93+
                }
94-
			} catch(Exception e) {
94+
            } catch (Exception e) {
95-
			     System.out.println("Could not send");
95+
                System.out.println("Could not send");
96-
				 System.exit(-1);
96+
                System.exit(-1);
97-
			}
97+
            }
98-
		}
98+
        }
99-
	}
99+
    }
100
}