forked from AdvancedProgramming1404/WS-09-Network
implement all TODOs
This commit is contained in:
@@ -38,18 +38,38 @@ public class ChatClient {
|
|||||||
|
|
||||||
private void connectToServer() throws IOException {
|
private void connectToServer() throws IOException {
|
||||||
// TODO: create socket connection to host:port
|
// TODO: create socket connection to host:port
|
||||||
|
Socket socket1 = new Socket(host, port);
|
||||||
// TODO: initialize output stream (PrintWriter)
|
// TODO: initialize output stream (PrintWriter)
|
||||||
|
out = new PrintWriter(socket1.getOutputStream(), true);
|
||||||
// TODO: initialize input stream (BufferedReader)
|
// TODO: initialize input stream (BufferedReader)
|
||||||
|
in = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readUserInputAndSend() throws IOException {
|
private void readUserInputAndSend() throws IOException {
|
||||||
// TODO: read continuously from console
|
// TODO: read continuously from console
|
||||||
// TODO: send each input line to server
|
// TODO: send each input line to server
|
||||||
|
// while (true){
|
||||||
|
// String message = in.readLine();
|
||||||
|
// out.println(message);
|
||||||
|
// }
|
||||||
|
String userInput;
|
||||||
|
while ((userInput = consoleReader.readLine()) != null)
|
||||||
|
{
|
||||||
|
out.println(userInput);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cleanup() {
|
private void cleanup() {
|
||||||
// TODO: close socket safely
|
// TODO: close socket safely
|
||||||
// TODO: release resources
|
// TODO: release resources
|
||||||
|
try {
|
||||||
|
if(socket != null && !socket.isClosed())
|
||||||
|
{
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
}catch (IOException e){
|
||||||
|
System.err.println("Error closing client: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -42,17 +42,30 @@ public class ChatServer {
|
|||||||
// step 1: iterate through connectedClients safely
|
// step 1: iterate through connectedClients safely
|
||||||
// step 2: skip the sender client
|
// step 2: skip the sender client
|
||||||
// step 3: send message to remaining clients using sendMessage
|
// step 3: send message to remaining clients using sendMessage
|
||||||
|
for(ClientHandler client: connectedClients)
|
||||||
|
{
|
||||||
|
if(!client.getUsername().equals(sender.getUsername()))
|
||||||
|
{
|
||||||
|
client.sendMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void broadcastSystemMessage(String message) {
|
public void broadcastSystemMessage(String message) {
|
||||||
// step 1: iterate through all connected clients
|
// step 1: iterate through all connected clients
|
||||||
// step 2: send system message to each client
|
// step 2: send system message to each client
|
||||||
|
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 1: remove client from connectedClients set
|
||||||
// step 2: retrieve username from clientHandler
|
// step 2: retrieve username from clientHandler
|
||||||
// step 3: broadcast system message about user leaving
|
// step 3: broadcast system message about user leaving
|
||||||
|
connectedClients.remove(clientHandler);
|
||||||
|
broadcastSystemMessage(clientHandler.getUsername() + " left");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public class ClientHandler implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void requestUsername() throws IOException {
|
private void requestUsername() throws IOException {
|
||||||
out.println("Enter your username:");
|
out.println("Enter your username: ");
|
||||||
username = in.readLine();
|
username = in.readLine();
|
||||||
if (username == null || username.trim().isEmpty()) {
|
if (username == null || username.trim().isEmpty()) {
|
||||||
username = "Anonymous";
|
username = "Anonymous";
|
||||||
|
|||||||
@@ -15,5 +15,15 @@ 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 message;
|
||||||
|
while ((message = inputReader.readLine()) != null)
|
||||||
|
{
|
||||||
|
System.out.println(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user