Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d18248cfe9 |
@@ -37,19 +37,29 @@ public class ChatClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void connectToServer() throws IOException {
|
private void connectToServer() throws IOException {
|
||||||
// TODO: create socket connection to host:port
|
|
||||||
// TODO: initialize output stream (PrintWriter)
|
socket = new Socket(host, port);
|
||||||
// TODO: initialize input stream (BufferedReader)
|
out = new PrintWriter(socket.getOutputStream(), true);
|
||||||
|
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readUserInputAndSend() throws IOException {
|
private void readUserInputAndSend() throws IOException {
|
||||||
// TODO: read continuously from console
|
|
||||||
// TODO: send each input line to server
|
String line;
|
||||||
|
while((line = consoleReader.readLine()) != null) {
|
||||||
|
out.println(line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cleanup() {
|
private void cleanup() {
|
||||||
// TODO: close socket safely
|
|
||||||
// TODO: release resources
|
try {
|
||||||
|
if (socket != null && !socket.isClosed()) {
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error closing socket: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -39,20 +39,29 @@ public class ChatServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void broadcast(String message, ClientHandler sender) {
|
public void broadcast(String message, ClientHandler sender) {
|
||||||
// step 1: iterate through connectedClients safely
|
|
||||||
// step 2: skip the sender client
|
synchronized (connectedClients) {
|
||||||
// step 3: send message to remaining clients using sendMessage
|
for (ClientHandler client : connectedClients) {
|
||||||
|
if (client != sender) {
|
||||||
|
client.sendMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void broadcastSystemMessage(String message) {
|
public void broadcastSystemMessage(String message) {
|
||||||
// step 1: iterate through all connected clients
|
|
||||||
// step 2: send system message to each client
|
synchronized (connectedClients) {
|
||||||
|
for (ClientHandler client : connectedClients) {
|
||||||
|
client.sendMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeClient(ClientHandler clientHandler) {
|
public void removeClient(ClientHandler clientHandler) {
|
||||||
// step 1: remove client from connectedClients set
|
|
||||||
// step 2: retrieve username from clientHandler
|
connectedClients.remove(clientHandler);
|
||||||
// step 3: broadcast system message about user leaving
|
broadcastSystemMessage("[System] " + clientHandler.getUsername() + " left the chat.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -15,5 +15,17 @@ public class MessageListener implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
// TODO: continuously read messages from server
|
// TODO: continuously read messages from server
|
||||||
// TODO: print received messages immediately
|
// TODO: print received messages immediately
|
||||||
|
|
||||||
|
try {
|
||||||
|
String line;
|
||||||
|
while ((line = inputReader.readLine()) != null) {
|
||||||
|
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
|
||||||
|
System.out.println("Error while reading from input stream" + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user