0

私はクライアント/サーバーを書いています (おそらくすでにご存知の方も多いでしょう!)。サーバーはクライアントにデータを送信します。ただし、クライアントは 1 回接続でき、その後、接続が終了すると再度接続を試行することはありません。同様に、サーバーがメッセージを送信すると、それ以降の接続試行は無視されます。

サーバ:

public Server(String p)
    {
        path = p;
        listener = new TcpListener(IPAddress.Any, 1337);
        listener.Start();
        Thread t = new Thread(new ThreadStart(ListenForClients));
        t.IsBackground = true;
        t.Start();
    }

    private void ListenForClients()
    {
        TcpClient client = listener.AcceptTcpClient();
        new Thread(new ParameterizedThreadStart(HandleClientCom)).Start(client);
    }

    private void HandleClientCom(object TcpClient)
    {
        new Dialog("Connection", "Connection established.");
        TcpClient client = (TcpClient)TcpClient;
        NetworkStream stream = client.GetStream();
        //SslStream stream = new SslStream(client.GetStream());
        //stream.AuthenticateAsServer(new X509Certificate(path + "\\ServerCert.cer"));

        ASCIIEncoding encoder = new ASCIIEncoding();
        String str = "This is a long piece of text to send to the client.";
        byte[] bytes = encoder.GetBytes(str);

        stream.Write(bytes, 0, bytes.Length);
        stream.Flush();
    }

クライアント:

public TCP(BackgroundWorker b)
    {
        try
        {
            bw = b;
            client = new TcpClient();
            IPEndPoint server = new IPEndPoint(IPAddress.Parse(srv), 1337);
            client.Connect(server); //Connect to the server
            bw.ReportProgress(0);   //Update the GUI with the connection status

            Thread t = new Thread(new ParameterizedThreadStart(HandleComms));
            t.IsBackground = true;
            t.Start(client);
        }
        catch (Exception ex)
        {
            lastException = ex;
        }
    }

    public void HandleComms(object c)
    {
        Boolean keepListening = true;
        TcpClient client = (TcpClient)c;
        NetworkStream stream = client.GetStream();
        //stream = new SslStream(client.GetStream());
        //stream.AuthenticateAsClient(srv);

        byte[] msg = new byte[4096]; ;
        int bytesRead = 0;

        while (keepListening)
        {
            try
            {
                bytesRead = stream.Read(msg, 0, 4096);
            }
            catch (Exception ex)
            {
                lastException = ex;
            }

            if (bytesRead > 0)
            {
                StreamWriter writer = new StreamWriter("C:\\Users\\Chris\\Desktop\\ClientLog.txt");
                ASCIIEncoding encoder = new ASCIIEncoding();
                rx = encoder.GetString(msg);
                writer.WriteLine(rx);
                keepListening = false;
                writer.Close();
            }
        }
    }

膨大な量のコードで申し訳ありません。とにかく私が間違っているところを指摘できますか?

4

1 に答える 1

2

接続を受け入れたら、もう一度リッスンを開始する必要があります。

この変更を加えてみてください:

private void ListenForClients()
{
    while(true)
    {
        TcpClient client = listener.AcceptTcpClient();
        new Thread(new ParameterizedThreadStart(HandleClientCom)).Start(client);
    }
}
于 2009-04-03T14:17:19.237 に答える