Click here to Skip to main content
11,720,785 members (67,681 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 socket
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 8-Jun-11 7:33am
Edited 8-Jun-11 21:35pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
SAKryukov at 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 at 8-Jun-11 15:06pm
   
Thanks SA
Morph King at 8-Jun-11 15:13pm
   
time, speed and delay are are the most important factors, i need to send only some commands and parameters
SAKryukov at 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 at 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
Kim Togo at 9-Jun-11 3:43am
   
I would go for XML, and make the XML very slim and compact. Easy to add more info to and easy to handle.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Kim Togo at 8-Jun-11 15:07pm
   
Good answers SA. My 5.
SAKryukov at 8-Jun-11 15:10pm
   
Thank you, Kim.
--SA
Un_NaMeD at 8-Jun-11 15:23pm
   
Really nice works SA.. My 5, too..
SAKryukov at 8-Jun-11 15:33pm
   
Thank you.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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?
  Permalink  
v3
Comments
Morph King at 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 at 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 at 9-Jun-11 3:22am
   
yes but before it became bytes? i need a suggestion on how to create the application protocol
Un_NaMeD at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 2,179
1 OriginalGriff 924
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540


Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 9 Jun 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100