I have a C# application where I enter the IP address of a device connected to my phone via Wi-Fi, which distributes the same phone. Here if you enter gibberish, for example 192..51.77, then try catch is worked out and thrown back to the initial IP input page. If you enter some correct address in the format there, for example, 192.168.30.7, which is not in the list of connected devices, and then try to send data to it, the application hangs, although I inserted part of the code responsible for sending in try catch. How do I check that such an address exists at all, and something can be sent to it? The device whose address I want to check for the existence of ESP8266.
using System;
using System.Net.Sockets;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using NetObserver.PingUtility;
namespace WindyBox
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PowerPage : ContentPage
{
private string ip;
private int port;
private int value = 0;
private double LastValue = 0;
public PowerPage(string ip, int port)
{
InitializeComponent();
this.ip = ip;
this.port = port;
Cooler.Source = ImageSource.FromResource("WindyBox.Resources.Cooler.png");
}
//Отправление значения, уменьшающее мощность на 1%.
private void Minus1(object sender, EventArgs e)
{
if (!(LastValue - 3 < 0) && !(value - 1 < 0))
{
try
{
TcpClient Client = new TcpClient();
Client.Connect(ip, port);
NetworkStream stream = Client.GetStream();
ValuePower.Text = $"{value - 1}%";
LastValue -= 3;
value -= 1;
string responce = Convert.ToString(LastValue);
byte[] data = Encoding.UTF8.GetBytes(responce);
stream.Write(data, 0, data.Length);
Client.Close();
stream.Close();
}
catch
{
Navigation.PopModalAsync();
}
}
else
{
try
{
TcpClient Client = new TcpClient();
Client.Connect(ip, port);
NetworkStream stream = Client.GetStream();
ValuePower.Text = "0%";
value = 0;
string responce = Convert.ToString(0);
byte[] data = Encoding.UTF8.GetBytes(responce);
stream.Write(data, 0, data.Length);
Client.Close();
stream.Close();
}
catch
{
Navigation.PopModalAsync();
}
}
}
//Отправление значения, уменьшающее мощность на 10%.
private void Minus10(object sender, EventArgs e)
{
if (!(LastValue - 26 < 0) && !(value - 10 < 0))
{
try
{
TcpClient Client = new TcpClient();
Client.Connect(ip, port);
NetworkStream stream = Client.GetStream();
ValuePower.Text = $"{value - 10}%";
LastValue -= 26;
value -= 10;
string responce = Convert.ToString(LastValue);
byte[] data = Encoding.UTF8.GetBytes(responce);
stream.Write(data, 0, data.Length);
Client.Close();
stream.Close();
}
catch
{
Navigation.PopModalAsync();
}
}
else
{
try
{
TcpClient Client = new TcpClient();
Client.Connect(ip, port);
NetworkStream stream = Client.GetStream();
ValuePower.Text = "0%";
value = 0;
string responce = Convert.ToString(0);
byte[] data = Encoding.UTF8.GetBytes(responce);
stream.Write(data, 0, data.Length);
Client.Close();
stream.Close();
}
catch
{
Navigation.PopModalAsync();
}
}
}
//Отправление значения, увеличевающее мощность на 10%.
private void Plus10(object sender, EventArgs e)
{
if (LastValue + 26 < 255 && value + 10 < 100)
{
try
{
TcpClient Client = new TcpClient();
Client.Connect(ip, port);
NetworkStream stream = Client.GetStream();
ValuePower.Text = $"{value + 10}%"; ;
LastValue += 26;
value += 10;
string responce = Convert.ToString(LastValue);
byte[] data = Encoding.UTF8.GetBytes(responce);
stream.Write(data, 0, data.Length);
Client.Close();
stream.Close();
}
catch
{
Navigation.PopModalAsync();
}
}
else
{
try
{
TcpClient Client = new TcpClient();
Client.Connect(ip, port);
NetworkStream stream = Client.GetStream();
ValuePower.Text = "100%";
LastValue = 255;
value = 100;
string responce = Convert.ToString(255);
byte[] data = Encoding.UTF8.GetBytes(responce);
stream.Write(data, 0, data.Length);
Client.Close();
stream.Close();
}
catch
{
Navigation.PopModalAsync();
}
}
}
//Отправление значения, увеличевающее мощность на 1%.
private void Plus1(object sender, EventArgs e)
{
if (LastValue + 3 < 255 && value + 1 < 100)
{
try
{
TcpClient Client = new TcpClient();
Client.Connect(ip, port);
NetworkStream stream = Client.GetStream();
ValuePower.Text = $"{value + 1}%";
LastValue += 3;
value += 1;
string responce = Convert.ToString(LastValue);
byte[] data = Encoding.UTF8.GetBytes(responce);
stream.Write(data, 0, data.Length);
Client.Close();
stream.Close();
}
catch
{
Navigation.PopModalAsync();
}
}
else
{
try
{
TcpClient Client = new TcpClient();
Client.Connect(ip, port);
NetworkStream stream = Client.GetStream();
ValuePower.Text = "100%";
LastValue = 255;
value = 100;
string responce = Convert.ToString(255);
byte[] data = Encoding.UTF8.GetBytes(responce);
stream.Write(data, 0, data.Length);
Client.Close();
stream.Close();
}
catch
{
Navigation.PopModalAsync();
}
}
}
/*private void ValidIp(string ip)
{
IcmpRequestSender test = new IcmpRequestSender();
System.Net.NetworkInformation.PingReply Connect = test.RequestIcmp(ip);
if (!(Connect.Status == System.Net.NetworkInformation.IPStatus.Success))
Navigation.PopModalAsync();
}*/
//Обратно на страницу ввода IP.
private async void BackMainPage(object sender, EventArgs e)
{
await Navigation.PopModalAsync();
}
}
}