如何从TcpClient读取响应代码

时间:2015-01-29 06:31:58

标签: c# tcpclient

我只是想知道如何从TCP客户端读取响应代码?示例代码如下。

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


NetworkStream stream = tcpClient.GetStream();

2 个答案:

答案 0 :(得分:1)

流>字节>串

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

答案 1 :(得分:0)

你可以用这个:

byte[] message = new byte[512];
int bytesRead;
bytesRead = stream.Read(message, 0, message.Length);
ASCIIEncoding encoder = new ASCIIEncoding();
Console.WriteLine(encoder.GetString(message, 0, bytesRead));