套接字通信协议 [英] Socket Communication Protocol
问题描述
创建通信协议的最佳方法是什么?
我需要发送命令,每个命令都将匹配某些参数.
接收方将根据命令执行某些操作.
我看过的所有示例仅发送字符串,我需要更强大的功能,所以我应该使用什么:类,结构,字典,枚举,数组?
关于什么是最佳方法有什么建议吗?
我正在使用System.Net.Sockets.TcpListener和System.Net.Sockets.TcpClient
我只想知道如何才能动态地创建和发送消息
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?
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
推荐答案
我认为最通用的解决方案是 ^ ].
但这取决于解决方案的复杂程度.更简单的解决方案就是交换XML文档.
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.
您可以使用多个级别.
请在我概述它们的地方查看我过去的答案:
如何将byte []发送到另一台PC [ ^ ],
在局域网上与两个Windows应用程序进行通信. [ ^ ].
最适合您的取决于您的要求.
—SA
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
Morph King,
它实际上取决于您将在应用程序中使用的协议.
对于 Tcp客户端协议,您可以编写以下类:
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;
}
}
}
另一个问题:
您要与电子设备(例如pic,dsPic)或计算机主机通信吗?
One more question:
Are you going to communicate with an electronic device (such as pic, dsPic) or a computer Host?
这篇关于套接字通信协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!