SORU
20 ŞUBAT 2009, Cuma


C#, nasıl eğer bir TCP bağlantı noktası mevcut olup olmadığını kontrol etmek için?

C# bir TcpClient kullanmak ya da genellikle bir yuva bağlanmak için nasıl eğer belli bir port makinemi ücretsiz bir bakabilir miyim?

daha fazla bilgi: Bu benim kullandığım kod

TcpClient c;
//I want to check here if port is free.
c = new TcpClient(ip, port);

CEVAP
20 ŞUBAT 2009, Cuma


TcpClient, bir kullanıyorsun beri açık TCP bağlantı noktalarını kontrol ediyorsun demektir. İyi nesneler System.Net.NetworkInformation ad vardır.

IPGlobalProperties nesne sonra bitiş IP ve port sorgulama hakkında hangi TcpConnectionInformation nesneleri bir dizi için kullanın.


 int port = 456; //<--- This is your value
 bool isAvailable = true;

 // Evaluate current system tcp connections. This is the same information provided
 // by the netstat command line application, just in .Net strongly-typed object
 // form.  We will look through the list, and if our port we would like to use
 // in our TcpClient is occupied, we will set isAvailable to false.
 IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
 TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();

 foreach (TcpConnectionInformation tcpi in tcpConnInfoArray)
 {
   if (tcpi.LocalEndPoint.Port==port)
   {
     isAvailable = false;
     break;
   }
 }

 // At this point, if isAvailable is true, we can proceed accordingly.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • AmazonWireless

    AmazonWirele

    8 EYLÜL 2010
  • 趣味そうこ♪

    趣味そう

    3 Mart 2010
  • WPBeginner - WordPress Tutorials

    WPBeginner -

    17 Temmuz 2009