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ş:
Eğer nasıl bağlantı dizesi geçerli olu...
Java eğer herhangi bir sonuç olup olma...
Nasıl eğer bir element, bir dizi içind...
Nasıl eğer bir nesnenin null olup olma...
nasıl eğer Alıcı android kayıtlı olup ...