Question about sockets in java. I have one socket server and multiple socket clients connected to that socket server. How to communicate between socket clients when they connect to same socket server? Is there some neat library to handle that socket server/client interactions? Googled for hours cant find a decent solution

  • 0
    Personally I would store each client in memory and asign them an ID so that you can distinguish them.

    Not sure if it's the best way, but that's how I've done it in the past.

    Edit: not Java specific but I don't see why that wouldn't work in Java
  • 0
    @GCHQ Don't use ids, use object references
  • 0
    What you are asking has been catered by libraries like Netty, MINA

    What exactly are you trying to achiever here?
  • 0
    @asgs I am trying to pass a linux command from one socket client to another socket client.
  • 1
    Between clients?Then your server must be a router. Hashmap sockets' remote adresses [addr:port] with sockers themselves in the server. Or use any other key, idc. The key should be smth you are using to distinguish the target sock client in the src sock client.

    P.S. Why sockets, not channels?
  • 0
    @12bitfloat I guess sock_src should be able to tell which sock_dst should receive the message. Using refs would look somewhat weird :) but would work well if the server is there to just broadcast.
  • 0
    @netikras Why? ServerSocket.accept() -> instantiate ClientConnection object that holds the Socket and represents the connection. No id needed. Except if he's doing some sort of authentication
  • 1
    @12bitfloat there are 5 clients connected to the ServerSocket: A B C D E. And I want to send a message from A to D. Not from SERVER to CLIENT(D) but from CLIENT(A) to CLIENT(D). And the next message CLIENT(A) wants to send is addressed to CLIENT(B). And so on.

    I'm just assuming this is what the OP wants.
  • 2
    The easiest (but probably not the most scalable one) is to do a new Thread for each accept'ed socket.
    Store the accept'ed socket in a Hashmap with the source address as a key.

    In the thread, wait for incoming data, parse it (e.g. target client and command) and send it to the correct socket by looking the address up in your Hashmap from earlier.

    Obviously, make the access to the Hashmap threadsafe.
  • 0
    @netikras yes exactly I want to send message from one client to another like a chat messaging service. But in this case client A will trigger client B to make a linux command for example add iptables entry on client B’s machine.
  • 1
    @zemaitis Is remote code execution important or is that more of a massive security vulnerability?
  • 0
    @zemaitis then yeah, work out the addressing you want to use [A B C and other letters might run out fairly quickly if you have >30 clients :) and they're too vague]. Upon connection to server map address to the socket/channel, store it in a map [hashmap would do, unless you introduce a critical section there] in the server. There should prolly be a discovery mechanism so clients would know which other clients are online and what are their addresses. Like GET /peers.

    And since the socket now has a few purposes, introducing a custom protocol might be helpful.

    Btw be careful with low-level sockets. You can never know whether you will transfer a complete message in one go or not. And if not - packets from different sources might mix up [the receiving client always receives messages from the same IP -- server. HA! You didn't think of that, did you! ;) ].

    Why not go with lightweight REST servers/clients? Imo it's less of a headache since http operates in complete messages. Or maybe websockets as medium.
  • 0
    @netikras For the love of god no custom protocol. We all know that can only end in suicidal thoughts
  • 0
Add Comment