BrandonPotter / SimpleTCP

Straightforward .NET library to handle the repetitive tasks of spinning up and working with TCP sockets (client and server).
Apache License 2.0
368 stars 108 forks source link

server.Start(ip, Convert.ToInt32(txtPort.Text)); Null error #68

Closed asenyurt closed 3 years ago

asenyurt commented 3 years ago

Hi when i click the start button its give me Null error from server.Start(ip, Convert.ToInt32(txtPort.Text)); ip is correct host is correct but i dont understand why i am getting error image

Brhsoftco commented 3 years ago

Each time the server starts, reset it:

try {
  //validation checks
  if (string.IsNullOrWhitespace(txtHost.Text)) return;
  if (!int.TryParse(txtPort.Text, out var port)) return;

  //start requirements
  var ip = IPAddress.Parse(txtHost.Text);

  //IP validation check
  if (ip == null) return;

  //create the server
  var server = new SimpleTcpServer();

  //attempt server start
  server.Start(ip, port);
}
catch(Exception ex) {
  MessageBox.Show($"An error occurred whilst starting the server:\n\n{ex}");
}

You also don't need to directly specify an IP Address if you're positive the first valid adapter will be correct:

//start the server by using the port only
server.Start(port);

TL;DR: Always ensure you check for nulls and invalid values