Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
GCHQ2775yPersonally 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 -
asgs112755yWhat you are asking has been catered by libraries like Netty, MINA
What exactly are you trying to achiever here? -
@asgs I am trying to pass a linux command from one socket client to another socket client.
-
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? -
@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.
-
@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
-
@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. -
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. -
@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.
-
@zemaitis Is remote code execution important or is that more of a massive security vulnerability?
-
@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. -
@netikras For the love of god no custom protocol. We all know that can only end in suicidal thoughts
Related Rants
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
rant
question