Connecting Tech Pros Worldwide Help | Site Map

Getting IP address of connected socket

Zytan
Guest
 
Posts: n/a
#1: Mar 20 '07
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

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Mar 20 '07

re: Getting IP address of connected socket


On Mar 20, 1:32 pm, "Zytan" <zytanlith...@yahoo.comwrote:
Quote:
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

SimeonArgus
Guest
 
Posts: n/a
#3: Mar 20 '07

re: Getting IP address of connected socket


On Mar 20, 8:32 am, "Zytan" <zytanlith...@yahoo.comwrote:
Quote:
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.

Zytan
Guest
 
Posts: n/a
#4: Mar 20 '07

re: Getting IP address of connected socket


IPEndPoint ip = endPoint as IPEndPoint;
Quote:
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

Zytan
Guest
 
Posts: n/a
#5: Mar 20 '07

re: Getting IP address of connected socket


Let me see if I understand the question: You have a software server
Quote:
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.
Quote:
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.)

Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#6: Mar 20 '07

re: Getting IP address of connected socket


Hi,

"Zytan" <zytanlithium@yahoo.comwrote in message
news:1174400390.306128.290310@o5g2000hsb.googlegro ups.com...
Quote:
Quote:
>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.
You changed it from the abstract base class to the derived class.


Zytan
Guest
 
Posts: n/a
#7: Mar 21 '07

re: Getting IP address of connected socket


Ok, so that's how you change from the abstract to the base. And "as"
Quote:
Quote:
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.
Yeah, that's what I meant, sorry. Yes, the abstract is the bass
class.

Zytan

Closed Thread