다음과 같은 베어 본 코드가 데이터를 보내거나 받으면 클라이언트의 연결이 끊어집니다.NetworkStream을 처리하면 클라이언트 연결이 끊어지는 이유
사용하는 블록이 생성하는 개체, 즉 NetworkStream 개체를 처분한다는 것은 제 것이 이해하지만 TcpClient 소켓은 왜 연결 해제합니까? 이 중요한 경우
콘솔 출력은 ... 진정한 은 거짓 여기
class Program
{
static void Main(string[] args)
{
Console.Title = "Client";
Process p = Process.Start(@"C:\Users\Teddy\Documents\visual studio 2015\code\TesyingNetworkStream\Server\bin\Debug\server.exe");
Thread.Sleep(1000);
IPEndPoint EP = new IPEndPoint(
IPAddress.Parse("192.168.1.10"), 4000
);
TcpClient cli = new TcpClient();
cli.Connect(EP);
UseClient(cli);
Console.ReadLine();
p.Kill();
p.Close();
}
private static void UseClient(TcpClient cli)
{
using (NetworkStream ns = cli.GetStream())
{
Console.WriteLine(cli.Connected);//True
}
Console.WriteLine(cli.Connected);//False
}
}
서버 코드입니다.
class Program2
{
static void Main(string[] args)
{
Console.Title = "Server";
TcpListener lis = new TcpListener(
new IPEndPoint(
IPAddress.Any, 4000
));
lis.Start();
lis.AcceptTcpClient();
while (true)
{
Thread.Sleep(10);
}
}
}