【问题标题】:Client/Server both give up after 1 connection客户端/服务器在 1 次连接后都放弃
【发布时间】:2010-10-17 09:04:32
【问题描述】:

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

服务器:

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

抱歉,代码量很大。无论如何能指出我哪里出错了吗?

【问题讨论】:

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


    【解决方案1】:

    接受连接后,您需要重新开始收听。

    尝试进行此更改:

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

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 2015-09-12
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 2019-01-18
      • 2021-07-01
      • 2012-03-27
      相关资源
      最近更新 更多