Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Server.java*/
- import java.io.*; //importing io classes
- import java.net.*; //importing networking classes
- class Server {
- private static Client clients[]; //array storing clients
- private static Client clientsTmp[];
- private static int noOfClients = 0;
- private static ServerSocket server;
- private static Socket client;
- private static Sender sender;
- private static PrintWriter outStream;
- private static BufferedReader buffer;
- private static String keyboardInput;
- public static void main(String argc[]) {
- try {
- server = new ServerSocket(2000);
- sender = new Sender();
- while (true) {
- System.out.println("New client connected...\nClients connected - " + noOfClients);
- client = server.accept();
- addClient(client);
- }
- } catch (Exception e) {
- System.out.println("Could not create server..." + e);
- System.exit(-1);
- }
- }
- static void addClient(Socket client) { //adds new client
- try {
- clientsTmp = new Client[++noOfClients];
- int i = 0;
- while (i < noOfClients - 1) {
- clientsTmp[i] = clients[i];
- i++;
- }
- clientsTmp[noOfClients - 1] = new Client(client);
- clients = clientsTmp.clone();
- } catch (Exception e) {
- System.out.println("Some problem occured " + e);
- }
- }
- static class Client {
- Socket client;
- PrintWriter outputStream;
- Client(Socket clientSocket) {
- client = clientSocket;
- try {
- outputStream = new PrintWriter(client.getOutputStream());
- } catch (Exception e) {
- System.out.println("Could not initialise client output stream");
- }
- }
- void print(String str) {
- try {
- outputStream.println(str);
- outputStream.flush();
- System.out.println("Sent");
- } catch (Exception e) {
- System.out.println("Could not send to client" + e);
- }
- }
- void close() {
- try {
- client.close();
- outputStream.close();
- } catch (Exception e) {
- System.out.println("Could not close client");
- }
- }
- }
- static class Sender extends Thread {
- Sender() {
- super("Sender Thread");
- try {
- buffer = new BufferedReader(new InputStreamReader(System. in ));
- System.out.println("Starting broadcast...");
- start();
- } catch (Exception e) {
- System.out.println("Could not start sender thread");
- System.exit(-1);
- }
- }
- public void run() {
- try {
- int i = 0;
- while (!(keyboardInput = buffer.readLine()).equals("stop")) {
- while (i < noOfClients) {
- System.out.println("Sending to client " + i);
- clients[i++].print(keyboardInput);
- }
- System.out.println("Packets containing " + keyboardInput + "sent.Waiting for next broadcast...");
- }
- } catch (Exception e) {
- System.out.println("Could not send");
- System.exit(-1);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement