0% found this document useful (0 votes)
14 views7 pages

Programming Assignment Unit 7

Uploaded by

cebeni8671
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
14 views7 pages

Programming Assignment Unit 7

Uploaded by

cebeni8671
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Online Chat Application: Java Implementation

Server Implementation
ChatServer.java
Code:
import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {


private static final int PORT = 12345;
private static Set<ClientHandler> clientHandlers = new HashSet<>();

public static void main(String[] args) {


System.out.println("Chat server started...");
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
while (true) {
Socket clientSocket = serverSocket.accept();
ClientHandler clientHandler = new ClientHandler(clientSocket);
clientHandlers.add(clientHandler);
new Thread(clientHandler).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}

static void broadcastMessage(String message, ClientHandler sender) {


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

static void removeClient(ClientHandler clientHandler) {


clientHandlers.remove(clientHandler);
}
}

class ClientHandler implements Runnable {


private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
private String userId;

public ClientHandler(Socket socket) {


this.clientSocket = socket;
}

public void run() {


try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);

userId = "User" + clientSocket.getPort();


sendMessage("Connected as " + userId);
System.out.println(userId + " connected.");

String message;
while ((message = in.readLine()) != null) {
System.out.println(userId + ": " + message);
ChatServer.broadcastMessage(userId + ": " + message, this);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
ChatServer.removeClient(this);
System.out.println(userId + " disconnected.");
}
}

void sendMessage(String message) {


out.println(message);
}
}

Client Implementation
ChatClient.java
Code:
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 = 12345;

public static void main(String[] args) {


try (Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()))) {

Scanner scanner = new Scanner(System.in);


new Thread(new IncomingReader(in)).start();

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


String message;
while (true) {
message = scanner.nextLine();
out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class IncomingReader implements Runnable {
private BufferedReader in;

public IncomingReader(BufferedReader in) {


this.in = in;
}

public void run() {


try {
String message;
while ((message = in.readLine()) != null) {
System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

README
README.md
# Online Chat Application

## Overview
This is a simple online chat application developed in Java using socket programming. It allows
multiple users to connect to a central server, send messages, and receive messages from other
users.
## Requirements
- Java Development Kit (JDK) 8 or higher

## How to Run

### Server
1. Compile the server code
2. Run the server

### Client
1. Compile the client code
2. Run the client

## Features
- Server can handle multiple client connections.
- Each client is assigned a unique user ID.
- Clients can send and receive messages in real-time.
## Screenshot

## Notes
- The server runs on port 12345 by default. Ensure this port is available on your machine.
- To change the server address or port for the client, modify the `SERVER_ADDRESS` and
`SERVER_PORT` constants in `ChatClient.java`.

You might also like