I have the following code. An error occurs and the code simply breaks at _clientStreamWriter.Flush(); at the SendData method.
No error is given, no exception is thrown… the program just stops. Any help? Thanks!
public class clsTCPClient
{
TcpClient TCPClient = new TcpClient();
StreamWriter _clientStreamWriter;
public bool Connect(string Dest, int Port)
{
try
{
if (!TCPClient.Connected)
{
TCPClient.Connect(Dest, Port);
_clientStreamWriter = new StreamWriter(TCPClient.GetStream());
}
}
catch(Exception e)
{
utilities.WriteLog(utilities.LogDir + "\\AEOS.log", e.ToString());
return false;
}
return true;
}
public void SendData(string Data)
{
try
{
//send message to server
_clientStreamWriter.WriteLine(Data);
_clientStreamWriter.Flush();
}
catch(Exception e)
{
utilities.WriteLog(utilities.LogDir + "\\AEOS.log", e.ToString());
}
}
}
TCP is a reliable connection. You need to read all data on the server side and properly finish reading for your code to move further. You probably will get the exception after timeout.
Edit:
I just tested it and actually Flush() shouldn’t block even if you don’t accept socket or read any data on server side. So it must be some other problem.