You are on page 1of 7

Online Chat Application

This assignment aims to assess your skills in socket programming, client-server communication,

and user interface design.

Assignment Instructions

You are tasked with developing a simple online chat application using Java. The application

should allow multiple users to connect to a central server, send messages, and receive messages

from other users.

Requirements:

Server Implementation:

a. Create a server class, ChatServer, using socket programming to manage connections from

multiple clients.

b. The server should be able to handle incoming connections, assign a unique user ID to each

connected client, and maintain a list of connected users.


Server Implementation (ChatServer.java):

import java.io.*;

import java.net.*;

import java.util.*;

public class ChatServer {

private static final int PORT = 5050;

private static Set<PrintWriter> clientWriters = new HashSet<>();

private static int userIdCounter = 1;

public static void main(String[] args) {

try (ServerSocket serverSocket = new ServerSocket(PORT)) {

System.out.println("Server is running...");

while (true) {

new ClientHandler(serverSocket.accept()).start();

} catch (IOException e) {

e.printStackTrace();

private static class ClientHandler extends Thread {


private Socket socket;

private PrintWriter writer;

private int userId;

public ClientHandler(Socket socket) {

this.socket = socket;

this.userId = userIdCounter++;

public void run() {

try {

BufferedReader reader = new BufferedReader(new

InputStreamReader(socket.getInputStream()));

writer = new PrintWriter(socket.getOutputStream(), true);

clientWriters.add(writer);

System.out.println("User " + userId + " connected.");

String message;

while ((message = reader.readLine()) != null) {

System.out.println("Received from user " + userId + ": " + message);

broadcast(message);

}
} catch (IOException e) {

e.printStackTrace();

} finally {

if (writer != null) {

clientWriters.remove(writer);

try {

socket.close();

} catch (IOException e) {

e.printStackTrace();

System.out.println("User " + userId + " disconnected.");

private void broadcast(String message) {

for (PrintWriter writer : clientWriters) {

writer.println("User " + userId + ": " + message);

}
Client Implementation:

a. Implement a client class, ChatClient, that connects to the server using sockets.

b. Each client should be able to send messages to the server, which will broadcast the messages

to all connected clients.

c. Clients should also be able to receive messages from other users.

Client Implementation (ChatClient.java):

import java.io.*;

import java.net.*;

import java.util.Scanner;

public class ChatClient {

private static final String SERVER_ADDRESS = "localhost";

private static final int SERVER_PORT = 5050;

public static void main(String[] args) {

try (Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT)) {

BufferedReader serverReader = new BufferedReader(new

InputStreamReader(socket.getInputStream()));

PrintWriter serverWriter = new PrintWriter(socket.getOutputStream(), true);

Scanner scanner = new Scanner(System.in);


System.out.println("Connected to the server.");

Thread receiveThread = new Thread(() -> {

try {

String message;

while ((message = serverReader.readLine()) != null) {

System.out.println(message);

} catch (IOException e) {

e.printStackTrace();

});

receiveThread.start();

String userInput;

while ((userInput = scanner.nextLine()) != null) {

serverWriter.println(userInput);

} catch (IOException e) {

e.printStackTrace();

}
Sample Output:

Server console:

Server is running...

User 1 connected.

Received from user 1: Hello everyone!

User 2 connected.

Received from user 2: Hi there!

User 1 disconnected.

Client console:

Connected to the server.

Hello everyone!

User 2: Hi there!

You might also like