为什么tcplistener服务器未启动?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论