import java.net.*; import java.io.*; import java.util.*; public class Server extends Thread{ private ServerSocket server = null; private LinkedList clients = new LinkedList(); public Server(int port) throws IOException { server = new ServerSocket(port); } public void run(){ while(true){ try{ addThread(server.accept()); } catch (IOException e){ e.printStackTrace(); } } } private void addThread(Socket socket) throws IOException { ServerThread serverThread = new ServerThread(this, socket); clients.add(serverThread); clients.getLast().start(); } public synchronized void handle(String input) throws IOException { for(ServerThread ss : clients){ ss.send(input); } } public static void main(String[] args){ try{ Server myServer = new Server(8080); myServer.start(); } catch (IOException e){ e.printStackTrace(); } } } import java.net.*; import java.io.*; public class ServerThread extends Thread{ private Server server = null; private Socket socket = null; private DataInputStream in = null; private DataOutputStream out = null; public ServerThread(Server server, Socket socket) throws IOException { this.server = server; this.socket = socket; in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); } public void run(){ while(true){ try { server.handle(in.readUTF()); } catch (IOException e){ e.printStackTrace(); } } } public void send(String message) throws IOException { out.writeUTF(message); out.flush(); } } import java.net.*; import java.io.*; public class Client extends Thread{ private Socket socket = null; private DataInputStream in = null; private DataOutputStream out = null; private ClientThread client = null; public Client(String serverAddress, int serverPort) throws UnknownHostException, IOException { socket = new Socket(serverAddress, serverPort); in = new DataInputStream(System.in); out = new DataOutputStream(socket.getOutputStream()); client = new ClientThread(this, socket); } public void run(){ while(true){ try{ out.writeUTF(in.readLine()); out.flush(); } catch (IOException e){ e.printStackTrace(); } } } public void handle(String message){ System.out.println(message); } public static void main(String[] args) { try{ Client myClient = new Client("ernie.icslab.agh.edu.pl", 8080); myClient.start(); } catch (UnknownHostException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } } } import java.net.*; import java.io.*; public class ClientThread extends Thread{ private Socket socket = null; private Client client = null; private DataInputStream in = null; public ClientThread(Client client, Socket socket) throws IOException { this.client = client; this.socket = socket; in = new DataInputStream(socket.getInputStream()); start(); } public void run(){ while(true){ try{ client.handle(in.readUTF()); } catch (IOException e){ e.printStackTrace(); } } } }