簡體   English   中英

C# Winforms 使用 TcpListener 我無法從局域網上的其他設備連接到服務器

[英]C# Winforms using TcpListener I can't connect to server from other devices on my LAN

我有一個 Winforms 應用程序,它將數據傳輸到 Android 應用程序,現在它接受連接並將一行文本顯示為 output。 我在本地機器上使用 PowerShell 中的 telnet 命令進行了測試,它返回了正確的消息。 當我從網絡上的另一台 PC 嘗試該命令時,它只是超時並且無法連接。 我確保端口是空閑的並且我的防火牆已關閉。

這是我的方法:

 public void senddata()
 {
        new Thread(() =>
        {
            Thread.CurrentThread.IsBackground = true;

            TcpListener server = null;
            try
            {
                Int32 port = 4296;
                IPAddress localAddr =IPAddress.Any;
                server = new TcpListener(localAddr, port);
                server.Start();

                Byte[] bytes = new Byte[256];

                while (true)
                {
                    server.Start();
                    Debug.WriteLine("Waiting for a connection... ");
                    TcpClient client = server.AcceptTcpClient();

                    NetworkStream stream = client.GetStream();

                    Debug.WriteLine("Connected!");
                    server.Stop();

                    while (stream.Read(bytes, 0, bytes.Length) != 0)
                    {
                        string data = "CPU: " + cpuCircle.Value + " C" + "        |         GPU: " + gpuCircle.Value + " C";
                        byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                        stream.Write(msg, 0, msg.Length);
                        stream.Flush();
                    }

                    client.Close();
                }
            }
            catch (SocketException m)
            {
                Debug.WriteLine("SocketException: "+m);
            }
            finally
            {
                server.Stop();
            }
        }).Start();
}

知道我做錯了什么嗎?

我認為主要問題是您的監聽器 object 是在 Thread 本身中創建的,因為只有第一個客戶端能夠連接。 請在調用 senddata() 方法之前使 TcpListener object 並且此偵聽器也應在調用 senddata() 方法之前接受連接,並將客戶端 object 傳遞給 senddata(TcpClient 客戶端) 方法。 通過這樣做,您將能夠同時招待理論上無限的客戶:

嘗試這個:

void StackOverflow4() // Your calling method
    {
        TcpListener server = null;
        Int32 port = 4296;
        IPAddress localAddr = IPAddress.Any;
        try
        {
            server = new TcpListener(localAddr, port);
            server.Start();

            Byte[] bytes = new Byte[256];

            while (true)
            {                    
                Debug.WriteLine("Waiting for a connection... ");
                TcpClient client = server.AcceptTcpClient();
                senddata(client);
            }
        }
        catch (SocketException m)
        {
            Debug.WriteLine("SocketException: " + m);
            //Some logic to restart listner
        }
        catch(Exception ex)
        {
            Debug.WriteLine(ex.Message);
            //Some logic to restart listner
        }
    }


    public void senddata(TcpClient client)
    {
        new Thread(() =>
        {
            Thread.CurrentThread.IsBackground = true;

           try
            {
                Byte[] bytes = new Byte[256];

                while (true)
                {
                    NetworkStream stream = client.GetStream();

                    Debug.WriteLine("Connected!");
                    //server.Stop();

                    while (stream.Read(bytes, 0, bytes.Length) != 0)
                    {
                        string data = "CPU: " + cpuCircle.Value + " C" + "        |         GPU: " + gpuCircle.Value + " C";
                        byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                        stream.Write(msg, 0, msg.Length);
                        stream.Flush();
                    }

                    client.Close();
                }
            }
            catch (SocketException m)
            {
                Debug.WriteLine("SocketException: " + m);
            }
            finally
            {
                //server.Stop();
            }
        }).Start();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM