1

c# でのソケットとスレッド化についてもっと学ぼうとしています。始めるのに役立つ多くの優れたリソースをオンラインで見つけました。これまでに作成したプログラムは、単純な「中間者」アプリケーションです。次のように設計されています: クライアント <--> [アプリケーション] <--> サーバー

次のコードが与えられた場合、このスレッドが 100% の CPU で実行されないようにするにはどうすればよいですか? クライアント/サーバーがアイドル状態のときにスレッドがデータを待機してブロックし、終了しないようにするにはどうすればよいですか?

while (true)
       {
            lock (ClientState)
            {
                checkConnectionStatus(client, server);
            }

            if (clientStream.CanRead && clientStream.DataAvailable)
            {
                Byte[] bytes = new Byte[(client.ReceiveBufferSize)];

                IAsyncResult result = clientStream.BeginRead(bytes, 0, client.ReceiveBufferSize, null, null);
                int size = clientStream.EndRead(result);

                sendData(bytes, serverStream, size);
            }
            if (serverStream.CanRead && serverStream.DataAvailable)
            {
                Byte[] bytes = new byte[(server.ReceiveBufferSize)];

                IAsyncResult result = serverStream.BeginRead(bytes, 0, server.ReceiveBufferSize, null, null);
                int size = serverStream.EndRead(result);

                sendData(bytes, clientStream, size);
            }
        }

編集:興味のある人のために、「Connection.cs」クラス全体を投稿することにしました。私はプログラミングの初心者なので、ここにいくつかの悪いコーディング慣行があることを知っています。基本的に、このクラス全体は別のスレッドで実行され、(クライアント ソケットまたはサーバー ソケットへの) 接続が切断されると終了します。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace TCPRelay
{
public class Connection
{
    public delegate void delThreadSafeHandleException(System.Exception ex);
    public delegate void ConnectionDelegate(Connection conn);
    public int DataGridIndex;

    Main pMain;

    public TcpClient client { get; set; }
    public TcpClient server { get; set; }
    public String ClientState { get; set; }

    public string ListenPort { get; set; }
    public string remotePort { get; set; }

    public string listenAddress { get; set; }
    public string remoteAddress { get; set; }

    private TcpListener service { get; set; }

    private Main Form
    {
        get
        {
            return pMain;
        }
    }
    private NetworkStream clientStream { get; set; }
    private NetworkStream serverStream { get; set; }


    public Connection(TcpClient client, TcpClient server)
    {
        clientStream = client.GetStream();
        serverStream = server.GetStream();
    }

    public Connection(String srcAddress, int srcPort, String dstAddress, int dstPort, Main caller)
    {
        try
        {
            pMain = caller;
            TcpListener _service = new TcpListener((IPAddress.Parse(srcAddress)), srcPort);

            //Start the client service and add to connection property
            _service.Start();
            service = _service;


            //Set other useful parameters
            listenAddress = srcAddress;
            ListenPort = srcPort.ToString();

            remoteAddress = dstAddress;
            remotePort = dstPort.ToString();

            this.ClientState = "Listening";
        }
        catch (Exception ex)
        {
            pMain.HandleException(ex);
            Thread.CurrentThread.Abort();
        }

    }

    private TcpClient getServerConnection(String address, int port)
    {
        TcpClient client = new TcpClient(address, port);
        if (client.Connected)
        {
            return client;
        }
        else
        {
            throw new Exception(
                String.Format("Unable to connect to {0} on port {0}",
                address,
                port)
                );
        }
    }
    private void sendData(Byte[] databuf, NetworkStream stream, int size)
    {
        bool waiting = true;
        while (waiting)
        {
            if (stream.CanWrite)
            {
                waiting = false;
                stream.Write(databuf, 0, size);
            }
            else { throw new Exception("Unable to write to network stream"); }
        }
    }

    //Main Looping and data processing goes here
    public void ProcessClientRequest()
    {
        try
        {
            //Wait for a connection to the client
            TcpClient client = service.AcceptTcpClient();

            //Get the streams and set the peer endpoints
            this.clientStream = client.GetStream();
            this.client = client;

            //Now that we have a client, lets connect to our server endpoint
            TcpClient server = getServerConnection(remoteAddress, int.Parse(remotePort));

            //Set some useful parameters
            this.server = server;
            this.serverStream = server.GetStream();
        }
        catch (Exception ex)
        {
            lock (ClientState)
            {
                this.ClientState = ex.Message;    
            }

            CloseConnection();
            Thread.CurrentThread.Abort();
        }
        while (true)
       {
            lock (ClientState)
            {
                checkConnectionStatus(client, server);
            }

            if (clientStream.CanRead && clientStream.DataAvailable)
            {
                Byte[] bytes = new Byte[(client.ReceiveBufferSize)];

                IAsyncResult result = clientStream.BeginRead(bytes, 0, client.ReceiveBufferSize, null, null);
                int size = clientStream.EndRead(result);

                sendData(bytes, serverStream, size);
            }
            if (serverStream.CanRead && serverStream.DataAvailable)
            {
                Byte[] bytes = new byte[(server.ReceiveBufferSize)];

                IAsyncResult result = serverStream.BeginRead(bytes, 0, server.ReceiveBufferSize, null, null);
                int size = serverStream.EndRead(result);

                sendData(bytes, clientStream, size);
            }
        }
    }

    private void checkConnectionStatus(TcpClient _client, TcpClient _server)
    {
        try
        {
            if (_client.Client.Poll(0, SelectMode.SelectRead))
            {
                byte[] buff = new byte[1];
                if (_client.Client.Receive(buff, SocketFlags.Peek) == 0)
                {
                    this.ClientState = "Closed";
                    CloseConnection();
                    Thread.CurrentThread.Abort();
                }
            }
            else if (_server.Client.Poll(0, SelectMode.SelectRead))
            {
                byte[] buff = new byte[1];
                if (_server.Client.Receive(buff, SocketFlags.Peek) == 0)
                {
                    this.ClientState = "Closed";
                    CloseConnection();
                    Thread.CurrentThread.Abort();
                }
            }
            else { this.ClientState = "Connected"; }
        }
        catch (System.Net.Sockets.SocketException ex)
        {
            this.ClientState = ex.SocketErrorCode.ToString();
            CloseConnection();
            Thread.CurrentThread.Abort();
        }
    }

    public void CloseConnection()
    {
        if (clientStream != null)
        {
            clientStream.Close();
            clientStream.Dispose();
        }

        if (client != null)
        {
            client.Close();
        }

        if (serverStream != null)
        {
            serverStream.Close();
            serverStream.Dispose();
        }

        if (server != null)
        {
            server.Close();
        }

        if (service != null)
        {
            service.Stop();
        }
    }       
}

}

