C# .NET - How to know a socket connected to ip address

Asked By balaji mogadali
23-Sep-11 02:34 AM
hi frds

how to know a socket connected to an ip address

pls help
  James H replied to balaji mogadali
23-Sep-11 02:40 AM

http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.remoteendpoint.aspx

You can then call the IPEndPoint..::.Address method to retrieve the remote IPAddress, and the IPEndPoint..::.Port method to retrieve the remote port number.

More from the link (fixed up alot heh):

Socket s;

IPEndPoint remoteIpEndPoint = s.RemoteEndPoint as IPEndPoint;
IPEndPoint localIpEndPoint = s.LocalEndPoint as IPEndPoint;

if (remoteIpEndPoint != null)
{
    // Using the RemoteEndPoint property.
    Console.WriteLine("I am connected to " + remoteIpEndPoint.Address + "on port number " + remoteIpEndPoint.Port);
}

if (localIpEndPoint != null)
{
    // Using the LocalEndPoint property.
    Console.WriteLine("My local IpAddress is :" + localIpEndPoint.Address + "I am connected on port number " + localIpEndPoint.Port);
}
  smr replied to balaji mogadali
23-Sep-11 02:43 AM
hi

You probably want to call http://msdn.microsoft.com/en-us/library/ms738543%28VS.85%29.aspx. Using it is pretty basic, you pass a sockaddr pointer and a length and it fills in the data for you.

As far as determining if the connection is local, http://msdn.microsoft.com/en-us/library/ms738520%28VS.85%29.aspx can give you a list of all available local addresses. You would compare the result of getpeername() to the local address list.

  Anoop S replied to balaji mogadali
23-Sep-11 04:51 AM

Since you're using a TcpClient, that means you're checking open TCP ports. There are lots of good objects available in the http://msdn.microsoft.com/en-us/library/system.net.networkinformation.aspx namespace.

Use the IPGlobalProperties object to get to an array of TcpConnectionInformation objects, which you can then interrogate about endpoint IP and port.


 
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.

Create New Account
help
Get Mac Address How to get Mac Address? public static void DisplayTypeAndAddress() { IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties(); NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); Console.WriteLine( "Interface information for {0}.{1} " , computerProperties.HostName, computerProperties.DomainName
Windows CE 4.2 but not Windows CE 5.0 IPAddress ipAddress = Network .GetServerIP(host); IPEndPoint ipEndPoint = new IPEndPoint (ipAddress, port); m_ClientSocket = new Socket ( AddressFamily .InterNetwork, SocketType .Stream, ProtocolType .Tcp); m_ClientSocket.Connect(ipEndPoint); / / throwing exception -this works on a Windows CE 4.2 device but not a Windows