Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 27th, 2012  |  syntax: Java  |  size: 3.86 KB  |  views: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class Server extends Thread{
  6.     private ServerSocket server = null;
  7.     private LinkedList<ServerThread> clients = new LinkedList<ServerThread>();
  8.  
  9.     public Server(int port) throws IOException {
  10.         server = new ServerSocket(port);
  11.     }
  12.  
  13.     public void run(){
  14.         while(true){
  15.             try{
  16.                 addThread(server.accept());
  17.             } catch (IOException e){
  18.                 e.printStackTrace();
  19.             }
  20.         }
  21.     }
  22.  
  23.     private void addThread(Socket socket) throws IOException {
  24.         ServerThread serverThread = new ServerThread(this, socket);
  25.         clients.add(serverThread);
  26.         clients.getLast().start();
  27.     }
  28.  
  29.     public synchronized void handle(String input) throws IOException {
  30.         for(ServerThread ss : clients){
  31.             ss.send(input);    
  32.         }
  33.     }  
  34.  
  35.     public static void main(String[] args){
  36.         try{
  37.             Server myServer = new Server(8080);
  38.             myServer.start();
  39.         } catch (IOException e){
  40.             e.printStackTrace();
  41.         }
  42.     }
  43. }
  44.  
  45.  
  46. import java.net.*;
  47. import java.io.*;
  48.  
  49. public class ServerThread extends Thread{
  50.     private Server server = null;
  51.     private Socket socket = null;
  52.     private DataInputStream in = null;
  53.     private DataOutputStream out = null;
  54.  
  55.     public ServerThread(Server server, Socket socket) throws IOException {
  56.         this.server = server;
  57.         this.socket = socket;
  58.         in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
  59.         out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
  60.     }
  61.  
  62.     public void run(){
  63.         while(true){
  64.             try {
  65.                 server.handle(in.readUTF());
  66.             } catch (IOException e){
  67.                 e.printStackTrace();
  68.             }
  69.         }
  70.     }
  71.  
  72.     public void send(String message) throws IOException {
  73.         out.writeUTF(message);
  74.         out.flush();    
  75.     }
  76.  
  77. }
  78.  
  79.  
  80. import java.net.*;
  81. import java.io.*;
  82.  
  83. public class Client extends Thread{
  84.     private Socket socket = null;
  85.     private DataInputStream in = null;
  86.     private DataOutputStream out = null;
  87.     private ClientThread client = null;
  88.  
  89.     public Client(String serverAddress, int serverPort) throws UnknownHostException, IOException {
  90.         socket = new Socket(serverAddress, serverPort);
  91.         in = new DataInputStream(System.in);
  92.         out = new DataOutputStream(socket.getOutputStream());
  93.         client = new ClientThread(this, socket);
  94.     }
  95.  
  96.     public void run(){
  97.         while(true){
  98.             try{
  99.                 out.writeUTF(in.readLine());
  100.                 out.flush();
  101.             } catch (IOException e){
  102.                 e.printStackTrace();
  103.             }
  104.         }
  105.     }
  106.  
  107.     public void handle(String message){
  108.         System.out.println(message);
  109.     }
  110.  
  111.     public static void main(String[] args) {
  112.         try{
  113.             Client myClient = new Client("ernie.icslab.agh.edu.pl", 8080);
  114.             myClient.start();
  115.         } catch (UnknownHostException e){
  116.             e.printStackTrace();
  117.         } catch (IOException e){
  118.             e.printStackTrace();
  119.         }
  120.     }
  121. }
  122.  
  123.  
  124. import java.net.*;
  125. import java.io.*;
  126.  
  127. public class ClientThread extends Thread{
  128.     private Socket socket = null;
  129.     private Client client = null;
  130.     private DataInputStream in = null;
  131.  
  132.     public ClientThread(Client client, Socket socket) throws IOException {
  133.         this.client = client;
  134.         this.socket = socket;
  135.         in = new DataInputStream(socket.getInputStream());
  136.         start();
  137.     }
  138.  
  139.     public void run(){
  140.         while(true){
  141.             try{
  142.                 client.handle(in.readUTF());
  143.             } catch (IOException e){
  144.                 e.printStackTrace();
  145.             }
  146.         }
  147.     }
  148. }