また、「メイン」フォームと「ConnectionManager」クラスをいじっています。

4

2 に答える 2

3

これを処理する最も効率的な方法は、各ストリームでコールバックを使用して読み取りを発行することです。

両方の読み取りを発行した後、スレッドがその作業を停止する必要があることを通知するために使用するオブジェクトを永遠に待機します (ManualResetEvent を使用するのが伝統的なものであり、一度に多くのスレッドに通知するために使用できます)。

データが受信されると、OS はコールバック関数を呼び出し、そこで処理を行ってから (重要なことに) 別の読み取りをキューに入れます。

これは、スレッドが永遠にアイドル状態であり、(「起動 - 死ぬ時間」のような方法で) 立ち去る時が来たことを知らせるシグナル オブジェクトを待機し、OS が指示したときにのみ作業を行うことを意味します。処理するデータがあること。

本当に使いやすくするために、非同期で書き込みを行い、一方の接続が他方の処理時間を枯渇させないようにします (現在の実装では、一方の書き込みがブロックされた場合、もう一方のストリームは処理されません)。

最後に、2 つのストリームを使用してメイン コードですべてを 2 回実行する代わりに、使用するストリームをパラメーターとして受け取るオブジェクトにこの動作をカプセル化し、そのうちの 2 つをインスタンス化するだけで十分です。

于 2012-09-14T03:02:45.213 に答える
2

仲介者でソケットを受け入れた後、次のことを行います。

 private void WaitForData()
    {
        try
        {
            if (socketReadCallBack == null)
            {
                socketReadCallBack = new AsyncCallback(OnDataReceived);
            }

            ReceiveState rState = new ReceiveState();
            rState.Client = mySocket;

            mySocket.BeginReceive(rState.Buffer, 0, rState.Buffer.Length, SocketFlags.None,
                new AsyncCallback(socketReadCallBack), rState);

        }
        catch (SocketException excpt)
        {
            // Process Exception
        }

    }

受信状態は次のとおりです。

public class ReceiveState
  {
    public byte[] Buffer = new byte[1024]; //buffer for network i/o
    public int DataSize = 0; //data size to be received by the server
    public bool DataSizeReceived = false; //whether prefix was received
    public MemoryStream Data = new MemoryStream(); //place where data is stored
    public Socket Client;   //client socket
   }

データが受信されると、私のルーチン「OnDataReceived」がそれを処理します。これでCPUの問題は発生していません。

クライアントと仲介者の両方に使用される同じコード。

于 2012-09-14T02:51:20.933 に答える