Thank you for taking time to explain each step in simple terms. I tried this on my own and was hung up on how each CH object could share 1 updated array list, but then you pointed out the beauty of static variables!
Remember to use 'Thread-Safe" collections or manage synchronization in this example.Our static arraylist is a shared resource, and can be accessed by multiple threads simultaneously, where each one can add, remove or read.
Thank you WittCode , you have just earned a new subscriber. I cannot begin to explain how helpful and useful your videos are especially this one. I am a software engineering student and I can safely say that this one video is more useful than my entire Java programming class.
In the broadcastMessage method, why compare usernames when you can just do: if (clientHandler != this) since clientHandlers is just an array of the clientHandler instances. Also doing it this way allows there to be multiple users with the same username since it does not rely on unique usernames to broadcast correctly, but instead on the instances. (I have not tested it yet, but it should work, if it doesn't then I will update this comment) Edit: I said I would only update if it didn't work, but it worked and I thought I would update it anyways. TL;DR it works, and its probably the better way to do it.
Runnable passed to Thread in listenForMessage() could be implemented by lambda new Thread( () -> {/*code*/}); because Runnable interface has only 1 abstract method, thus making it functional interface.
Great cover of sockets in general. Just wanted to mention that I got an error in the ClientHandler whenever the Client exited. For some reason, the exception was not thrown and the bufferedReader read a "null" value from the input. I added this line and it worked: if(messageFromClient == null) throw new IOException(); By the way, if we wanted to create a network without the use of an intermediary server, so basically client-to-client direct communication. Any tips on how you could go about doing that? Thank you sir!
I met that problem, too. The things that if I applied your code, the server won't notify that the client who exited is leaving the chat. Is there another way to do that
@@ngocminhpham1358 You should add the code that you want to execute in the catch: try { messageFromClient = bufferedReader.readLine(); if(messageFromClient == null) throw new IOException(); broadcastMessage(messageFromClient); } catch(IOException e) { closeEverything(socket, bufferedReader, bufferedWriter); break; } Inside closeEverything, there should be a method called "removeClientHandler();" (this is in the video). If this method is executed then the server should be notifying everyone that the client has left the chat. Double check that you added this catch cluase as well as your "closeEverything" method.
Hello, I see that exception that is : Exception in thread "Thread-0" java.lang.NullPointerException When a client leave from chat. Code is OK but why do i see that exception?
Perfeito bom demais essa aula, ficou muito bom mesmo . Sem enrolação e sem complicação foi ótimo esse tutorial, valeu deixo o meu like e minha inscrição, obrigado
This is very impressive and it works. I like your approach with clientHandlers. Im a little confused about how multiple clients can connect using the same port. When I did a similar project in C# i got an error saying something like "this port is already in use". Can anyone explain that to me?
Hey wittcode, I hope you see this, I loved the tutorial, and you explained everything very well with a hands on approach, but is it possible to run the clients on different computers? If so, how?
i gotchu just run a client-like file on a different computer and use the ip address of your server computer instead of "localhost", also make sure they're on the same network
Isn't it safer to convert public classes to private classes if they are not used in another class? or there is a reason for it? Thank you for this incredible work, your explanations are amazing !😍
Nice video it was a really nice watch and i learned a bunch of new things from it! Can anyone please explain how to run it in CMD tho? (it looked cool)
hello i have a question, what should i do to make them connect from different pcs and not on localhost. i tried some solutions but it always give me an error
Hey good to hear from you again! That is correct! And actually probably a better idea as I did not check for duplicate usernames provided by clients! 😄
Great video, loved it. Thank you for explaining the details. Is it possible to implement this structure of group chat with graphical user interface? Actually I tried but I couldn't figure it out. It simply didn't work
I just wanted to say I love your content and how you interact with your fan base but I was also wondering how to run this program from command prompt and how to configure this for use over WAN networks. PS: this tutorial inspired me to make my own chat room project which has been my first real (successful) program.
Hey John thanks so much for the feedback! To run it over the command prompt you just have to go to where you made the java files and compile it like javac .java and then run it like java . And I have gotten a few questions about making these Java networking programs accessible over the internet and to be honest I am not entirely sure how to do it besides port forwarding or giving your server a public address. Im probably gonna take some time to look into this and make a video on it! 😀
Extremely thankful for helping us in the easiest ways but, if i want to store every one of the texts sent in a file that stays in the "Server" and whenever a new client joins he/she gets all of the messages printed from the file in the console .. to do that what are the changes i should do?
how come the same socket in client that sends a message doesn't receive one? I know that we look in arrayList and avoid that but it should work based on the code from the client.java
Heyo, thank you so much for the tutorial! I have a little problem though. If I close one of the client windows and start a new one. The chat is filled with the word "null" and I don't know what would cause it. Any idea how to fix it? 😅
Ran into the same issue. The fix is to do a null check for what bufferedReader.readLine() returns in run() method in ClientHandler.java, cuz readLine() will immediately return null if the the corresponding Client program gets terminated.
Tried to adapt this to my game, but only managed to get the server to register the connections, but the clients didnt seem to receive anything. Dunno if the issue was caused by the gui or what.