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

Java Chat Server and Client Code

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)
6 views4 pages

Java Chat Server and Client Code

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

import [Link].

*;
import [Link].*;
import [Link].*;

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);
[Link]("Chat server started on port 12345...");

while (true) {
Socket clientSocket = [Link]();
clientId++;
ClientHandler clientHandler = new ClientHandler(clientSocket, "User" + clientId);
[Link](clientHandler);
new Thread(clientHandler).start();
}
}

static void broadcast(String message, ClientHandler sender) {


for (ClientHandler client : clientHandlers) {
if (client != sender) {
[Link](message);
}
}
}

static void removeClient(ClientHandler clientHandler) {


[Link](clientHandler);
}

static class ClientHandler implements Runnable {


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

public ClientHandler(Socket socket, String username) {


[Link] = socket;
[Link] = username;
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader([Link]()));
out = new PrintWriter([Link](), true);

[Link]("Welcome " + username + "!");

String msg;
while ((msg = [Link]()) != null) {
String formatted = username + ": " + msg;
[Link](formatted);
[Link](formatted, this);
}
} catch (IOException e) {
[Link](username + " disconnected.");
} finally {
try {
[Link]();
} catch (IOException e) {}
[Link](this);
}
}

void sendMessage(String message) {


[Link](message);
}
}
}

import [Link].*;
import [Link].*;
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([Link]()));
PrintWriter output = new PrintWriter([Link](), true);
BufferedReader consoleInput = new BufferedReader(new InputStreamReader([Link]));
// Read welcome message from server
[Link]([Link]());
// Create a thread to read messages from server
new Thread(() -> {
String serverMsg;
try {
while ((serverMsg = [Link]()) != null) {
[Link](serverMsg);
}
} catch (IOException e) {
[Link]("Disconnected from server.");
}
}).start();
// Send user input to server
String userInput;
while ((userInput = [Link]()) != null) {
[Link](userInput);
}
} catch (IOException e) {
[Link]("Unable to connect to server: " + [Link]());
}
}
}

You might also like