rendered paste body// Pass the function data as a string and TRUE boolean value to wait for the reply (blocking)
private string SendServerData(string sData, bool bReadWait)
{
try
{
tcpclnt = new TcpClient();
tcpclnt.Connect(new IPEndPoint(IPAddress.Parse(this.config.sIpAddress), this.config.nPort));
stm = tcpclnt.GetStream();
const int byteCount = 300;
int readBytes = 0;
writeToServer(sData);
if(bReadWait)
{
bool exit = false;
while(!exit)
{
try
{ readBuffer = new Byte[byteCount];
readBytes = stm.Read(readBuffer,0,byteCount);
if(Encoding.ASCII.GetString(readBuffer,0,readBytes).Length > 0)
exit = true;
}
catch (Exception){break;}
}
stm.Close();
tcpclnt.Close();
return Encoding.ASCII.GetString(readBuffer,0,readBytes);
}
else
return "";
}
catch(SocketException sockexcep)
{
cwp.mobile.utils.DisplayError(sockexcep);
return "-1";
}
catch(Exception e)
{
cwp.mobile.utils.DisplayError(e);
return "-1";
}
}
private void writeToServer(string sData)
{
ASCIIEncoding encord = new ASCIIEncoding();
writeBuffer = encord.GetBytes(sData);
if(stm != null)
stm.Write(writeBuffer,0,writeBuffer.Length);
}