import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; public class TCPServer { private static Thread threadClients; private static Thread threadWriteToClients; private static Thread threadServerCommands; private static ServerSocket welcomeSocket; private static List<Socket> clientsArray = new ArrayList<Socket>(); private static boolean go_on = true; private static String sentence; private static String serverCMD; private static Socket connectionSocket; private static PrintStream outToClient; public static void main(String argv[]) throws Exception { System.out.println("starting main"); welcomeSocket = new ServerSocket(2345); System.out.println("Server socket klar"); // Laver sockets til hver enkel Client (accepterer) threadClients = new Thread(new Runnable() { public void run() { while(go_on) { try { connectionSocket = welcomeSocket.accept(); clientsArray.add(connectionSocket); } catch (IOException e) { e.printStackTrace(); } } } }); threadClients.start(); // Traad til at skrive til samtlige clients threadWriteToClients = new Thread(new Runnable() { public void run() { while(go_on) { try { for (int i = 0; i < clientsArray.size(); i++) { connectionSocket = clientsArray.get(i); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); sentence = inFromClient.readLine(); outToClient = new PrintStream(clientsArray.get(i).getOutputStream()); outToClient.println("FROM SOME CLIENT: " + sentence + "\n"); outToClient.flush(); } } catch (Exception e) { e.printStackTrace(); } } } }); threadWriteToClients.start(); // SERVER kommandoer (gaar ogsaa ud til clients) threadServerCommands = new Thread(new Runnable() { public void run() { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); while(go_on) { try { serverCMD = inFromUser.readLine(); for (int i = 0; i <= clientsArray.size(); i++) { outToClient = new PrintStream(clientsArray.get(i).getOutputStream()); outToClient.println(serverCMD + "\n"); } if (serverCMD.equals("quit")) { go_on = false; System.out.println("quitting the loop and closing the socket"); connectionSocket.close(); welcomeSocket.close(); } } catch (IOException e) { e.printStackTrace(); } } } }); threadServerCommands.start(); } }
import java.io.*; import java.net.*; class TCPClient { private static Thread outputThread; private static Thread inputThread; private static boolean go_on = true; private static String sentence; private static Socket clientSocket; private static DataOutputStream outToServer; private static BufferedReader inFromServer; private static BufferedReader inFromUser; public static void main(String argv[]) throws Exception { System.out.println("starting TCP chat program"); inFromUser = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Forbinder til server"); clientSocket = new Socket("127.0.0.1", 2345); System.out.println("Der er forbindelse til serveren! \n"); outToServer = new DataOutputStream(clientSocket.getOutputStream()); inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); // Output (fra client'en til server) outputThread = new Thread(new Runnable() { public void run() { while(go_on) { try { System.out.println("Please type your text: "); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); } catch (IOException e) { e.printStackTrace(); } } } }); outputThread.start(); inputThread = new Thread(new Runnable() { public void run() { while(go_on) { try { sentence = inFromServer.readLine(); System.out.println(sentence); } catch (IOException e) { e.printStackTrace(); } } try { clientSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }); inputThread.start(); } }