0% found this document useful (0 votes)
2 views4 pages

Chat App

The document contains a Java implementation of a simple chat server and client. The server listens for client connections, assigns usernames, and facilitates message broadcasting among connected clients. The client connects to the server, receives a welcome message, and allows users to send and receive messages in real-time.

Uploaded by

belovedvince
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Chat App

The document contains a Java implementation of a simple chat server and client. The server listens for client connections, assigns usernames, and facilitates message broadcasting among connected clients. The client connects to the server, receives a welcome message, and allows users to send and receive messages in real-time.

Uploaded by

belovedvince
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.io.

*;
import java.net.*;
import java.util.*;

public class ChatServer {


private static int clientId = 0;
private static Set<ClientHandler> clientHandlers = new HashSet<>();

public static void main(String[] args) throws IOException {


ServerSocket serverSocket = new ServerSocket(12345);
System.out.println("Chat server started on port 12345...");

while (true) {
Socket clientSocket = serverSocket.accept();
clientId++;
ClientHandler clientHandler = new ClientHandler(clientSocket, "User" + clientId);
clientHandlers.add(clientHandler);
new Thread(clientHandler).start();
}
}

static void broadcast(String message, ClientHandler sender) {


for (ClientHandler client : clientHandlers) {
if (client != sender) {
client.sendMessage(message);
}
}
}

static void removeClient(ClientHandler clientHandler) {


clientHandlers.remove(clientHandler);
}

static class ClientHandler implements Runnable {


private Socket socket;
private PrintWriter out;
private BufferedReader in;
private String username;

public ClientHandler(Socket socket, String username) {


this.socket = socket;
this.username = username;
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);

out.println("Welcome " + username + "!");

String msg;
while ((msg = in.readLine()) != null) {
String formatted = username + ": " + msg;
System.out.println(formatted);
ChatServer.broadcast(formatted, this);
}
} catch (IOException e) {
System.out.println(username + " disconnected.");
} finally {
try {
socket.close();
} catch (IOException e) {}
ChatServer.removeClient(this);
}
}

void sendMessage(String message) {


out.println(message);
}
}
}

import java.io.*;
import java.net.*;
public class ChatClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 12345); // Connect to server on port 12345
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));
// Read welcome message from server
System.out.println(input.readLine());
// Create a thread to read messages from server
new Thread(() -> {
String serverMsg;
try {
while ((serverMsg = input.readLine()) != null) {
System.out.println(serverMsg);
}
} catch (IOException e) {
System.out.println("Disconnected from server.");
}
}).start();
// Send user input to server
String userInput;
while ((userInput = consoleInput.readLine()) != null) {
output.println(userInput);
}
} catch (IOException e) {
System.out.println("Unable to connect to server: " + e.getMessage());
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy