我试图打印发送到端口 2000 的信息,但我得到的只是“System.Net.Sockets.NetworkStream”。有没有办法将流转换为字符串?或者一种直接打印流信息的方法。
static void Main(string[] args)
{
while (true)
{
TcpListener TL = new TcpListener(IPAddress.Any, 2000);
TL.Start();
Socket S = TL.AcceptSocket();
Stream SS = new NetworkStream(S);
string MSG = SS.ToString();
Console.WriteLine(MSG);
}
}
编辑:这是客户
static void Main(string[] args)
{
try
{
TcpClient TC = new TcpClient();
TC.Connect("XXX.XXX.XXX.XXX", 2000);
NetworkStream Writer = TC.GetStream();
string MSG = Console.ReadLine();
byte[] pack = Encoding.ASCII.GetBytes(MSG);
Writer.Write(pack, 0, pack.Length);
Writer.Flush();
}
catch
{
Console.WriteLine(" faild");
Console.ReadLine();
}
}
PS:执行后应用程序也会崩溃