24# 서버와 접속 중일 때 자신 포트 찾기
서버에 접속하려면 서버 ip와 포트를 알아야 한다.
그런데 클라이언트에서 자기 포트를 알아야 하는 경우가 생겼는데, 별 다른 글이 없는 도중 양형의 좋은 글을 발견.
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endPoints = ipProperties.GetActiveTcpListeners(); TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections();
foreach (TcpConnectionInformation info in tcpConnections) { if(info.RemoteEndPoint.Address.ToString() == WebSocketConfig.serverIp && info.RemoteEndPoint.Port == WebSocketConfig.serverPort) { LazySingleton<UserConfig>.Instance.IpPort = string.Format("{0}:{1}", info.LocalEndPoint.Address, info.LocalEndPoint.Port); break; }
//Console.WriteLine("Local: {0}:{1}\nRemote: {2}:{3}\nState: {4}\n", // info.LocalEndPoint.Address, info.LocalEndPoint.Port, // info.RemoteEndPoint.Address, info.RemoteEndPoint.Port, // info.State.ToString()); } |
참고 사이트
https://codeday.me/ko/qa/20190307/16164.html
c#에서 TCP 포트를 사용할 수 있는지 확인하는 방법은 무엇입니까? - 코드 로그
c#에서는 TcpClient를 사용하거나 일반적으로 소켓에 연결하는 방법 컴퓨터에서 특정 포트가 사용 가능한지 확인하려면 어떻게해야합니까? 더 많은 정보:이것은 내가 사용하는 코드입니다. TcpClient c; //I want to check here if port is free. c = new TcpClient(ip, port); 우수 답변 TcpClient를 사용하고 있기 때문에 열려있는 TCP 포트를 확인하고있는 것입니다. System.Net.Net
codeday.me