我正在编写一个客户机服务器应用程序,用于计算机实验室并充当服务(而不是作为服务运行)。我有一个控制台应用程序使用控制台的HWND对象调用本机函数"ShowWindow"/SW_HIDE --这给了它我在这里想要的东西。服务器/客户端正在工作,我发送了消息"Hello!“从客户端到服务器多次,我很高兴。(我使用UDP作为套接字协议,因为IT部门需要一种无连接的方法。)
我的问题在于客户机-服务器.之间的通信协议。
服务器背后的目标包括:
有些事情我想包括的是简单的问题被来回发送:
我毫不怀疑我能做到这一点。我现在最关心的是数据安全。在我的学院里,我们确实有一些“黑客”,他们可能知道如何将数据包发送到特定的服务器,或者做恶意的事情,或者获取敌人的信息等等。
我的想法是对发送的所有数据提供一个加密方案,并在接收时对其进行解码。
与我交谈过的一位朋友说,我应该使用一些packer,然后我开始将他的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
}
}我希望问题是清楚的?我注意到我写的大部分问题都不清楚。:/
发布于 2009-11-13 21:47:19
来自SslStream的System.Net.Security类可能会满足您的需要
(SslStream)提供一个用于客户端-服务器通信的流,该流使用安全套接字层(SSL)安全协议对服务器进行身份验证,并可选地验证客户端。
..。
SSL协议有助于为使用SslStream传输的消息提供机密性和完整性检查。在客户端和服务器之间通信敏感信息时,应该使用SSL连接(如SslStream提供的连接)。使用SslStream有助于防止任何人在网络传输时阅读和篡改信息。
这里的更多细节和示例:SslStream类
https://stackoverflow.com/questions/1731773
复制相似问题