Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the best way to create a communication protocol?
I need to send commands, each command will match certain parameters.
the receiver will do some action based on command.
All the examples i've seen send only string, i need something more powerfull so what should i use: class, struct, dictionary, enum, array?
Any suggestions on what the best approach?

EDIT:
I'm using System.Net.Sockets.TcpListener and System.Net.Sockets.TcpClient
I want only to know how i can dinamically create and send my messages
Posted
Updated 8-Jun-11 21:35pm
v3

I think the most versatile solution is Windows Communication Foundation[^].
But it depends on how complex the solution is. More simple solutions is just to exchange a XML document.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jun-11 14:51pm    
In many cases, yes, but it really depends on application and requirements. (I voted 5.)
Please see my answer.
--SA
Kim Togo 8-Jun-11 15:06pm    
Thanks SA
Morph King 8-Jun-11 15:13pm    
time, speed and delay are are the most important factors, i need to send only some commands and parameters
Sergey Alexandrovich Kryukov 8-Jun-11 18:17pm    
Maybe the optimal level will be TcpClient/TcpListener. If you use serialization, it can also be a bottleneck. You need fast binary serialization.
--SA
Morph King 9-Jun-11 3:27am    
i'm not talking about connection, i need to know which is better to use and to serialize between: class enum array dictionary
There are several levels you can use.

Please see my past answer where I overview them:
how i can send byte[] to other pc[^],
Communication b/w two Windows applications on LAN.[^].

What's the best for you really depends on your requirements.

—SA
 
Share this answer
 
Comments
Kim Togo 8-Jun-11 15:07pm    
Good answers SA. My 5.
Sergey Alexandrovich Kryukov 8-Jun-11 15:10pm    
Thank you, Kim.
--SA
Un_NaMeD 8-Jun-11 15:23pm    
Really nice works SA.. My 5, too..
Sergey Alexandrovich Kryukov 8-Jun-11 15:33pm    
Thank you.
--SA
Hi Morph King,

it actually depends on the protocol which you will use in your application.

For Tcp Client Protocol you may write a class as below:

class Connection
{
    // Connection variable
    private TcpClient Connection { get; set; }

    // Network communication.
    private NetworkStream ns { get; set; }

    // Connection is done or not.
    public bool isConnection{ get; set; }

    // Buffer for receiving/sending bytes.
    private byte[] BytesToSend, bytesReceived;

    // Connection constructor.
    public Baglanti(string hostname, string port)
    {
        // Take IP adress ve Port number.
        // Make a connection.
        baglanti = new TcpClient(hostname, int.Parse(port));

        if (baglanti.Connected) // if connected.
        {
            ByteReceived= new byte[baglanti.ReceiveBufferSize];
            ns = Connection.GetStream();

            isConnection = true;
        }
    }

    // Method for connection
    public bool Baglan(string hostname, string port)
    {
        // if is Connected.
        if (isConnection)
            return isConnection;
        // Test Connection
        try
        {
            Connection = new TcpClient(hostname, int.Parse(port));
            if (Connection.Connected)
            {
                ByteReceived = new byte[Connection.ReceiveBufferSize];
                ns = Connection.GetStream();
                isConnection = true;
                return true;
            }
        }
        // if no connection
        catch (Exception)
        {
           isConnection = false;
        }
        return isConnection;
    }

    // Cut off the Connection
    public void CutOffConnection()
    {
        ns.Close();
        Connection.Close();
        isConnection = false;
    }

    // Sending Data
    public bool SendData(string s)
    {
        if (!Connection.Connected)
        {
            isConnection = false;
            return false;
        }
        try
        {
            BytesToSend = System.Text.Encoding.ASCII.GetBytes(s);
            ns.Write(BytesToSend, 0, BytesToSend.Length);
            ns.Flush();
            return true;
        }
        catch (Exception)
        {
            baglantivar = false;
            return false;
        }
    }

    // Getting Data
    public bool GetData(ref string ServerResponse)
    {
        if (!isConnection.Connected)
        {
            isConnection = false;
            return false;
        }
        try
        {
            ns.Read(ByteReceived, 0, Conneciton.ReceiveBufferSize);
            ServerResponse = System.Text.Encoding.ASCII.GetString(ByteReceived );
            return true;
        }
        catch (Exception)
        {
            isConnection = false;
            return false;
        }
    }
}


One more question:
Are you going to communicate with an electronic device (such as pic, dsPic) or a computer Host?
 
Share this answer
 
v3
Comments
Morph King 8-Jun-11 15:15pm    
I've done the "connection part" i need to know how to compose the message, i need something more powerfull than strings
Un_NaMeD 8-Jun-11 15:24pm    
What do you mean by "composing the message". Aren't you sending/receiving bytes?
I don't get it..
Morph King 9-Jun-11 3:22am    
yes but before it became bytes? i need a suggestion on how to create the application protocol
Un_NaMeD 9-Jun-11 13:27pm    
"yes but before it became bytes?" ?!
"... create the application protocol" which app.? Client app. or server app?

My friend, I really don't understand what you exactly mean.
Wish you find a solution..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900