Search code examples
c#tcpclient

How to read response code from TcpClient


I just wonder how to read Response code from TCP client? The sample codes are below.

var tcpClient = new TcpClient();
tcpClient.Connect(this.Settings.MailServer,  this.Settings.MailServerPort);


NetworkStream stream = tcpClient.GetStream();

Solution

  • Stream > byte > String

    byte[] buffer = new byte[1024];
    int bytesRead = stream.Read(buffer, 0, buffer.Length);
    response = Encoding.ASCII.GetString(buffer, 0, bytesRead);
    Console.WriteLine(response);