Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.net.*;
- import java.io.*;
- public class ChatServerThread extends Thread
- {
- private ChatServer server = null;
- private Socket socket = null;
- private int ID = -1;
- private BufferedReader streamIn = null;
- private PrintWriter streamOut = null;
- public ChatServerThread(ChatServer _server, Socket _socket)
- {
- super();
- server = _server;
- socket = _socket;
- ID = socket.getPort();
- }
- public void send(String msg)
- {
- streamOut.println(msg);
- }
- public int getID()
- {
- return ID;
- }
- @Override
- public void run()
- {
- System.out.println("Server Thread " + ID + " running.");
- while (true && !this.isInterrupted())
- {
- try
- {
- server.handle(ID, streamIn.readLine());
- } catch (IOException ioe)
- {
- System.out.println(ID + " ERROR reading: " + ioe.getMessage());
- server.remove(ID);
- this.interrupt();
- }
- }
- }
- public void open() throws IOException
- {
- streamIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- streamOut = new PrintWriter(socket.getOutputStream(), true);
- }
- public void close() throws IOException
- {
- if (socket != null)
- {
- socket.close();
- }
- if (streamIn != null)
- {
- streamIn.close();
- }
- if (streamOut != null)
- {
- streamOut.close();
- }
- }
- }
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.net.*;
- import java.io.*;
- public class ChatServer implements Runnable
- {
- private ChatServerThread clients[] = new ChatServerThread[50];
- private ServerSocket server = null;
- private Thread thread = null;
- private int clientCount = 0;
- public ChatServer(int port)
- {
- try
- {
- System.out.println("Binding to port " + port + ", please wait ...");
- server = new ServerSocket(port);
- System.out.println("Server started: " + server);
- start();
- } catch (IOException ioe)
- {
- System.out.println("Can not bind to port " + port + ": " + ioe.getMessage());
- }
- }
- @Override
- public void run()
- {
- while (thread != null && !thread.isInterrupted())
- {
- try
- {
- System.out.println("Waiting for a client ...");
- addThread(server.accept());
- } catch (IOException ioe)
- {
- System.out.println("Server accept error: " + ioe);
- stop();
- }
- }
- }
- private int findClient(int ID)
- {
- for (int i = 0; i < clientCount; i++)
- {
- if (clients[i].getID() == ID)
- {
- return i;
- }
- }
- return -1;
- }
- public synchronized void handle(int ID, String input)
- {
- if (input.equals(".bye"))
- {
- clients[findClient(ID)].send(".bye");
- remove(ID);
- } else
- {
- for (int i = 0; i < clientCount; i++)
- {
- clients[i].send(ID + ": " + input);
- }
- }
- }
- public synchronized void remove(int ID)
- {
- int pos = findClient(ID);
- if (pos >= 0)
- {
- ChatServerThread toTerminate = clients[pos];
- System.out.println("Removing client thread " + ID + " at " + pos);
- if (pos < clientCount - 1)
- {
- for (int i = pos + 1; i < clientCount; i++)
- {
- clients[i - 1] = clients[i];
- }
- }
- clientCount--;
- try
- {
- toTerminate.close();
- } catch (IOException ioe)
- {
- System.out.println("Error closing thread: " + ioe);
- }
- toTerminate.interrupt();
- }
- }
- private void addThread(Socket socket)
- {
- if (clientCount < clients.length)
- {
- System.out.println("Client accepted: " + socket);
- clients[clientCount] = new ChatServerThread(this, socket);
- try
- {
- clients[clientCount].open();
- clients[clientCount].start();
- clientCount++;
- } catch (IOException ioe)
- {
- System.out.println("Error opening thread: " + ioe);
- }
- } else
- {
- System.out.println("Client refused: maximum " + clients.length + " reached.");
- }
- }
- public void start()
- {
- if (thread == null)
- {
- thread = new Thread(this);
- thread.start();
- }
- }
- public void stop()
- {
- if (thread != null)
- {
- thread.interrupt();
- thread = null;
- }
- }
- public static void main(String args[])
- {
- ChatServer server = null;
- server = new ChatServer(9988);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment