selector.select() is a blocking call. I assume when you say that the process stops that you're saying the program hangs at that call rather than the program stopping such as throwing an exception or Java crashing entirely. That call is a blocking call because the selector is waiting for an operation on one of its selected keys to fire. If the selector has a key registered and that key triggers an event, you should move on from that select() method and into more logic in your code. For example, the code in the video moves on initially when a socket connection gets established and the OS is ready to accept the connection.
Subscribed!! I've been trying to make a program that has multiple chatrooms (and also multiple clients) but i'm stumped (bordering on desperate). I just can't figure out how to get more than one chatroom working. Do you possibly have any tips for me?
That’s a good question. For a multi chat room application, you’d want to have a protocol for the server to tell the connecting client the list of chat rooms available or creating a new one. Once that’s done, then you want to maintain the list of active chat rooms in the server and then the client connects to a particular room. Once in the room, the client can broadcast its message to the other clients in that specific room. That’s at least how I would approach the problem initially
Can I broadcast the message to other clients ? tried in else part not working else { buffer.flip(); var message = new String(buffer.array(), buffer.position(), byteRead); System.out.println(message); while (buffer.hasRemaining()) { for (var otherClient : clients) { if(otherClient != client) otherClient.write(buffer); } } buffer.clear(); }
Yes, something like this should do the trick for (var entry : clients) { while (buffer.hasRemaining()) { entry.write(buffer); } buffer.rewind(); } You have to rewind the buffer because when you do a read or write it sets the position and mark accordingly with where in the buffer it should do the next read/write. If you want to send the same message to multiple entities, you essentially want to leave the data in the buffer while setting it back to its starting possition. Additionally to get this working in the client you'll have to modify some code there too. Currently the clients are in blocking mode and expect input from the user first. If you want the server to send something separately from user input, you'll either need to setup the client with a selector like we have on the server or have a different thread reading data from the connection separate from a writing thread. Hopefully this helps!
If I’m understanding your question correctly, you would need to setup a public IP address with this method that is reachable to other networks and as long as you had network connectivity and the same socket protocol, you should generally be able to communicate.
excellent underrated channel.
Thanks, I appreciate it!
Fantastic socket programming tutorial, Will! You give fantastic examples and explain everything very clearly. 😀
Thank you! This is an interesting API for sure
Great job explaining this. Very informative and fun.
Thank you! Glad you liked it
I just love these Java socketing videos. You're the best!
You’re very welcome! Glad to hear you like them
Thanks for this great content! 🎉
My pleasure!
you're awesome man, you should have way more suscribers and views
Thank you!
Thanks for this tutorial.
Glad you liked it! I thought this one was fun to make 🙂
Very nice, I use the same code, jdk 11, debug on Eclipse 4.24.0, process stops at selector.select(); Any suggestion?
selector.select() is a blocking call. I assume when you say that the process stops that you're saying the program hangs at that call rather than the program stopping such as throwing an exception or Java crashing entirely. That call is a blocking call because the selector is waiting for an operation on one of its selected keys to fire. If the selector has a key registered and that key triggers an event, you should move on from that select() method and into more logic in your code. For example, the code in the video moves on initially when a socket connection gets established and the OS is ready to accept the connection.
Subscribed!! I've been trying to make a program that has multiple chatrooms (and also multiple clients) but i'm stumped (bordering on desperate). I just can't figure out how to get more than one chatroom working. Do you possibly have any tips for me?
That’s a good question. For a multi chat room application, you’d want to have a protocol for the server to tell the connecting client the list of chat rooms available or creating a new one. Once that’s done, then you want to maintain the list of active chat rooms in the server and then the client connects to a particular room. Once in the room, the client can broadcast its message to the other clients in that specific room. That’s at least how I would approach the problem initially
@@willtollefson Thanks!
Can I broadcast the message to other clients ?
tried in else part not working
else {
buffer.flip();
var message = new String(buffer.array(), buffer.position(), byteRead);
System.out.println(message);
while (buffer.hasRemaining()) {
for (var otherClient : clients) {
if(otherClient != client) otherClient.write(buffer);
}
}
buffer.clear();
}
Yes, something like this should do the trick
for (var entry : clients) {
while (buffer.hasRemaining()) {
entry.write(buffer);
}
buffer.rewind();
}
You have to rewind the buffer because when you do a read or write it sets the position and mark accordingly with where in the buffer it should do the next read/write. If you want to send the same message to multiple entities, you essentially want to leave the data in the buffer while setting it back to its starting possition. Additionally to get this working in the client you'll have to modify some code there too. Currently the clients are in blocking mode and expect input from the user first. If you want the server to send something separately from user input, you'll either need to setup the client with a selector like we have on the server or have a different thread reading data from the connection separate from a writing thread. Hopefully this helps!
can someone from another network join the chat with this method
If I’m understanding your question correctly, you would need to setup a public IP address with this method that is reachable to other networks and as long as you had network connectivity and the same socket protocol, you should generally be able to communicate.