尝试在 WPF 中制作一个基本的 TCP 客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16579319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:48:14  来源:igfitidea点击:

Trying to make a basic TCP client in WPF

c#wpftcp

提问by Nick Falconer

I am trying to implement a basic TCP client in WPF. I have managed to to this using windows forms but can't get it working in WPF. I have tried going back to the very basics and adding things bit by bit. This is to connect to an arduino that is outputing single lines of code. I can successfully connect to it through telnet so the problem is with my code.

我正在尝试在 WPF 中实现一个基本的 TCP 客户端。我已经设法使用 Windows 窗体做到了这一点,但无法在 WPF 中使用它。我尝试回到最基本的东西,一点一点地添加东西。这是为了连接到输出单行代码的 arduino。我可以通过 telnet 成功连接到它,所以问题出在我的代码上。

This is what I have so far:

这是我到目前为止:

public partial class MainWindow : Window
{
    private TcpClient tcp;
    private StreamWriter SwSender;
    private StreamReader SrReciever;
    private Thread thrMessaging;
    private delegate void UpdateLogCallBack(string strMessage);

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btn_Connect_Click(object sender, RoutedEventArgs e)
    {
        TcpClient tcp = new TcpClient();
        txt_Log.AppendText("connecting");
        tcp.Connect(IPAddress.Parse("192.168.137.1"), 2000);
        txt_Log.AppendText("Connected");
        thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
        thrMessaging.Start();

    }
    private void ReceiveMessages()
    {
        SrReciever = new StreamReader(tcp.GetStream());
        while (true)
        {
            string response = SrReciever.ReadLine();
            txt_Log.Dispatcher.Invoke(new UpdateLogCallBack(this.UpdateLog), new object[] { response });
        }
    }
    private void UpdateLog(string strMessage)
    {
        txt_Log.AppendText(strMessage);
    }
}

}

}

Running this gives me an error in the Receive messages method. It says the error is on the line with "SrReciever = new StreamReader(tcp.GetStream());" saying it a NullReferenceException, Object reference not set to an instance of an object.

运行此命令会导致我在 Receive messages 方法中出现错误。它说错误与“SrReciever = new StreamReader(tcp.GetStream());”有关 说它是 NullReferenceException,未将对象引用设置为对象的实例。

I'm not the best at programming, so if there is an example out there for a TCP client that works in WPF then that will be very helpful.

我不是最擅长编程,所以如果有一个在 WPF 中工作的 TCP 客户端的例子,那将非常有帮助。

Thanks Nick

谢谢尼克

回答by gideon

That is simply because you're creating a scoped variable here:

这仅仅是因为您在这里创建了一个作用域变量:

private void btn_Connect_Click(object sender, RoutedEventArgs e)
{
    //TcpClient tcp = new TcpClient();
    //this initialized a new tcp variable only here...

    //do this instead...
    tcp = new TcpClient();
    //this will assign a new TcpClient to MainWindow.tcp

So what happens is MainWindow.tcpis actually null and then when ReceiveMessagesis called you are trying to call GetStream a null value.

所以发生的事情MainWindow.tcp实际上是空值,然后当ReceiveMessages被调用时,您试图将 GetStream 调用为空值。