0

I'm sending messages using java.net. And the server part works with many clients and depending on from what client I receive the message I choose the way to process the message. As I think I need to encolose the username to the message and then get it from there making the rest of the message as it was and sending it for the proceissing. How to do this? How such issues are usually resolved?

Nikitin Mikhail
  • 2,983
  • 9
  • 42
  • 67
  • The answer can be found [here][1]. It is very close [1]: http://stackoverflow.com/questions/3736058/java-object-to-byte-and-byte-to-object-converter-for-tokyo-cabinet – Nikitin Mikhail Jul 23 '13 at 13:36

1 Answers1

1

When you then receiver data, you can identify the client based on the sender's IP-address. If you still need the clientname, then send it as part of the first message after opening the connection. You can then relate the clientname with its IP-address.

Mike de Dood
  • 393
  • 1
  • 10
  • I've thought about identifying clients according to IP-addresses but it is possible that some user can run the client app on the computer at offise and in the evening run it on the computer which is placed at home. And if I send it as a part of the message how could I get it from the message and then pass the rest of the message(which must have a strong stucture) to the processing? – Nikitin Mikhail Jul 22 '13 at 13:21
  • As I said, you send the clientname as part of the first message after opening the connection to the server and then relate the clientname with the IP-address. Don't forget, that when the connection gets closed, this IP-address might not be valid anymore. So when a connection gets closed (is ended) you have to drop the current relation between username and IP-address again. To simplify the task of identifying what is the username, just send a single message first, containing only the username. And not as part of other data. – Mike de Dood Jul 22 '13 at 13:24
  • yes that's right. But I need to check every message because I have several clients and only one server. And messages comes to the server chaotically which means that I don't know from who this message is – Nikitin Mikhail Jul 22 '13 at 13:38
  • Not sure what protocol you are using, but for TCP you have the sender (since one TCP-connection per client) and with UDP you have the sender IP-address with every message you receive. In case you need to take care of multiple connections from a single computer, then use the sender's port number to identify the connection. – Mike de Dood Jul 22 '13 at 13:43
  • I understand that but what if the user wants his message to be processed according to his rules, but changes the IP address? – Nikitin Mikhail Jul 22 '13 at 13:46
  • So you have a relation between the username and the rules to apply. When the connection get established (at the server), the first message you receive contains the username. Now you know what rule to apply for this connection. And this you have to apply every time a connection starts. – Mike de Dood Jul 22 '13 at 13:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33912/discussion-between-kamikaze-lux-and-nikitin-mikhail) – Mike de Dood Jul 22 '13 at 13:53