컴퓨터/C#
C# TCP Client (TcpClient 사용)
Begi
2022. 10. 4. 20:11
반응형
TcpClient를 이용한 TCP Client 프로그램은 다음과 같다.
// 1.열기
TcpClient tc = new TcpClient("192.168.0.1", 500);
NetworkStream stream = tc.GetStream();
// 2.전송
byte[] buf = new byte[10];
buf에 전송할 데이터 씀
stream.Write(buf, 0, buf.Length);
// 3.시간지연
Thread.Sleep(100);
// 4.수신
int n = stream.Read(buf, 0, buf.Length);
// 5.닫기
stream.Close();
tc.Close();
TcpClient 메소드는 일정시간 동안 접속을 시도하고 접속을 실패하면 에러가 발생한다. 접속 실패했을 때는 try-catch로 처리 한다.
반응형