Obtain second response from TCPServer

  • Thread starter Thread starter G-Oker
  • Start date Start date
G

G-Oker

Guest
Hello,

I am trying to receive 2 message from a TCP server. It will initially send an acknowledgement reply, then a few seconds later, and reply to the data sent.

I can make a connection and send the data, and the requested job gets actioned, but my stream only gets the acknowledgement, not the second reply.

I haven't close the stream (hoping it would keep listening), and Ive tried (obviously badly) a for loop, and an if loop, but then I get NO response from the server.

Can someone advise me how I can make my tcp 127.0.0.1 client wait for a second reply (or time out after xx seconds if the 1st reply is <NACK> ) ?

My code ATM is

try
{
/// Create a TcpClient.
TcpClient client = new TcpClient(127.0.0.1, 5011);

Byte[] data = System.Text.Encoding.GetEncoding(437).GetBytes(message);
String command = Encoding.GetEncoding(437).GetString(data, 0, data.Length);

richTextBox1.AppendText("message reads: " + message + Environment.NewLine + Environment.NewLine);

NetworkStream stream = client.GetStream();

stream.ReadTimeout = Convert.ToInt32(timeout);

// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);

richTextBox1.AppendText("Sending above message to 127.0.0.1" + Environment.NewLine);

// Receive the TcpServer.response.

// Buffer to store the response bytes.
data = new Byte[256];
String responseData = String.Empty;

Int32 bytes = stream.Read(data, 0, data.Length);

responseData = System.Text.Encoding.UTF8.GetString(data, 0, bytes);


richTextBox1.AppendText("Received: " + responseData);
richTextBox1.AppendText(Environment.NewLine + Environment.NewLine);

//stream.Close();
//client.Close();

}

catch (Exception ee)
{
richTextBox1.AppendText(Environment.NewLine + "\tException: " + ee.Message + Environment.NewLine);
}

Thanks in advance

Continue reading...
 
Back
Top