Skip to content

Commit

Permalink
Merge pull request Bobcodiz#1 from Bobcodiz/main
Browse files Browse the repository at this point in the history
check if the user exists
  • Loading branch information
Red-stevo authored Jul 30, 2024
2 parents 99516f6 + 48bbc70 commit b54cd3d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
15 changes: 14 additions & 1 deletion ChatApp/src/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,51 @@
import java.util.Scanner;

public class Client {
// Define the server port to connect to
private static final int SERVER_PORT = 8080;

public static void main(String[] args) {
String SERVER_IP = null;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the server ip address: ");
// Read the server IP address from the user input
SERVER_IP = scanner.nextLine();

// Try to establish a connection to the server
try (Socket socket = new Socket(SERVER_IP, SERVER_PORT);


// Create a reader to read user input from the console
BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
// Create a reader to read messages from the server
BufferedReader serverIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// Create a writer to send messages to the server
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {

// Thread to listen for and print messages from the server
Thread serverListener = new Thread(() -> {
String serverMessage;
try {
// Continuously read messages from the server
while ((serverMessage = serverIn.readLine()) != null) {
// Print each message received from the server
System.out.println(serverMessage);
}
} catch (IOException e) {
// Handle any IO exceptions that occur while reading messages from the server
e.printStackTrace();
}
});
// Start the server listener thread
serverListener.start();

// Read user input from the console and send it to the server
String userMessage;
while ((userMessage = userIn.readLine()) != null) {
// Send each user input message to the server
out.println(userMessage);
}
} catch (IOException e) {
// Handle IO exceptions that may occur during client operation
e.printStackTrace();
}
}
Expand Down
14 changes: 12 additions & 2 deletions ChatApp/src/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ClientHandler extends Thread{
private BufferedReader in;
private InputStreamReader reader;
private OutputStreamWriter writer;

public ClientHandler(Socket socket, Set<ClientHandler> clientHandlers) throws IOException {
this.socket = socket;
this.clientHandlers = clientHandlers;
Expand All @@ -33,20 +34,28 @@ public void run() {
}

String message;

// Continuously read messages from the client
while ((message = in.readLine()) != null) {
if (message.startsWith("@")){

// Handle private messages
int splitIndex = message.indexOf(' ');
String target = message.substring(1,splitIndex);
String privateChat = message.substring(splitIndex + 1);
sendPrivateMessage(target,privateChat);
}else {
}
else {
// Broadcast public messages to all clients
broadcastMessage(message);
}
}
} catch (IOException e) {
// Handle IO exceptions that may occur during client communication
throw new RuntimeException(e);
}
finally {
// Cleanup and notify other clients when a client disconnects
try {
socket.close();
} catch (IOException e) {
Expand All @@ -61,7 +70,7 @@ public void run() {
}
}
}

// Method to send a private message to a specific client
private void sendPrivateMessage(String targetName, String message) throws IOException {
synchronized (clientHandlers) {
for (ClientHandler client : clientHandlers) {
Expand All @@ -72,6 +81,7 @@ private void sendPrivateMessage(String targetName, String message) throws IOExce
}
}
}
// Method to broadcast a message to all connected clients
private void broadcastMessage(String message) {
synchronized (clientHandlers) {
for (ClientHandler client : clientHandlers) {
Expand Down
22 changes: 19 additions & 3 deletions ChatApp/src/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@

public class Server {

static int SERVER_PORT = 8080;
private static final Set<ClientHandler> clientHandlers = Collections.synchronizedSet(new HashSet<>());
// The port number on which the server will listen for client connections
static int SERVER_PORT = 8080;

// A synchronized set to keep track of all connected client handlers
private static final Set<ClientHandler> clientHandlers = Collections.synchronizedSet(new HashSet<>());

public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
System.out.println("Waiting for clients..");


// Continuously listen for new client connections


while (true) {
// Accept a new client connection
Socket socket = serverSocket.accept();
System.out.println("Client connected..");

// Create a new ClientHandler for the connected client
ClientHandler clientHandler = new ClientHandler(socket, clientHandlers);

for (ClientHandler client : clientHandlers){
Expand All @@ -28,13 +37,20 @@ public static void main(String[] args) {
}
}

// Add the new client handler to the set of client handlers
clientHandlers.add(clientHandler);

// Start a new thread to handle the client's communication
new Thread(clientHandler).start();

}

} catch (IOException e) {
// Handle IO exceptions that may occur during server operation
throw new RuntimeException(e);
}
}
}

}


0 comments on commit b54cd3d

Please sign in to comment.