我正在编写一个客户端 - 服务器应用程序,用于计算机实验室并充当服务(不作为服务运行)。我有一个控制台应用程序使用控制台的HWND对象调用本机函数“ShowWindow”/ SW_HIDE - 这给了它我想要的东西。服务器/客户端正在工作,我发送了消息“Hello world!”从客户端到服务器很多次,我很高兴。 (我使用UDP作为套接字协议,因为IT部门需要无连接方法。)
我的问题在于客户端 - 服务器之间通信的“协议”。
服务器背后的目标包括:
我想要包含的一些事情是来回发送的简单问题:
我毫不怀疑我能做到这一点。我现在所知道的是数据安全。在我的大学里,我们确实有一些“黑客”,他们可能知道如何分组嗅探并能够将数据包重新发送到特定服务器,以便做恶意事情或获取有关敌人或其他信息的信息。
我的想法是为所有发送的数据提供加密方案,并在接收时对其进行解码。
我与之交谈过的朋友说我应该使用一个打包器,然后我开始将他的BitPacker类从C ++移植到C#,我很困惑,来到这里看看Stackoverflow的想法。
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
}
}
P.S。我希望问题很清楚?我注意到我写的大部分问题都不清楚。 :/
答案 0 :(得分:2)
System.Net.Security中的SslStream类可能会满足您的需求
(SslStream)提供用于的流 使用的客户端 - 服务器通信 安全套接字层(SSL)安全性 用于验证服务器的协议 以及可选的客户。
...
SSL协议有助于提供 机密性和完整性检查 用于传输的消息 SslStream。 SSL连接,例如 由SslStream提供的,应该是 在沟通敏感时使用 客户与客户之间的信息 服务器。使用SslStream有助于 阻止任何人阅读和 篡改信息 在网络上传输
此处提供了更多详细信息和示例:SslStream Class