-1

私はソケットのチュートリアルを通して自分の道を進んでいますが、問題が発生しています。私は3つのクラスを持っています。サーバー、サーバークライアント、およびクライアント。Server は接続要求をリッスンし、ServerClient はサーバー側のソケット ラッパーであり、Client は接続を試みるユーザー側のプログラムです。

私が直面している問題は、サーバーが受け入れられたソケットを ServerClient クラスに渡すと、ソケットが切断されることです。渡されたソケットをデバッグすると Connected = True になりますが、ServerClient でローカル変数を設定すると Connected = False になります。

クライアント側では、問題に直面していませんが、ソケットが接続されたままであることを知っています。

私のコードのどこが悪いのでしょうか?

サーバ:

    public async void Run()
    {
        //
        if (AcceptingClients)
        {
            // Error already running
            return;
        }

        //
        LoadSettings();

        //
        IPEndPoint localEndPoint = new IPEndPoint(_settings.Ip, _settings.Port);

        // Start The Server
        _server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        _server.Bind(localEndPoint);
        _server.Listen(100);

        // 
        AcceptingClients = true;

        //
        OnServerStart(EventArgs.Empty);

        // Running => Accept connections
        await AcceptConnections();

        // Stopped => Close connections
        await CloseConnections();

        // Server Stopped => Finish stuff

        //
        OnServerStop(EventArgs.Empty);
    }
    private async Task AcceptConnections()
    {
        await Task.Run(() =>
        {
            while (AcceptingClients)
            {
                try
                {
                    // Set the _acceptConnectionDone to nonsignaled state.  
                    _acceptConnectionDone.Reset();

                    //
                    _server.BeginAccept(AcceptConnectionsCallback, _server);

                    // Wait until a connection is made before continuing.  
                    _acceptConnectionDone.WaitOne();
                }
                catch
                {
                    //
                }
            }
        });
    }
    private static void AcceptConnectionsCallback(IAsyncResult ar)
    {
        // Signal the AcceptConnections Task to continue.  
        _acceptConnectionDone.Set();

        // 
        if (!AcceptingClients)
        {
            // Server Stopping or stopped
            return;
        }

        // Get the socket that handles the client request.  
        Socket server = (Socket)ar.AsyncState;
        Socket client = server.EndAccept(ar);

        // Create the client object.  
        ServerClient newclient = new ServerClient(client);

        // Add Events


        //
        _connections.TryAdd(_totalConnectionCount++, newclient);
    }

サーバークライアント:

    private readonly Socket _connection;

    //
    public ServerClient(Socket s)
    {
        //
        _connection = s;

        //
        Run();

        // Disconnected or needs to be disconnected => Process
        Disconnect();
        return;
    }
    private async void Run()
    {
        //
        if (_connection.Connected)
        {
            //
            _connection.ReceiveBufferSize = ServerExt.ReadBufferSize;
            _connection.SendBufferSize = ServerExt.SendBufferSize;
            _connection.NoDelay = true;

            //
            await ListenToClient();
        }
    }

クライアント:

    public async void Connect()
    {
        if (_connection != null)
        {
            // Check if we are not connected already

        }

        //
        LoadSettings();

        //
        try
        {
            //
            IPEndPoint remoteEndPoint = new IPEndPoint(_settings.Ip, _settings.Port);

            // Create a TCP/IP socket.  
            _connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            {
                ReceiveBufferSize = ServerExt.ReadBufferSize,
                SendBufferSize = ServerExt.SendBufferSize,
                NoDelay = true
            };

            // Connect to the remote endpoint.  
            _connection.Connect(remoteEndPoint);

            //
            if (_connection.Connected)
            {
                //
                await ListenToServer();
            }
        }
        catch
        {
            //

        }

        // Disconnected or needs to be disconnected => Process
        Disconnect();
        return;
    }

サンクス。

4

1 に答える 1