Java Socket Programming - Multiple Clients Chat

Поделиться
HTML-код
  • Опубликовано: 20 окт 2024

Комментарии • 293

  • @f3sa899
    @f3sa899 2 года назад +61

    I think this is the best video on RUclips about I/O streams and sockets.

  • @GiyuTomiokaTheOneAndOnly
    @GiyuTomiokaTheOneAndOnly Год назад +3

    This dude literally explained my entire 5 months course in a 40 minute video

  • @amc1140
    @amc1140 2 года назад +45

    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!

    • @WittCode
      @WittCode  2 года назад +3

      Thanks so much man! Also love your taste in music haha 🎸

    • @amc1140
      @amc1140 2 года назад

      @@WittCode hell ya Java and Punk Rock are a 90s dynamic duo🤘

    • @og_yamiatan3957
      @og_yamiatan3957 2 года назад

      @@WittCode can i make those names of clients in diferent color ?

    • @Rohan-Prabhala
      @Rohan-Prabhala 2 месяца назад

      @@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

  • @uuo4930
    @uuo4930 2 года назад +29

    This truly explains everything so clear, thanks man

  • @weaponkid1121
    @weaponkid1121 3 года назад +3

    Haven't even finished the video yet but I'm happy I found this channel!

  • @cybernetic777
    @cybernetic777 Год назад +11

    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.

  • @MostafaMahmoud-cj2sh
    @MostafaMahmoud-cj2sh 2 года назад +1

    IT WORKED, THANKS I'VE BEEN LOOKING FOR THIS FOREVER, BUT NO TUTORIAL COULD EXPLAIN IT AS YOU DID

  • @romanzdev
    @romanzdev 8 месяцев назад +2

    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.

  • @imanjallali8258
    @imanjallali8258 2 года назад

    Let me just tell you one thing. Thanks for Being in RUclips to share content most are not capable of doing.

  • @pawe7420
    @pawe7420 2 года назад +2

    Wow man, you really clearly showed how to make such chat in Java, thanks a lot! :)

  • @jadabouzeid9165
    @jadabouzeid9165 2 года назад +3

    Thank you so much for this video! Very well explained, it helped me a lot for my university project.

  • @UninspiredFilm5
    @UninspiredFilm5 3 месяца назад

    THIS IS SO COOL! And I managed to reinforce sockets, IO, AND threads ALL AT ONCE

  • @jackinquisitor9645
    @jackinquisitor9645 Год назад

    Thx for your tutorial, I finally understand how socket work

  • @spenzr6920
    @spenzr6920 Год назад

    So underrated!!! I wish you'll gain more subscribers and viewers. Amazing video

  • @wriottter
    @wriottter Год назад

    I understand all men you're a great teacher!!

  • @cristiangligan-ignatescu771
    @cristiangligan-ignatescu771 2 месяца назад

    Thank you so much dude! Consider becoming a teacher in the future 🙏 You have that pedagogical talent :)

  • @irinatrandafir7390
    @irinatrandafir7390 2 года назад +3

    You explain everything so well and simple ♡

  • @freddiewittmann1494
    @freddiewittmann1494 3 года назад +1

    Great work! I love the thumbnail! So many WittCodes!

    • @WittCode
      @WittCode  3 года назад

      Thanks for the support Freddie! 😎

  • @oveezion
    @oveezion 2 года назад

    Lowkey saved my uni lab work xd , keep up the good work !!

  • @martingogaming1777
    @martingogaming1777 3 года назад

    Thank you, I had trubles with sending message to multiple clients, because of reading system input, thanks again!

    • @WittCode
      @WittCode  3 года назад

      Hey there thanks so much! Yeah the reading input part tripped me up for a while too! 😃

  • @motodankz4838
    @motodankz4838 Год назад

    Wow you are suck a great teacher. You explain every step. Thank you so much!

  • @mosesfrancis4976
    @mosesfrancis4976 Год назад

    This is what, I looked for in Java. Thank you so.

  • @premkarki2
    @premkarki2 7 месяцев назад

    Thank you for the clear and concise explanation. I just subscribed you. I wish to watch your more videos.

  • @harrygabbi5181
    @harrygabbi5181 2 года назад +1

    great video thanks for making it i was stuck on this for so long

  • @bhpure30
    @bhpure30 6 месяцев назад

    great video, wonderful explanations throughout too.

  • @maxhitmemedi6598
    @maxhitmemedi6598 2 года назад +2

    You are a lifesaver man, thank you a lot you are saving me a ton of hours and nerves :)

  • @tungle3635
    @tungle3635 3 года назад

    thank you so much, this content deserves much more credit

    • @WittCode
      @WittCode  3 года назад

      Thanks so much! That means a lot! 😃

  • @danielgitonga9478
    @danielgitonga9478 4 дня назад

    Great video all the best

  • @borissmax3
    @borissmax3 Год назад

    Thank you! Learned a lot from this

  • @nadiiar75
    @nadiiar75 2 года назад +1

    Greetings from Ukraine! You are awesome! Thank you so much for this great tutorial. It helped me a lot.

  • @kevincianci5443
    @kevincianci5443 3 года назад +5

    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.

    • @WittCode
      @WittCode  3 года назад +3

      Thanks so much Kevin! And I will certainly be making a GUI version of this! Probably using JavaFX 😀

    • @vikaskotwani9573
      @vikaskotwani9573 2 года назад

      @@WittCode , hey witt please do that asap

  • @rnyah_
    @rnyah_ Год назад

    I'm so grateful to you, this really so helpful man thank you so much

  • @0brooo
    @0brooo 2 года назад

    I took a server side approach instead of a client side approach, love the video tho, thank you

  • @uzumakiuchiha7678
    @uzumakiuchiha7678 Год назад

    Nice one man. Keep it up!

  • @damiaotiktok023
    @damiaotiktok023 8 месяцев назад

    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

  • @feuerherz007
    @feuerherz007 2 года назад +2

    Hello sir. Is the code still on your website? i can only seem to see some Javascript. Is it on a github page?

  • @mc_mzcgt
    @mc_mzcgt 3 года назад

    Thank you so much!!! This is what I was finding!

  • @charlesjaussaud1822
    @charlesjaussaud1822 2 года назад

    your video is awesome and helped me a lot. thank you.

  • @g.722
    @g.722 2 года назад +1

    Very helpful! Thank you so much!

  • @vasantpatel9297
    @vasantpatel9297 2 года назад

    this is briliant my friends thank you so much

  • @haris7521
    @haris7521 2 года назад

    This is simply awesome man 👍❤️

  • @angelinawho5551
    @angelinawho5551 2 года назад

    You are lifesaver man, thank you a lot :)

  • @chetan788
    @chetan788 Год назад

    video is very helpful. Thank you.

  • @celerisBackendFramework
    @celerisBackendFramework 6 месяцев назад

    thanks a lot bro, keep the good job.✌

  • @arthurbakker740
    @arthurbakker740 6 месяцев назад

    Great content, learned a lot!

    • @bernardkingsley8598
      @bernardkingsley8598 6 месяцев назад

      Is there anyway I can see your code by chance, that is if it's working

  • @hvasomhelst
    @hvasomhelst 10 месяцев назад

    thank you so much, saved my homework truly!

  • @akalankaalutwela7712
    @akalankaalutwela7712 3 года назад

    Thank you very much for this!

  • @sirojiddingroup
    @sirojiddingroup 2 года назад

    Thanks bro it has been very usefull for me

  • @3umaryy
    @3umaryy 2 года назад +1

    Why we can't run the server again? After it being in use? Like turn off the server and Re-open it.

  • @Tygelin86
    @Tygelin86 3 месяца назад

    Great tutorial!

  • @ambawadisu2050
    @ambawadisu2050 2 года назад +1

    Thank you man, is that possible to chat only with one person from the group please share if you have such kind of code

  • @freddierobinson9587
    @freddierobinson9587 Год назад

    this was perfect for me thank you

  • @orabylaikhan
    @orabylaikhan 2 года назад

    nice tutuorial man and one silly question "can I connect to that server from another PC?" Or it works only local host?

  • @yashwanth7978
    @yashwanth7978 Год назад +2

    Can you share the code, not able to find in the link you kept in description

  • @sebastiangard18
    @sebastiangard18 2 года назад +1

    Is it a TCP or UDP based communication? Really good Video

  • @丅-u2h
    @丅-u2h Год назад +1

    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 !😍

  • @patsondamascus885
    @patsondamascus885 Год назад +2

    i tried it it works but my message dont appear to other clients it only appears in my chat only not other chats'

  • @m4rt_
    @m4rt_ 2 года назад +8

    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.

    • @XiamJoy
      @XiamJoy Год назад

      yah it sounds correct

  • @15iunie94
    @15iunie94 Год назад

    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)

  • @mihaioprea9398
    @mihaioprea9398 2 года назад

    Great video! Really helped!

  • @fabiansvensson9588
    @fabiansvensson9588 3 года назад +7

    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!

    • @ngocminhpham1358
      @ngocminhpham1358 2 года назад

      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

    • @fabiansvensson9588
      @fabiansvensson9588 2 года назад +1

      @@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.

    • @ngocminhpham1358
      @ngocminhpham1358 2 года назад

      @@fabiansvensson9588 thanks a lot!

    • @sankarphat
      @sankarphat 2 года назад

      @@fabiansvensson9588 Oh my god i was desperate while seaching the solution and boom i found your comment. You're a god.

    • @lara564
      @lara564 2 года назад +1

      at which line do you add this "if(messageFromClient == null) throw new IOException();" ?

  • @aladin7339
    @aladin7339 2 года назад +1

    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

  • @karolinemalko1723
    @karolinemalko1723 2 года назад

    thank you! Subscribed!

  • @Asswipe69
    @Asswipe69 3 года назад

    Nice tutorial man, rly need something like tihs :D

    • @WittCode
      @WittCode  3 года назад +2

      Thanks so much! Also, love your username lol

  • @replikvltyoutube3727
    @replikvltyoutube3727 2 года назад

    This helped me a lot, thanks!

  • @johnnys.828
    @johnnys.828 2 года назад

    Thank you very much!

  • @RNDev666
    @RNDev666 3 года назад +2

    tysm for this video! its great! one question tho.. at 21:14
    couldn't you check if this!=clientHandler?

    • @WittCode
      @WittCode  3 года назад +1

      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! 😄

    • @RNDev666
      @RNDev666 3 года назад

      @@WittCode yeah i was wondering abt duplicate usernames lol

  • @kurwa_mach
    @kurwa_mach 2 года назад

    Thank you very much, man

  • @bobfarker4001
    @bobfarker4001 Год назад

    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?

  • @sahilbhirud1301
    @sahilbhirud1301 3 года назад +1

    Love the thumbnail hahah

    • @WittCode
      @WittCode  3 года назад

      Sahil! Good to hear from you man! And yeah I thought it was the coolest one Ive made so far lol

  • @kutilkol
    @kutilkol 2 года назад +1

    beautiful! thanks! (do more java tutorials pls

  • @elifkaragoz2606
    @elifkaragoz2606 2 года назад +2

    Which design patterns are we using in this program and which pattern I can implement into the code in addition to develop the code?

  • @siradjeddinefisli1440
    @siradjeddinefisli1440 2 года назад +2

    the client message is not sent to all clients !!

  • @vishnusudarsanam5375
    @vishnusudarsanam5375 2 года назад

    Real gem!!

  • @mahamatmahamatabdoulaye893
    @mahamatmahamatabdoulaye893 2 года назад

    Many thanks Sir 😘

  • @ritchiemaegamot9460
    @ritchiemaegamot9460 7 месяцев назад

    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

  • @igormanolescu5910
    @igormanolescu5910 6 месяцев назад

    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

  • @kutilkol
    @kutilkol 2 года назад +1

    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.

  • @mnembachambuya3592
    @mnembachambuya3592 Год назад

    Thank you for good educating content. on 21:46, could we use if(this!=clientHandler) {}?

  • @long3850
    @long3850 2 года назад

    very helpful video !

  • @aladin7339
    @aladin7339 2 года назад

    thank uuuuu so much, this is so helpful

  • @morgard211
    @morgard211 Год назад +1

    Shouldn't we rather use AtomicReferenceArray for the clientHandlers to avoid race conditions since more thread access it?

    • @romanzdev
      @romanzdev 8 месяцев назад

      You are correct, use thread-safe collections or manage synchronization

  • @noobmaster0810
    @noobmaster0810 2 года назад +1

    can the client use anathor computer
    like LAN
    ???

  • @HAZARD_LIGHTS
    @HAZARD_LIGHTS 3 года назад +1

    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.

    • @WittCode
      @WittCode  3 года назад

      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! 😀

    • @HAZARD_LIGHTS
      @HAZARD_LIGHTS 3 года назад

      @@WittCode Thank you so much, I really appreciate you taking the time out of your day to respond to my request!

  • @AlexKayzProductions
    @AlexKayzProductions 2 года назад

    Great tutorial, thanks

  • @chillapavankumarkumar6577
    @chillapavankumarkumar6577 8 месяцев назад

    I got error at client.java public static void Socket socket= new Socket ("localhost",1234);
    I didn't getting host: and port: in above how to fix it

  • @wriottter
    @wriottter Год назад

    Helpful video thanks for making this. Can you do a server PC and a client mobile phone.

  • @elishaamenuveve4307
    @elishaamenuveve4307 3 года назад

    Great video!

  • @heshakarathnasekara7135
    @heshakarathnasekara7135 2 года назад

    awesome video i love it thankyouu

  • @wasiuddinbhuyian5081
    @wasiuddinbhuyian5081 2 года назад

    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?

  • @madonselakhumbulani6038
    @madonselakhumbulani6038 11 месяцев назад

    mine is refusing to connect to the server and when i check on my code everything is oky
    i don't if is it safer to change the port number or what?

  • @nikolapilipovic2552
    @nikolapilipovic2552 4 месяца назад

    can someone explain how to run this in CMD like he did in the beginning please?

  • @iopersonelweb
    @iopersonelweb 2 года назад

    Thanks! Can you help me? I can start the client, but the server are closing in few seconds

  • @salmatariq9839
    @salmatariq9839 Год назад

    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!

  • @ismailgunduz7
    @ismailgunduz7 2 года назад +1

    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

  • @danielluo6499
    @danielluo6499 2 года назад

    great channel

  • @ihsannuruliman3656
    @ihsannuruliman3656 2 года назад

    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.

  • @dabexter
    @dabexter 2 месяца назад

    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?

    • @Rohan-Prabhala
      @Rohan-Prabhala 2 месяца назад

      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

    • @dabexter
      @dabexter 2 месяца назад

      @@Rohan-Prabhala tysm, so i run the client script and not the client handler right?

    • @arunvasunny7386
      @arunvasunny7386 17 дней назад +1

      @@dabexter client handler works on the server side so that other clients can parallelly work without any error.

    • @dabexter
      @dabexter 17 дней назад

      @@arunvasunny7386 thank you

    • @arunvasunny7386
      @arunvasunny7386 17 дней назад

      @@dabexter have you completed your project?

  • @mige5fin
    @mige5fin 2 года назад

    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.

  • @greenman3846
    @greenman3846 2 года назад

    Hi. Thank you for your work. I have some questions. what if i want to send a private message. I will need to creat a new thread for chating with only this client?