In my client-server architecture, the cilent-side application would send specific messages to the server-side application.
How should i handle those messages?
Previously, in a small project, i had just passed the received message to a method where it would process the message through a switch case structure.
However, this doesnt seem feasible as this is a much bigger project.
Can anyone provide me a better alternative.
I have thought of using delegate. However, i would rather wait for some suggestions. :)

Recommended Answers

All 7 Replies

Which technology you use? WCF or just socket programming what's the importance of data? all of these questions determine the best practice of implementing passing messages.

Which technology you use? WCF or just socket programming what's the importance of data? all of these questions determine the best practice of implementing passing messages.

i am developing it in C#.NET, using the helper classes - TcpClient, TcpListener. (i mean no direct use of Socket).
Also i am using asynchronous method of communication.
The project i am developing is a client monitoring application. Hence, the data is much more important. They are the messages, rather commands that are sent by the server-side application to the client-side application.
For example, if i would like to start keylogging on some client PC, i would hit a button on my server application, sending a command to start keylogger module on that client.
Also, then the client would send the whole log through a stream to the server.
Thus, its a two-way communication.
I think this would suffice. If you wish any more information though, you may ask. :)

You should link in your other thread for future questions on this project to give people a good background on what you're doing without having to retype it all :)

Take a look at what HTTP, FTP, or many other protocols do. They assign numeric values for a command and then have metadata beyond that.

Just switch() the int off to the desired logic. This also allows you to implement new commands on one side and not the other, for unknown ints you can return a "command not understood" so you know that its' software needs to be updated.

<- Microsoft FTP Service
-> USER *
<- 331 Password required for upload.
-> PASS *
<- 230 User * logged in.
-> QUIT
<- 221

HTTP:
404 - not found
403 - forbidden
301 - resource moved perm.
302 - resource moved temp.

I've sure you have seen those before

Oh.. one more point I wanted to mention. Enum's are great for this so you don't have to memorize the command codes. I use numerics exactly as I described above for IPC between windows services and their desktop configuration apps.

You can cast an enum to/from an int, but watch out when you are doing int->enum as it will return a number even if the enum doesn't explicitly exist for it. I don't have visual studio handy but you can set enums:

public enum Commands
{
  StartKeyLogger = 1,
  StopKeyLogger = 2,
}

Then use Enum.IsDefined(typeof(Commands), someInt); before you convert the int to enum.

Oh.. one more point I wanted to mention. Enum's are great for this so you don't have to memorize the command codes. I use numerics exactly as I described above for IPC between windows services and their desktop configuration apps.

You can cast an enum to/from an int, but watch out when you are doing int->enum as it will return a number even if the enum doesn't explicitly exist for it. I don't have visual studio handy but you can set enums:

public enum Commands
{
  StartKeyLogger = 1,
  StopKeyLogger = 2,
}

Then use Enum.IsDefined(typeof(Commands), someInt); before you convert the int to enum.

ohk..yup..i know the enum type.
I thought to follow the same method. Indeed, i would follow what you suggested.
i will just use the switch case to direct the command to some function that handles that command. You may correct me though if i am wrong. :)
And yaa..i will link up all my posts as you said.
But what did you mean by that? should i just put up my links to other posts in any of my upcoming posts ? :)
Or is there any special facility here? i couldng find any.

What I meant was in the original post you went in to great detail about what the application does and provided examples of the commands you planned on implementing.

The original post in this thread was not enough information and had I not been involved with your original post I would have asked the same question Ramy did. *What* you are doing guides *how* you will do it, so you should give the same background on what you are doing for questions like this.

Its' all for your benefit so you can get your posts answered quicker and with better insight :)

Good luck and please mark this thread as solved if your question has been answered.

What I meant was in the original post you went in to great detail about what the application does and provided examples of the commands you planned on implementing.

The original post in this thread was not enough information and had I not been involved with your original post I would have asked the same question Ramy did. *What* you are doing guides *how* you will do it, so you should give the same background on what you are doing for questions like this.

Its' all for your benefit so you can get your posts answered quicker and with better insight :)

Good luck and please mark this thread as solved if your question has been answered.

yup..i got it...i will link in the posts from now. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.