0

i am new in the socket programming in C/C++. I wrote Client and Server Socket. They can send and receive string. The idea is to send the commands to the server in the string. It can look something like this : "GET X" or "SET X 2". I know how i can split string. and the program know that the first word in the string is command, the second is attribute, and the third can be a value in case of SET-Command. My question is how i can say to programm that if it gets the string "GET X" it should call the function get(attribute). I thought the first about switch-case, but i think it's not a best solution for it. In case if it will be a lot of commands. THank you

3
  • 1
    You can be programming in either C or C++, not both at the same time as they are different languages. Please use the appropriate tags and specify exactly which language you are using. Commented May 4, 2011 at 19:23
  • 1
    You're basically implementing remote procedure calls. Unless you're doing this for educational purposes, why reinvent the wheel? There are many RPC technologies available in multiple programming languages. A brief list of those technologies is available here: en.wikipedia.org/wiki/Remote_procedure_call Commented May 4, 2011 at 19:47
  • @Seth Carnegie Technically it is possible to include C files in a C++ project, or perhaps the server and client are in different languages. Neither of those things may be the case here, but those are possibilities. Commented May 4, 2011 at 20:34

3 Answers 3

1

You can basically boil the problem down to the optimal way to map an operation name to the actual function being invoked, and whether or not solutions with better lookup characteristics are really needed.

  • For a small number of functions a simple O(n) based solution (e.g. linear search) could be sufficient.
  • An O(log(n)) based approach (e.g. self-balancing binary trees) could be sufficient for more operations, as well.
  • Most folks jump straight to hash table based solution because of the theoretical O(1) lookup characteristics. However, that depends on a good hash function, which is itself not free in terms of cost.

There are always time and space trade-offs involved. So its really best to profile before deciding on which approach is best for your application.

Regardless, these sorts of operation dispatch problems have been researched heavily. For example, here's one paper that discusses operation dispatch strategies in a CORBA C++ ORB implementation.

Besides optimizing operation lookup, you'll likely have to deal with other factors such as:

  • Concurrency: is your application required to handle multiple connections from clients in parallel? Distributed concurrency is often very difficult to implement correctly.
  • Serialization: how will you send and receive data over-the-wire? Problems you could encounter include: sending plain text could be slow, numerical values may have to be converted to the receiving platforms endianness, your serialization protocols could change which would affect your users, etc.

There are potentially many other problems you'll have to deal with on the client side as well. If at all possible, I'd recommend going with an existing RPC-like technology rather than reinvent the wheel yourself.

Sign up to request clarification or add additional context in comments.

Comments

0

I think it's best to use a switch statement.

If not you can use a map with the command string as key and a pointer to the callback function as a value.

1 Comment

And be sure to check that the key exists, otherwise your program might explode.
0

One way you could do it is have a lookup table of functions that you could call depending on the command which all returned a char*, and send the response back to the client. You'd have to make a rudimentary hashing function of course.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.