Untitled
unknown
plain_text
21 days ago
4.3 kB
3
Indexable
Never
//Server import java.io.*; import java.net.*; import java.util.*; public class ChatServer { private static final int PORT = 12345; private static Set<PrintWriter> clientWriters = new HashSet<>(); public static void main(String[] args) { System.out.println("Chat server started..."); try (ServerSocket serverSocket = new ServerSocket(PORT)) { while (true) { new ClientHandler(serverSocket.accept()).start(); } } catch (IOException e) { e.printStackTrace(); } } private static class ClientHandler extends Thread { private Socket socket; private PrintWriter out; private BufferedReader in; private String clientName; public ClientHandler(Socket socket) { this.socket = socket; } public void run() { try { in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); synchronized (clientWriters) { clientWriters.add(out); } // Get client name clientName = in.readLine(); // Client should send its name immediately broadcast("User " + clientName + " has joined the chat."); String message; while ((message = in.readLine()) != null) { broadcast(clientName + ": " + message); } } catch (IOException e) { e.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } synchronized (clientWriters) { clientWriters.remove(out); } broadcast("User " + clientName + " has left the chat."); } } private void broadcast(String message) { synchronized (clientWriters) { for (PrintWriter writer : clientWriters) { writer.println(message); } } } } } //Client import java.io.*; import java.net.*; import java.util.Scanner; public class ChatClient { private static final String SERVER_ADDRESS = "127.0.0.1"; private static final int PORT = 12345; private PrintWriter out; private String name; public static void main(String[] args) { new ChatClient().start(); } public void start() { try (Socket socket = new Socket(SERVER_ADDRESS, PORT)) { out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); Scanner scanner = new Scanner(System.in); // Thread to listen for messages from the server new Thread(() -> { String message; try { while ((message = in.readLine()) != null) { System.out.println("\n" + message); System.out.print("You: "); // Re-prompt after message } } catch (IOException e) { System.out.println("Connection closed."); } }).start(); // Prompt for username and send it to the server System.out.print("Enter your name: "); name = scanner.nextLine(); out.println(name); // Send username to the server // Message input loop String message; while (true) { System.out.print("You: "); // Prompt for user input message = scanner.nextLine(); if (message.equalsIgnoreCase("!Q")) { out.println(name + " has left the chat."); break; // Exit the loop if user types !Q } out.println(message); // Send message to server } System.out.println("Exiting the chat..."); out.close(); // Close the PrintWriter } catch (IOException e) { e.printStackTrace(); } } }
Leave a Comment