Do I need to close the connection to have messages actually sent? Because whether I use send command or use a network stream, my messages don't get processed until I close connection. Is that the way it's supposed to be or am I missing something?
Ok here's the code.
private void connectButton_Click(object sender, EventArgs e)
{
try
{
client = new TcpClient();
client.Connect(ip, port);
netStream = client.GetStream();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void disconnectButton_Click(object sender, EventArgs e)
{
if (client != null)
{
if (client.Connected)
{
client.Close();
client = null;
}
}
}
private void sendButton_Click(object sender, EventArgs e)
{
byte[] cmd = ToByteArray("bla bla bla");
netStream.Write(cmd, 0, cmd.Length);
netStream.Flush();
}
I don't think it has to do with this method but have a look.
public static byte[] ToByteArray(string str)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
you are probably using buffered streams, try to call the .Flush method, which is also automatically called when clode is invoked.
If you are blocking the main thread from executing after you Write, this could prevent the message pump for running and not sending your data. So if you release the main thread (close your data stream), it might seem like that is fixing it, but really is the releasing of the main thread that is allow the data to be processed and written.
If you do wish to block for the data to be written, make sure you use BeginWrite like this:
It seems like Nagle's algorithm in action. Try enabling NoDelay socket option using SetSocketOption method. But be careful, disabling Nagle's algorithm will downgrade the throughput.
No; they'll get sent out, possibly after a 200ms delay.
There must be something else wrong.