我的服务器现在使用异步System.Net.Sockets运行,但我想使用SSL流。我对使用SSL很陌生,所以如果有人能帮助我,这里是我的服务器代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
public class Wrapper
{
public byte[] buffer;
public Socket _socket;
public object connector;
}
public class WinSocket
{
private Dictionary<string, byte> Connections;
public event Action<Wrapper> AnnounceNewConnection;//Event Handlers
public event Action<Wrapper> AnnounceDisconnection;
public event Action<byte[], Wrapper> AnnounceReceive;
private Socket _socket;
public int MAX_USER_CONNECTIONS = 2;//Max User Connections
public WinSocket(ushort port)
{
try
{
Connections = new Dictionary<string, byte>();
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.Bind(new IPEndPoint(IPAddress.Any, port));
_socket.Listen(500);
_socket.BeginAccept(AcceptConnections, new Wrapper());
}
catch (Exception e)
{
Console.WriteLine(e);//write an exception
}
}
private void AcceptConnections(IAsyncResult result)
{
try
{
Wrapper wr = result.AsyncState as Wrapper;
wr._socket = _socket.EndAccept(result);
#region Invisible
string IP = wr._socket.RemoteEndPoint.ToString().Split(':')[0].ToString();//Get user ip
if (!Connections.ContainsKey(IP))
Connections.Add(IP, 1);
else
if (Connections[IP] <= MAX_USER_CONNECTIONS)//Maximum Connections Per IP
{
byte connections = Connections[IP];
Connections.Remove(IP);//Limit exceeded
Connections.Add(IP, (byte)(connections + 1));
}
else
{
wr._socket.Disconnect(false);
_socket.BeginAccept(AcceptConnections, new Wrapper());
return;
}
#endregion
wr.buffer = new byte[65535];
wr._socket.BeginReceive(wr.buffer, 0, 65535, SocketFlags.None, ReceiveData, wr);
AnnounceNewConnection.Invoke(wr);
_socket.BeginAccept(AcceptConnections, new Wrapper());
}
catch (Exception e)
{
Console.WriteLine(e);//write an exception
}
}
private void ReceiveData(IAsyncResult result)//Receiving Data
{
try
{
Wrapper wr = result.AsyncState as Wrapper;
string IP = wr._socket.RemoteEndPoint.ToString().Split(':')[0].ToString();//Get UIP
if (Connections.ContainsKey(IP))
{
SocketError error = SocketError.Disconnecting;
int size = wr._socket.EndReceive(result, out error);
if (error == SocketError.Success && size != 0)
{
byte[] buffer = new byte[size];
Buffer.BlockCopy(wr.buffer, 0, buffer, 0, size);
AnnounceReceive.Invoke(buffer, wr);//The delegate
if (wr._socket.Connected)//Make sure socket is connected
wr._socket.BeginReceive(wr.buffer, 0, 65535, SocketFlags.None, ReceiveData, wr);//Start Receiving Data
}
else
{
if (wr._socket.Connected)
{
wr._socket.Disconnect(true);//Disconnect the client
}
byte connections = Connections[IP];
Connections.Remove(IP);
Connections.Add(IP, (byte)(connections - 1));
try
{
AnnounceDisconnection.Invoke(wr);
}
catch { }
}
}
}
catch (Exception e)
{
Console.WriteLine(e);//write an exception
}
}
}
因此,我的问题再次明确:如何将SSL流与套接字类一起使用,如上面的代码
将Stream
类替换为System.Net.SslStream类。除了上面的代码外,还可以调用AuthenticateAsServer并在WinSocket
构造函数中传递服务器SSL证书。