为什么tcplistener服务器未启动?

发布于 2025-02-09 17:35:31 字数 2376 浏览 3 评论 0原文

tcplistener服务器不启动,我想从连接到ESP8266的湿度传感器中阅读,这应该应该连接到它,与NULL有关的错误出现了。服务器正在Xamarin应用程序上升高。 我已经尝试在代码的不同部分中注册服务器初始化,但仍然没有出现。告诉我错误是什么。

using System;
using Xamarin.Forms;
using System.Net.Sockets;
using System.Net;
using Android.Widget;
using System.Text;
using System.Collections.Generic;

namespace Teplica
{
    public partial class MainPage : ContentPage
    {
        private string ip = "192.168.1.2"; 

        private int port = 8545;

        private bool door = true;

        TcpListener server;

        TcpClient client = null;

        NetworkStream stream = null;

        private bool Timer = true;

        private byte[] data = null;

        StringBuilder builder = null;

        private List<string> list = new List<string>();
        public MainPage()
        {
            InitializeComponent();

            server = new TcpListener(IPAddress.Parse(ip), 8545);

            server.Start();//NullReferencesException

            client = server.AcceptTcpClient();

            stream = client.GetStream();

            Device.StartTimer(TimeSpan.FromSeconds(1), TeplicaTimer);
        }
        private bool TeplicaTimer()
        {
            data = new byte[256];

            builder = new StringBuilder();

            int bytes = stream.Read(data,0, data.Length);

            builder.Append(Encoding.UTF8.GetString(data, 0, bytes));

            list.Add(Convert.ToString(builder));

            DataVlazhnost.ItemsSource = list;

            return Timer;
        }
        private void OpenDoor_Clicked(object sender, EventArgs e)
        {
            if (door)
            {
                Toast.MakeText(Android.App.Application.Context, "Открытие двери", ToastLength.Short).Show();

                OpenDoor.Text = "Закрыть";

                door = false;
            }
            else
            {
                Toast.MakeText(Android.App.Application.Context, "Закрытие двери", ToastLength.Short).Show();

                OpenDoor.Text = "Открыть";

                door = true;
            }
        }

        private void Water_Clicked(object sender, EventArgs e)
        {
            Toast.MakeText(Android.App.Application.Context, "Подача воды", ToastLength.Short).Show();
        }
        
    }
}

The TcpListener server does not start, I want to take readings from a humidity sensor connected to ESP8266, which should connect to it, an error related to null comes out. The server is being raised on the Xamarin application.
I have already tried to register server initialization in different parts of the code, but still nothing comes out. Tell me what the error is.

using System;
using Xamarin.Forms;
using System.Net.Sockets;
using System.Net;
using Android.Widget;
using System.Text;
using System.Collections.Generic;

namespace Teplica
{
    public partial class MainPage : ContentPage
    {
        private string ip = "192.168.1.2"; 

        private int port = 8545;

        private bool door = true;

        TcpListener server;

        TcpClient client = null;

        NetworkStream stream = null;

        private bool Timer = true;

        private byte[] data = null;

        StringBuilder builder = null;

        private List<string> list = new List<string>();
        public MainPage()
        {
            InitializeComponent();

            server = new TcpListener(IPAddress.Parse(ip), 8545);

            server.Start();//NullReferencesException

            client = server.AcceptTcpClient();

            stream = client.GetStream();

            Device.StartTimer(TimeSpan.FromSeconds(1), TeplicaTimer);
        }
        private bool TeplicaTimer()
        {
            data = new byte[256];

            builder = new StringBuilder();

            int bytes = stream.Read(data,0, data.Length);

            builder.Append(Encoding.UTF8.GetString(data, 0, bytes));

            list.Add(Convert.ToString(builder));

            DataVlazhnost.ItemsSource = list;

            return Timer;
        }
        private void OpenDoor_Clicked(object sender, EventArgs e)
        {
            if (door)
            {
                Toast.MakeText(Android.App.Application.Context, "Открытие двери", ToastLength.Short).Show();

                OpenDoor.Text = "Закрыть";

                door = false;
            }
            else
            {
                Toast.MakeText(Android.App.Application.Context, "Закрытие двери", ToastLength.Short).Show();

                OpenDoor.Text = "Открыть";

                door = true;
            }
        }

        private void Water_Clicked(object sender, EventArgs e)
        {
            Toast.MakeText(Android.App.Application.Context, "Подача воды", ToastLength.Short).Show();
        }
        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文