Getting IP address of connected socket

Z

Zytan

Once a socket is connected, perhaps to a hostname that contains
multiple IP addresses, how can I determine to which IP address the
connection is made? The only thing I can get from my Socket is an
EndPoint, which doesn't help, since it's abstract, and I need an
IPEndPoint to get the data I want (I believe). It is difficult to
find information on this, since most results show how to create a
socket given an IP address or hostname, not the reverse (which most
people couldn't care less about).

Zytan
 
J

Jon Skeet [C# MVP]

Once a socket is connected, perhaps to a hostname that contains
multiple IP addresses, how can I determine to which IP address the
connection is made? The only thing I can get from my Socket is an
EndPoint, which doesn't help, since it's abstract, and I need an
IPEndPoint to get the data I want (I believe). It is difficult to
find information on this, since most results show how to create a
socket given an IP address or hostname, not the reverse (which most
people couldn't care less about).

IPEndPoint ip = endPoint as IPEndPoint;
if (ip != null)
{
Console.WriteLine ("Endpoint address is {0}", ip.Address);
}
else
{
Console.WriteLine ("Endpoint is not an IPEndPoint");
}

Jon
 
S

SimeonArgus

Once a socket is connected, perhaps to a hostname that contains
multiple IP addresses, how can I determine to which IP address the
connection is made? The only thing I can get from my Socket is an
EndPoint, which doesn't help, since it's abstract, and I need an
IPEndPoint to get the data I want (I believe). It is difficult to
find information on this, since most results show how to create a
socket given an IP address or hostname, not the reverse (which most
people couldn't care less about).

Zytan


Let me see if I understand the question: You have a software server
runing that accepts a socket connection. You want to know if the
connection came in on your ethernet card A or your ethernet card B,
correct?

Are you using tcpListener? Or are you using raw Sockets to manage the
connection? What is the class/tool you are using, because that will
determine how we can best answer you.
 
Z

Zytan

IPEndPoint ip = endPoint as IPEndPoint;
if (ip != null)
{
Console.WriteLine ("Endpoint address is {0}", ip.Address);}
else
{
Console.WriteLine ("Endpoint is not an IPEndPoint");
}

Ok, so that's how you change from the abstract to the base. And "as"
returns null if it fails. I didn't even know you could do this for
this case.

Thanks, Jon.

Zytan
 
Z

Zytan

Let me see if I understand the question: You have a software server
runing that accepts a socket connection. You want to know if the
connection came in on your ethernet card A or your ethernet card B,
correct?

No, I have a hostname. I create an endpoint with that hostname and
port. I connect directly to that. (I could get a list of IPs that
the hostname gives, and then use one of them, as well, but I don't, I
just use Socket.Connect direct to the hostname + port). Now, I want
to know to which IP did I connect to? Using that IP, I want to get
the hostname of that IP. This is not necessarily the same hostname
that I used to connect, since that original hostname could
'forward' (I don't know the proper terminology, sorry) to a bunch of
other hostnames / IPs. And this is what I want to know.
Are you using tcpListener? Or are you using raw Sockets to manage the
connection? What is the class/tool you are using, because that will
determine how we can best answer you.

I am using:

Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
socket.Connect(hostName, port);

Is this what is called raw sockets?

Thanks for your reply,

Zytan

(Btw, Jon already answered my main question in another post.)
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Zytan said:
Ok, so that's how you change from the abstract to the base. And "as"
returns null if it fails. I didn't even know you could do this for
this case.

You changed it from the abstract base class to the derived class.
 
Z

Zytan

Ok, so that's how you change from the abstract to the base. And "as"
You changed it from the abstract base class to the derived class.

Yeah, that's what I meant, sorry. Yes, the abstract is the bass
class.

Zytan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top