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

Programming Assignment UNIT 7

The document contains Java code for a simple chat application consisting of a server (ChatServer.java) and a client (ChatClient.java). The server handles multiple clients, broadcasting messages and managing user connections, while the client allows users to send and receive messages. Additionally, there is a README file mentioned, although its content is not provided.

Uploaded by

emberwadee25
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

Programming Assignment UNIT 7

The document contains Java code for a simple chat application consisting of a server (ChatServer.java) and a client (ChatClient.java). The server handles multiple clients, broadcasting messages and managing user connections, while the client allows users to send and receive messages. Additionally, there is a README file mentioned, although its content is not provided.

Uploaded by

emberwadee25
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

Java Chat Application

Codes
[Link]

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

public class ChatServer {


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

public static void main(String[] args) {


[Link]("Chat server started...");
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
while (true) {
Socket socket = [Link]();
ClientHandler clientHandler = new ClientHandler(socket);
[Link](clientHandler);
new Thread(clientHandler).start();
}
} catch (IOException e) {
[Link]();
}
}

public static void broadcast(String message, ClientHandler excludeUser) {


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

public static void removeClient(ClientHandler client) {


[Link](client);
}
}

class ClientHandler implements Runnable {


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

public ClientHandler(Socket socket) {


[Link] = socket;
}

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

// Get the username


[Link]("Enter your username: ");
username = [Link]();
[Link](username + " has joined.");
[Link](username + " has joined the chat.", this);

String message;
while ((message = [Link]()) != null) {
[Link](username + ": " + message);
[Link](username + ": " + message, this);
}
} catch (IOException e) {
[Link]();
} finally {
try {
[Link]();
} catch (IOException e) {
[Link]();
}
[Link](this);
[Link](username + " has left.");
[Link](username + " has left the chat.", this);
}
}

public void sendMessage(String message) {


[Link](message);
}
}

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

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([Link](),
true);
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
Scanner scanner = new Scanner([Link])) {

// Get and send the username


[Link]("Enter your username: ");
String username = [Link]();
[Link](username);
// Read messages from server
Thread readThread = new Thread(() -> {
try {
String message;
while ((message = [Link]()) != null) {
[Link](message);
}
} catch (IOException e) {
[Link]();
}
});
[Link]();

// Send messages to server


String message;
while ([Link]()) {
message = [Link]();
[Link](message);
}
} catch (IOException e) {
[Link]();
}
}
}

README file
Output of the codes

You might also like