C# 客户机-服务器数据加密及协议设计
我正在编写一个在计算机实验室中使用的客户机-服务器应用程序,并作为服务(而不是作为服务运行)。我有一个控制台应用程序,它使用控制台的HWND对象调用本机函数“ShowWindow”/SW_HIDE——这就是我想要的。服务器/客户端正在工作,我已经多次从客户端向服务器发送消息“Hello world!”,我很高兴。(我使用UDP作为套接字协议,因为IT部门需要一种无连接的方法。) 我的问题在于客户端与服务器之间的通信“协议”。 服务器背后的目标包括:C# 客户机-服务器数据加密及协议设计,c#,encryption,client-server,bit-packing,C#,Encryption,Client Server,Bit Packing,我正在编写一个在计算机实验室中使用的客户机-服务器应用程序,并作为服务(而不是作为服务运行)。我有一个控制台应用程序,它使用控制台的HWND对象调用本机函数“ShowWindow”/SW_HIDE——这就是我想要的。服务器/客户端正在工作,我已经多次从客户端向服务器发送消息“Hello world!”,我很高兴。(我使用UDP作为套接字协议,因为IT部门需要一种无连接的方法。) 我的问题在于客户端与服务器之间的通信“协议”。 服务器背后的目标包括: 以编程方式访问我们的IT部门为了安全而阻止的
- 以编程方式访问我们的IT部门为了安全而阻止的某些功能(例如“net.exe”)
- 允许访问我的程序以监视学生在计算机实验室中查看的内容
- “REQUSER”命令将返回用户名和全名(之前“net user”允许)
- “REQPROCS”命令将返回当前用户用户名下正在运行的进程列表
namespace Atlantis.Net.Sockets
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
public class UdpServer : System.Net.Sockets.UdpClient
{
#region Constructor(s)
public UdpServer(Int32 port)
: base(port)
{
}
public UdpServer(IPEndPoint endPoint)
: base(endPoint)
{
}
#endregion
#region Properties
private Int32 m_Backlog = 32;
/// <summary>
/// Sets how many active connections the socket can support
/// </summary>
public Int32 Backlog
{
private get
{
return m_Backlog;
}
set
{
m_Backlog = value;
}
}
private Boolean m_IsInitialized = false;
/// <summary>
/// Gets a value indicating whether the server has been initialized
/// </summary>
public Boolean IsInitialized
{
get
{
return m_IsInitialized;
}
private set
{
m_IsInitialized = value;
}
}
private Int32 m_Port = 1337;
/// <summary>
/// Sets the port number for listening for incoming connections
/// </summary>
public Int32 Port
{
private get
{
return m_Port;
}
set
{
m_Port = value;
}
}
private Encoding m_Encoding = Encoding.ASCII;
/// <summary>
/// Gets or sets the text encoding for data being transferred to/from the server
/// </summary>
public Encoding Encoding
{
get
{
return m_Encoding;
}
set
{
m_Encoding = value;
}
}
#endregion
#region Events
public event EventHandler<UdpReceiveEventArgs> DataReceive;
#endregion
#region Methods
protected virtual void OnDataRecieve(String data, object state)
{
if (DataReceive != null)
{
DataReceive(this, new UdpReceiveEventArgs(data, ((UdpState)state)));
}
}
private void DataReceiveCallback(IAsyncResult ar)
{
UdpClient u = (UdpClient)((UdpState)ar.AsyncState).host;
IPEndPoint e = (IPEndPoint)((UdpState)ar.AsyncState).endPoint;
Byte[] data = u.EndReceive(ar, ref e);
OnDataRecieve(Encoding.GetString(data), ((UdpState)ar.AsyncState));
UdpState state = new UdpState();
state.endPoint = new IPEndPoint(IPAddress.Any, Port);
state.host = u;
u.BeginReceive(new AsyncCallback(DataReceiveCallback), ((UdpState)ar.AsyncState));
}
/// <summary>
/// .
/// </summary>
public void Initialize()
{
if (IsInitialized)
{
return;
}
//Debug.WriteLine(String.Format("Local address and port : {0}", Client.RemoteEndPoint.ToString()));
UdpState state = new UdpState();
state.endPoint = new IPEndPoint(IPAddress.Any, Port);
state.host = this;
BeginReceive(new AsyncCallback(DataReceiveCallback), state);
IsInitialized = true;
}
#endregion
}
}
namespace Atlantis.Net.Sockets
{
使用制度;
使用System.Collections.Generic;
使用系统诊断;
使用System.Linq;
Net系统;
使用System.Net.Sockets;
使用系统文本;
使用System.Windows.Forms;
公共类UdpServer:System.Net.Sockets.UdpClient
{
#区域构造函数
公共UdpServer(Int32端口)
:基本(端口)
{
}
公共UdpServer(IPEndPoint端点)
:基准(端点)
{
}
#端区
#区域属性
私有Int32 m_Backlog=32;
///
///设置套接字可以支持的活动连接数
///
公共Int32积压
{
私人获取
{
返回m_积压;
}
设置
{
m_Backlog=价值;
}
}
私有布尔m_初始化=false;
///
///获取一个值,该值指示服务器是否已初始化
///
公共布尔初始化
{
得到
{
返回m_已初始化;
}
专用设备
{
m_i初始=值;
}
}
专用Int32 m_端口=1337;
///
///设置用于侦听传入连接的端口号
///
公共Int32端口
{
私人获取
{
返回m_港;
}
设置
{
m_端口=值;
}
}
私有编码m_Encoding=Encoding.ASCII;
///
///获取或设置向服务器传输或从服务器传输的数据的文本编码
///
公共编码
{
得到
{
返回m_编码;
}
设置
{
m_编码=值;
}
}
#端区
#地区活动
公共事件事件处理程序DataReceive;
#端区
#区域方法
受保护的虚拟void OnDataRecieve(字符串数据、对象状态)
{
if(DataReceive!=null)
{
DataReceive(这是新的UdpReceiveEventArgs(数据,((UdpState)状态));
}
}
私有void DataReceiveCallback(IAsyncResult ar)
{
UdpClient u=(UdpClient)((UdpState)ar.AsyncState).host;
ipendpointe=(IPEndPoint)((UdpState)ar.AsyncState);
字节[]数据=u.EndReceive(ar,参考e);
OnDataRecieve(Encoding.GetString(数据),((UdpState)ar.AsyncState));
UdpState状态=新的UdpState();
state.endPoint=新的IPEndPoint(IPAddress.Any,端口);
state.host=u;
u、 BeginReceive(新的AsyncCallback(DataReceiveCallback),((UdpState)ar.AsyncState));
}
///
/// .
///
公共无效初始化()
{
如果(已初始化)
{
返回;
}
//WriteLine(String.Format(“本地地址和端口:{0}”,Client.RemoteEndPoint.ToString());
UdpState状态=新的UdpState();
state.endPoint=新的IPEndPoint(IPAddress.Any,端口);
state.host=this;
BeginReceive(新的异步回调(DataReceiveCallback),状态);
IsInitialized=true;
}
#端区
}
}
还有,我希望问题很清楚?我注意到我写的大多数问题都不清楚/ System.Net.Security中的SslStream类可能会满足您的需要 (SslStream)提供用于 使用 安全套接字层(SSL)安全性 验证服务器的协议 以及可选的客户端 SSL协议有助于提供 机密性和完整性检查 对于使用 SSL流。SSL连接,例如 由SslStream提供,寿