Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class Server
- {
- private static int cid;
- private ArrayList<ClientThread> ct;
- private GUIForServer gfs;
- private int port;
- private boolean runForever;
- public Server(int port)
- {
- this(port, null);
- }
- public Server(int port, GUIForServer gfs)
- {
- this.gfs = gfs;
- this.port = port;
- ct = new ArrayList<ClientThread>();
- }
- public void start()
- {
- runForever = true;
- try
- {
- ServerSocket ss = new ServerSocket(port);
- while(runForever)
- {
- displayMessage("Server waiting for clients on port " + port + ".");
- Socket s = ss.accept();
- if(!runForever)
- {
- break;
- }
- ClientThread t1 = new ClientThread(s);
- ct.add(t1);
- t1.start();
- }
- try
- {
- ss.close();
- for(int i = 0; i < ct.size(); i++)
- {
- ClientThread cli = ct.get(i);
- try
- {
- cli.ois.close();
- cli.oos.close();
- cli.s.close();
- }
- catch(IOException ioe)
- {
- }
- }
- }
- catch(Exception e)
- {
- displayMessage("Exception closing the server and client(s): " + e);
- }
- }
- catch(IOException ioe)
- {
- String msg = ("Exception on the new server socket: " + ioe + "\n");
- displayMessage(msg);
- }
- }
- protected void stop()
- {
- runForever = false;
- try
- {
- new Socket("localhost", port);
- }
- catch(Exception e)
- {
- }
- }
- private void displayMessage(String msg)
- {
- String message = msg;
- if(gfs == null)
- {
- System.out.println(msg);
- }
- else
- {
- gfs.eventTextArea(message + "\n");
- }
- }
- private synchronized void broadcastToAll(String message)
- {
- String newMessage = message + "\n";
- if(gfs == null)
- {
- System.out.print(newMessage);
- }
- else
- {
- gfs.chatTextArea(newMessage);
- }
- for(int i = ct.size(); --i >= 0;)
- {
- ClientThread cthr = ct.get(i);
- if(!cthr.writeMessage(newMessage))
- {
- ct.remove(i);
- displayMessage("The client that disconnected: " + cthr.username + " has been removed from the list of clients that connected.");
- }
- }
- }
- private synchronized void removeClient(int id)
- {
- for (int i = 0; i < ct.size(); i++) {
- ClientThread cthr = ct.get(i);
- if(cthr.id == id)
- {
- ct.remove(i);
- return;
- }
- }
- }
- public static void main (String[] args)
- {
- int portNum = 7257;
- switch(args.length)
- {
- case 0:
- break;
- case 1:
- try
- {
- portNum = Integer.parseInt(args[0]);
- }
- catch(Exception e)
- {
- System.out.println("Invalid port number.");
- System.out.println("Usage of this program is as follows: -> java Server (portNum)");
- return;
- }
- default:
- System.out.println("Usage of this program is as follows: -> java Server (portNum)");
- return;
- }
- Server server = new Server(portNum);
- server.start();
- }
- public class ClientThread extends Thread
- {
- Socket s;
- ObjectInputStream ois;
- ObjectOutputStream oos;
- int id;
- String username;
- messageType mt;
- public ClientThread(Socket s)
- {
- id = cid++;
- this.s = s;
- System.out.println("Thread is trying to create object Input or Output streams...");
- try
- {
- oos = new ObjectOutputStream(s.getOutputStream());
- ois = new ObjectInputStream(s.getInputStream());
- username = (String) ois.readObject();
- displayMessage(username + " has connected.");
- }
- catch(IOException ioe)
- {
- displayMessage("Exception creating new input or output streams: " + ioe);
- return;
- }
- catch(ClassNotFoundException cnfe)
- {
- }
- }
- public void run()
- {
- boolean runForever = true;
- while(runForever)
- {
- try
- {
- mt = (messageType) ois.readObject();
- }
- catch(IOException ioe)
- {
- displayMessage(username + " has an exception reading streams: " + ioe);
- break;
- }
- catch(ClassNotFoundException cnfe)
- {
- break;
- }
- String message = mt.getMessage();
- switch(mt.getType())
- {
- case messageType.MESSAGE:
- broadcastToAll(username + "'s message: " + message);
- break;
- case messageType.EXIT:
- displayMessage(username + "exited by typing EXIT.");
- runForever = false;
- break;
- }
- }
- removeClient(id);
- close();
- }
- public void close()
- {
- try
- {
- if(oos != null)
- {
- oos.close();
- }
- }
- catch(Exception e)
- {
- }
- try
- {
- if(ois != null)
- {
- ois.close();
- }
- }
- catch(Exception e)
- {
- }
- try
- {
- if(s != null)
- {
- s.close();
- }
- }
- catch(Exception e)
- {
- }
- }
- private boolean writeMessage(String msg)
- {
- if(!s.isConnected())
- {
- close();
- return false;
- }
- try
- {
- oos.writeObject(msg);
- }
- catch(IOException ioe)
- {
- displayMessage("Error sending message to " + username + "!");
- displayMessage(ioe.toString());
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement