0

我正在编写一个客户端/服务器(你们中的许多人可能已经知道了!)。服务器将向客户端发送数据。但是,客户端可以连接一次,之后一旦连接终止,它就不会再次尝试连接。同样,一旦服务器发送了它的消息,它将忽略未来的连接尝试。

服务器:

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 回答