Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将SSL流与异步System.Net.Sockets一起使用_Sockets_Ssl_Asynchronous_Stream_Certificate - Fatal编程技术网

将SSL流与异步System.Net.Sockets一起使用

将SSL流与异步System.Net.Sockets一起使用,sockets,ssl,asynchronous,stream,certificate,Sockets,Ssl,Asynchronous,Stream,Certificate,我的服务器现在正在使用Asynchronous 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;

我的服务器现在正在使用Asynchronous 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
            }
        }
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
Net系统;
使用System.Net.Sockets;
公共类包装器
{
公共字节[]缓冲区;
公共插座(u插座),;
公共对象连接器;
}
公共类WinSocket
{
专用字典连接;
public event Action AnnounceNewConnection;//事件处理程序
公共事件行动;
公共事件行动;
专用插座(u插座),;
public int MAX_USER_CONNECTIONS=2;//MAX USER CONNECTIONS
公共WinSocket(ushort端口)
{
尝试
{
连接=新字典();
_套接字=新套接字(AddressFamily.InterNetwork、SocketType.Stream、ProtocolType.Tcp);
_绑定(新的IPEndPoint(IPAddress.Any,port));
_插座。听(500);
_beginacept(AcceptConnections,new Wrapper());
}
捕获(例外e)
{
Console.WriteLine(e);//编写异常
}
}
专用void AcceptConnections(IAsyncResult结果)
{
尝试
{
Wrapper wr=result.asynchState作为包装;
wr._socket=_socket.EndAccept(结果);
#不可见区域
字符串IP=wr.\u socket.RemoteEndPoint.ToString().Split(“:”)[0]。ToString();//获取用户IP
如果(!Connections.ContainsKey(IP))
连接。添加(IP,1);
其他的

如果(连接[IP]将
类替换为类。除上述代码外,在
WinSocket
构造函数中调用并传递服务器SSL证书。

什么错误你得到了吗?除了问题中包含的内容之外,你还尝试过其他方法吗?我没有发现错误,我上面的代码正在工作,但我想在其中使用SSL来保护我的连接,我不知道如何。很抱歉,有一个小要求:你能突出显示需要编辑的部分,或者编辑我为我提供的代码吗?因为e我对SSL完全感到困惑请帮助我,我正在尝试编辑它,但我始终获取对象引用未设置为对象的实例