Socket Programming with Java NIO - Channels, Selectors, and Buffers

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ม.ค. 2025

ความคิดเห็น • 26

  • @energy-tunes
    @energy-tunes หลายเดือนก่อน +2

    excellent underrated channel.

    • @willtollefson
      @willtollefson  หลายเดือนก่อน

      Thanks, I appreciate it!

  • @DenaTollefson
    @DenaTollefson 8 หลายเดือนก่อน +5

    Fantastic socket programming tutorial, Will! You give fantastic examples and explain everything very clearly. 😀

    • @willtollefson
      @willtollefson  8 หลายเดือนก่อน +2

      Thank you! This is an interesting API for sure

  • @moncefabboud2839
    @moncefabboud2839 หลายเดือนก่อน

    Great job explaining this. Very informative and fun.

    • @willtollefson
      @willtollefson  หลายเดือนก่อน

      Thank you! Glad you liked it

  • @NatiShen
    @NatiShen 8 หลายเดือนก่อน +2

    I just love these Java socketing videos. You're the best!

    • @willtollefson
      @willtollefson  8 หลายเดือนก่อน +1

      You’re very welcome! Glad to hear you like them

  • @malloryranae8069
    @malloryranae8069 8 หลายเดือนก่อน +3

    Thanks for this great content! 🎉

  • @senorgriffin6405
    @senorgriffin6405 6 หลายเดือนก่อน +1

    you're awesome man, you should have way more suscribers and views

  • @adnanblanco4439
    @adnanblanco4439 3 หลายเดือนก่อน +1

    Thanks for this tutorial.

    • @willtollefson
      @willtollefson  3 หลายเดือนก่อน

      Glad you liked it! I thought this one was fun to make 🙂

  • @Daxin-m1c
    @Daxin-m1c 2 หลายเดือนก่อน +1

    Very nice, I use the same code, jdk 11, debug on Eclipse 4.24.0, process stops at selector.select(); Any suggestion?

    • @willtollefson
      @willtollefson  หลายเดือนก่อน

      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.

  • @submersedsoap1631
    @submersedsoap1631 7 หลายเดือนก่อน +2

    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?

    • @willtollefson
      @willtollefson  7 หลายเดือนก่อน +2

      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

    • @submersedsoap1631
      @submersedsoap1631 7 หลายเดือนก่อน +1

      @@willtollefson Thanks!

  • @tahidurrahman9285
    @tahidurrahman9285 7 หลายเดือนก่อน +1

    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();
    }

    • @willtollefson
      @willtollefson  7 หลายเดือนก่อน +1

      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!

  • @alxbudn
    @alxbudn 5 หลายเดือนก่อน +1

    can someone from another network join the chat with this method

    • @willtollefson
      @willtollefson  5 หลายเดือนก่อน

      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.