473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Getting IP address of connected socket

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

Mar 20 '07 #1
6 3601
On Mar 20, 1:32 pm, "Zytan" <zytanlith...@yahoo.comwrote:
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

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

Mar 20 '07 #3
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

Mar 20 '07 #4
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.)

Mar 20 '07 #5
Hi,

"Zytan" <zy**********@yahoo.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
>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.
Mar 20 '07 #6
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.
Yeah, that's what I meant, sorry. Yes, the abstract is the bass
class.

Zytan

Mar 21 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
5
by: Jeronimo Bertran | last post by:
Hello, We have developed a socket tcpip server in .net that accepts calls from clients. Using the connected socket we are able to use the Socket.RemoteEndPoint to obtain the IP address of the...
1
by: Matt_is_a_novice | last post by:
I am writing a server to open a TCPListener, the client connects, and the server returns a Socket. I want to know what IP address is connected to my server. How do I retrieve the IP address of my...
13
by: Sandeep Singh | last post by:
I am making socket client application in C# how can i get ip address of client who has connected to server
14
by: Ankit Aneja | last post by:
The code of classes given below is for server to which clients connect i want to get ip address of client which has connected pls help how can i get //listen class public class listen {
0
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
5
by: SolaFide | last post by:
On Linux, it is a simple matter to get the local ip address with system.os("ifconfig >> /tmp/ip"); ip=open("/tmp/ip").readlines(), etc. How can I do this with Windows?
7
by: semedao | last post by:
Hi all, I view many posts about this issue , the connected property does not tell us the current status of the socket. based on couple of suggestions of msdn , and some article here , I try to...
1
by: vabh | last post by:
Help from there, i want the IP Address of all computers which are connected in LAN with me. Its really urgent for me. I using one kind of coding which returns me IP Address of only one computer, but...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.