客户端/服务器在1次连接后都放弃了

时间:2009-04-03 14:13:38

标签: c# .net networking tcp client-server

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

服务器:

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();
            }
        }
    }

对不起大量的代码。无论如何可以指出我出错的地方?

1 个答案:

答案 0 :(得分:2)

接受连接后,您需要再次开始收听。

尝试进行此更改:

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