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