0

I'm writing a "Instant-Messaging software" using TCP server / client model using Java's socket API and I'm wondering what's the recommended way to call methods/functions of my clients from my server.

Currently I'm looking at sending a string over to my clients with a 3 characters in front of the string to allow my clients to determine what type of message it is.

For example:

  1. Server sends a string, "MSGhelloworld" to its clients
  2. Client splits the string into "MSG" and "helloworld"
  3. Client executes the 3 character command, "MSG" using a switch statement
  4. Then Client executes the corresponding method/function with "MSG" determining what method to execute and "helloworld" being the parameter for the method

I'm opened for any other recommended solutions and any UDP solutions as well, thank you.

CatsGalore
  • 21
  • 1
  • 5
  • How are you expecting the server to know how long the string is (i.e. when it's finished reading it)? Don't forget that if you're using TCP, that's a stream protocol - the only kind of "data finished" you can spot is when the connection is closed. Consider using a length prefix. Also, explicitly decide which encoding you want to use. (I'd recommend UTF-8.) – Jon Skeet Feb 28 '14 at 08:02
  • what do you mean? I'm using a BufferedReader to read client's input, whenever my client sends a command my server receives it as a whole using the readLine() method, why do I even need to check the length of the string? – CatsGalore Feb 28 '14 at 08:20
  • Ah, so you're using a line break as an "end of message" token. You hadn't mentioned that. It does mean you can't send more than one line in a message though. – Jon Skeet Feb 28 '14 at 08:21
  • 1
    Sounds like a case for RMI to me. – user207421 Feb 28 '14 at 08:36
  • comparison between RMI and my above method? – CatsGalore Mar 01 '14 at 04:27

0 Answers0