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.
hi, how did you end one client using the command line? At time 2:01. I tried it using Ctrl+C in mac and the server generated a NullPointerException as soon as that client terminated using Ctrl+C
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! 😄
At 23:40 shouldn't you be doing closeEverything(clientHandler.socket, clientHandler.bufferedReader, clientHandler.bufferedWriter) ? You are iterating through each of clientHandlers, and for each clientHandler, you push the message. Hence if u encounter an exception, you should be closing THAT particular clientHandler's connections. Same logic applies when we are removing the clientHandler from the array. Instead of passing 'this', shouldn't we pass clientHandler?
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
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.
hi, thank you for let me learn something in this video. i am using Eclipse IDE to run this application. i found the port and the localhost issue in Eclipse IDE i am using in. I dont know how to make changes to it i tried to use some method in some reference book but it still pops up this window"PORT cannot be resolved to a variable".
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.
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)
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! 😀
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
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?
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 !😍
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?
is there any difference if we use other io classes such as printwriter, datagramoutput, bufferedoutputstream? I got confused sometimes when I work with other io classes for Java Socket, it doesn't work. The io classes don't input nor output the given string.
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
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
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.
I think this is the best video on UA-cam about I/O streams and sockets.
Is there any way I can see your code
This dude literally explained my entire 5 months course in a 40 minute video
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!
Thanks so much man! Also love your taste in music haha 🎸
@@WittCode hell ya Java and Punk Rock are a 90s dynamic duo🤘
@@WittCode can i make those names of clients in diferent color ?
@@og_yamiatan3957 I mean it's in the cmd prompt so prob not, but if you make a GUI for it you could find a way
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.
This truly explains everything so clear, thanks man
This is so exciting!!! I had been trying other tutorials, even paid ones and had problems with all the others. On this one everything went perfect.
Haven't even finished the video yet but I'm happy I found this channel!
IT WORKED, THANKS I'VE BEEN LOOKING FOR THIS FOREVER, BUT NO TUTORIAL COULD EXPLAIN IT AS YOU DID
Wow man, you really clearly showed how to make such chat in Java, thanks a lot! :)
Let me just tell you one thing. Thanks for Being in UA-cam to share content most are not capable of doing.
Thanks so much!
this video is a real gem for us cs students. thank you so much!
Great to hear! Yeah back when I was in school I had to code this for a class 😀
Thank you so much for this video! Very well explained, it helped me a lot for my university project.
Best video out here, thank you kind dude
THIS IS SO COOL! And I managed to reinforce sockets, IO, AND threads ALL AT ONCE
You explain everything so well and simple ♡
So underrated!!! I wish you'll gain more subscribers and viewers. Amazing video
I understand all men you're a great teacher!!
Thx for your tutorial, I finally understand how socket work
Great work! I love the thumbnail! So many WittCodes!
Thanks for the support Freddie! 😎
Lowkey saved my uni lab work xd , keep up the good work !!
You are a lifesaver man, thank you a lot you are saving me a ton of hours and nerves :)
Thank you so much dude! Consider becoming a teacher in the future 🙏 You have that pedagogical talent :)
Thank you, I had trubles with sending message to multiple clients, because of reading system input, thanks again!
Hey there thanks so much! Yeah the reading input part tripped me up for a while too! 😃
thank you so much, this content deserves much more credit
Thanks so much! That means a lot! 😃
This is what, I looked for in Java. Thank you so.
Thank you! Awesome video and explanation!
😃
great video thanks for making it i was stuck on this for so long
great video, wonderful explanations throughout too.
😀
I'm so grateful to you, this really so helpful man thank you so much
Greetings from Ukraine! You are awesome! Thank you so much for this great tutorial. It helped me a lot.
Wow you are suck a great teacher. You explain every step. Thank you so much!
Nice one man. Keep it up!
Thank you for the clear and concise explanation. I just subscribed you. I wish to watch your more videos.
😀
Thank you! Learned a lot from this
You are lifesaver man, thank you a lot :)
Very helpful! Thank you so much!
This is simply awesome man 👍❤️
this is briliant my friends thank you so much
Your content is amazing brother, keep it up! I would love to see this video with a GUI integration as you said in the end of the video.
Thanks so much Kevin! And I will certainly be making a GUI version of this! Probably using JavaFX 😀
@@WittCode , hey witt please do that asap
Thank you for this excellent content.
😀
Great content, learned a lot!
Is there anyway I can see your code by chance, that is if it's working
your video is awesome and helped me a lot. thank you.
hi, how did you end one client using the command line? At time 2:01. I tried it using Ctrl+C in mac and the server generated a NullPointerException as soon as that client terminated using Ctrl+C
Great video all the best
Great to hear! Thanks for watching
Thank you so much!!! This is what I was finding!
thank you! I liked this video.
Great video! Really helped!
I took a server side approach instead of a client side approach, love the video tho, thank you
thanks a lot bro, keep the good job.✌
Thank you very much for this!
tysm for this video! its great! one question tho.. at 21:14
couldn't you check if this!=clientHandler?
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! 😄
@@WittCode yeah i was wondering abt duplicate usernames lol
At 23:40 shouldn't you be doing closeEverything(clientHandler.socket, clientHandler.bufferedReader, clientHandler.bufferedWriter) ? You are iterating through each of clientHandlers, and for each clientHandler, you push the message. Hence if u encounter an exception, you should be closing THAT particular clientHandler's connections. Same logic applies when we are removing the clientHandler from the array. Instead of passing 'this', shouldn't we pass clientHandler?
video is very helpful. Thank you.
thank you so much, saved my homework truly!
bro can you send code please
Great tutorial!
This helped me a lot, thanks!
Thank you for good educating content. on 21:46, could we use if(this!=clientHandler) {}?
this was perfect for me thank you
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
Thanks bro it has been very usefull for me
thank you! Subscribed!
Great video!
Hello sir. Is the code still on your website? i can only seem to see some Javascript. Is it on a github page?
thank uuuuu so much, this is so helpful
Which design patterns are we using in this program and which pattern I can implement into the code in addition to develop the code?
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.
@@fabiansvensson9588 thanks a lot!
@@fabiansvensson9588 Oh my god i was desperate while seaching the solution and boom i found your comment. You're a god.
at which line do you add this "if(messageFromClient == null) throw new IOException();" ?
Thank you man, is that possible to chat only with one person from the group please share if you have such kind of code
Great tutorial, thanks
Nice tutorial man, rly need something like tihs :D
Thanks so much! Also, love your username lol
awesome video i love it thankyouu
very helpful video !
Thank you very much, man
hi, thank you for let me learn something in this video.
i am using Eclipse IDE to run this application. i found the port and the localhost issue in Eclipse IDE i am using in. I dont know how to make changes to it i tried to use some method in some reference book but it still pops up this window"PORT cannot be resolved to a variable".
Love the thumbnail hahah
Sahil! Good to hear from you man! And yeah I thought it was the coolest one Ive made so far lol
Why we can't run the server again? After it being in use? Like turn off the server and Re-open it.
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.
yah it sounds correct
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)
i tried it it works but my message dont appear to other clients it only appears in my chat only not other chats'
Is it a TCP or UDP based communication? Really good Video
Can you share the code, not able to find in the link you kept in description
Shouldn't we rather use AtomicReferenceArray for the clientHandlers to avoid race conditions since more thread access it?
You are correct, use thread-safe collections or manage synchronization
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! 😀
@@WittCode Thank you so much, I really appreciate you taking the time out of your day to respond to my request!
Maybe I missed it, but why did you write out the closeServerSocket method and not use it?
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
Very good!
Many thanks Sir 😘
Real gem!!
Thank you very much!
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?
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 !😍
Helpful video thanks for making this. Can you do a server PC and a client mobile phone.
great channel
If closing the socket also closes both the input and output streams, why are you closing them in addition to closing the socket in 26:30?
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?
is there any difference if we use other io classes such as printwriter, datagramoutput, bufferedoutputstream? I got confused sometimes when I work with other io classes for Java Socket, it doesn't work. The io classes don't input nor output the given string.
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
I have tried to allow multiple instances of my ChatClient class but it isn't working. Can you please help me with this! Thank you!
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
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.
What would be the best way to add latency to all the clients receiving a message? Like, if one writes a message, everyone receives it 5s later?