Click here to Skip to main content
15,176,823 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to get the ipaddress of the server from the client machine programmatically and vice versa

What I have tried:

from the previous example of server - client connection, i learned how to do connection

if tcplistener is mylist and
if s is of socket type so
console.write("connection accepted from "+ s.remoteendpoint);

by s.remoteendpoint we get what ?and how can i get the ipaddress of the server
not to say by mylist because while creating mylist object we give the ipaddress
Posted
Updated 15-Feb-21 0:34am
v2
Comments
Richard MacCutchan 15-Feb-21 5:56am
   
When you start your server you will know its IP address, so you just need to plug that number into your client application. Once the client has successfully connected to the server then the sockets take care of everything.

1 solution

You can't get a useful IP address for a client at all: all you get is the public IP address of the point of contact with the internet - the router. That isn't specific to one client, it's shared by all users connecting via the same point of contact. The actual client IP will always be a local network address, probably 192.168.xxx.xxx - and the only way a client can find the internet IP address he is using today is to ask a server, but he will still get the router IP, not anything you can use to connect to the actual client from outside. And often, that router IP address can change every time the router is power cycled.

That's why client/server interactions are always started by the client: they establish a socket connection which the server can then respond on.

Even for local network connections, the IP address is normally fleeting - every time the client connects to the network, they can get a new IP address.

Why do you think you need this?
   
v2
Comments
Member 12712527 15-Feb-21 5:41am
   
that means server when online gets a new ipaddress ?
OriginalGriff 15-Feb-21 6:11am
   
Most servers have a fixed IP address - called a static address - while most client connections are dynamic.
If you think about it, that's the "easiest way" to do it - the server address is published to domain name services (DNS, like Google, OpenDNS, etc) which translates "xxx.com" to an IP address "123.456.789.012" which the router then uses to connect.
Because the client initiates the connection, there is no need for a static IP there (though they are generally available but at an extra cost so nobody bothers). Instead, they come from an ISP pool, which is why IP address geolocation is so useless!
Have a look here: https://www.codeproject.com/Tips/792723/Using-IP-based-Geolocation-and-why-its-pretty-much

Generally, a DB connection doesn't care too much about IP addresses, it uses a instanceName.domain format so the hardware can be easily replaced without any software changes.
Member 12712527 15-Feb-21 6:20am
   
thank you sir my queries regarding this ends
Member 12712527 15-Feb-21 5:43am
   
then how could i make oledb connection which runs through MAN
Member 12712527 15-Feb-21 5:51am
   
what will i do ?
oledbconnection con=new oledbconnection
// giving all the parameters to the con
when someone writes in c#
con.connect
it will connect both database and the front end ( which may reside in same or different pc)
Member 12712527 15-Feb-21 5:57am
   
okay sir i can do it but you say will i get the ipaddress and the port no of the remote pc if i code
socket s=mylist.remoteendpoint -- mylist is the object of tcplistener class
Richard Deeming 16-Feb-21 4:12am
   
"192.1678.xxx.xxx"

Is that one of those TV/movie IP addresses? 😁
OriginalGriff 16-Feb-21 4:20am
   
IP V7.NCSI I think. :O
OriginalGriff 16-Feb-21 4:20am
   
Fixed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900