Skip to content

Instantly share code, notes, and snippets.

@paco-valdez
Last active January 19, 2018 19:46
Show Gist options
  • Save paco-valdez/7d6a8b677bdf474d482b56a528d3c497 to your computer and use it in GitHub Desktop.
Save paco-valdez/7d6a8b677bdf474d482b56a528d3c497 to your computer and use it in GitHub Desktop.
static void Connect(String message)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 5005;
TcpClient client = new TcpClient("tracking.sintrafico.com", port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment