I'm having a problem with using a TcpClient to send data over a stream written to by a StreamWriter.
private void sendMessage(string[] hostlist, string message)
{
foreach (string host in hostlist)
{
try
{
messageClient = new TcpClient(host, 24300);
StreamWriter writer = new StreamWriter(messageClient.GetStream());
writer.Write(message);
writer.Flush();
}
catch (Exception)
{
MessageBox.Show("Error 1\n" +
"This may be due to two things:\n" +
"1. The hostname is invalid.\n" +
"2. The destination computer is not online.",
"Error Sending Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
The problem line is where messageClient is initialized. If I use an IP address, there is no hang at all, the message is instantly sent and received. However, if I use a hostname such as "lappy" (name of my laptop) the program completely hangs for exactly 6 seconds, then sends the message. This happens every time you attempt to send a message using a hostname. Is there something I'm doing wrong here? Is there a different implementation if you need to use hostnames rather than IP addresses?
Thanks.