How do I specify Server Ip address in my Client program? - c#

I'm currently working with TCP/IP Sockets, My client console program has to connect with server which is my PC, If the destination in the client program is specified as a local host it works fine, what I need to get done with is to connecting the client with my server through internet, what I did is looked up for my IP address on http://www.whatismyip.com/ and tried but it didn't work as I think it's a Network Interface IP address, then I altered destination IP address in client program specifically to the address of my computer which I want it to be a server, but that didn't work also. Here's my code.
Ip = (IPAddress.Parse("192.168.1.4"));
MyClient.Connect(Ip,6000);
GetStream = MyClient.GetStream();
Console.WriteLine("CONNECTED TO SERVER");
Read = new BinaryReader(GetStream);
Write = new BinaryWriter(GetStream);

There 2 things (at least) that you should be aware of:
1. To access your computer from Internet (from the public address, the one you get with whatismyip.com) you need to open the port (6000) at the router, and tell the router to what IP it should forward the incoming connection. You could specified specifics ports or put a DMZ host where all the incoming connections will be routed to that host/PC. Read your router manual to see how that is done.
2. You cannot access your public IP from the inner side of the router (intranet), if you want to connect to your public IP you need to be in another network.
If you have a dynamic IP (is the default) every time the router is powered off and on then, most probably, that IP would change, you need to investigate thru whatismyip.com in order to know what IP has been assigned. You could connect to dyndns.org and ask for a host name myhost.mydomain.com (.es, .fr, etc), and in the router tell the DDNS (Dynamic DNS) to update that host every time the IP changes. In your client program you then connect to MyClient.Connect("myhost.mydomain.com", 6000);
Hope I explain myself well enough, anyway, if you have any question let me know.

Related

.NET Remoting Bind to All NICs

I am working on my companies .NET remoting server application to try and make it bind to all available NICs, so that it listens on multiple subnets.
The current way we do it is by telling the server to bind to a specific IP address and that has worked in the past, but when hosted on a server with multiple subnets the clients all need to be on the same subnet or they won't be able to speak to the server.
One solution I tried was to tell the server to bind to the machine name of the server it was running on using the following code:
// If there is an IP present in the config to bind to, then bind on that specific IP.
if (!string.IsNullOrEmpty(remotingBindTo))
{
props.Add("bindTo", remotingBindTo);
}
// Otherwise bind on the machine name, meaning the server should bind on all IP addresses.
else
{
props.Add("machineName", Environment.MachineName);
props.Add("useIpAddress", false);
}
// Create a new channel
IChannel channel = new TcpServerChannel(props, null);
// Register the channel
ChannelServices.RegisterChannel(channel, remotingSecure);
That basically says if the servers config setting "RemotingBindTo" is set then we bind to the provided IP address otherwise we bind to the machine name.
The problem with this is if you use a VPN connection to connect a client to the server. When the client is creating a proxy to a server object it is trying to connect via the machine name and the client PC can not resolve that to an IP address. A work around to this was to place the name into the Windows hosts file and tell it to resolve to the VPN IP address, but because the VPN gives a different IP address every time this is not a viable solution.
I suppose my question is: Is it possible AT ALL to get a .NET remoting server to bind to all available NICs/IP addresses?
Has anyone else experienced anything similar?

How do I start a tcplistener on a domain name?

I have the problem that my tcplistener only gets requests when specifying the actual IP in the browser, but I want to be able to call it by computer name as well.
var listener = new TcpListener(IPAddress.Any, 5556);
listener.Start();
visit this in browser:
http://mycomputername.com:5556/
but it doesnt work. I got my computer name by "nslookup [myip]"
Unless you are running your own internal DNS server the "mycomputername.com" url is going to get resolved by an external DNS server, which will return the external (internet facing) IP address. You will need to configure your router(s) to forward the correct port (tcp:5556) from the internet facing side of your network to the correct internal computer. This is usually in the "NAT", or "Firewall" section of your router's configuration.

How to get my correct IP address?

I try to get my correct IP but I can't
I'm using this code:
{
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}
But it does not work for me!
For example my real IP is 151.246.147.86 but with this code I get 192.168.1.2.
Note: I have 4 Network adapters and in DOC with IPConfig I see this:
Network adapter: Local Area Connection 6
Media state : Media disconnected
Network adapter: Local Area Connection 4
Media state : Media disconnected
Network adapter: Local Area Connection 3
IP Address: 10.10.255.222
Network adapter: Local Area Connection
IP Address: 192.168.1.2
Now I connect to net and using with "Local Area Connection"; the public IP of my WAN connection is 151.246.147.86. I want to get this public IP (151.246.147.86), how can I do that?
Note : I don't want (and I can't, since I am using a VPN) use third party websites to get my IP
Please help!
-------EDIT :-------------
Note : I using from VPN and my VPN IP (for example) is : 176.227.197.111. But the IP of my WAN is: 151.246.147.86 and i want to get this address.
You (probably) are using a router/modem, therefore 192.168.1.2 is your "real IP". Your modem/router will be on the same (private) network, and its public interface (WAN) will have a public IP. So you need to get the IP on the public interface of your modem/router.
How to get the WAN IP of your modem depends on your mark and model; if it supports UPnP you can probably do it, or maybe if it is an enterprise class router it may also support SNMP...
You should specify your make and model.
Another way without using external sites: do a tracert to a known site (google?) the first hop will be your route.
Also, if you are on a VPN, you may be able to use the same technique. For example, if you know another host on the VPN (A server maybe?) you can ping/tracert it and discover your router from there. I don't know if in this case you will obtain what you call "a real IP" (by the way, how do you know this IP in the first place? You may be able to obtain it in the same way, programmatically).
Another solution for your VPN-based scenario: you can use Windows to help you. Windows has some kind of VPN management (RAS) which may help you; I would suggest starting from here to understand the basics, and then look for a library/SDK to help you (a quick google returned http://dotras.codeplex.com/).
What you ask is not possible unless there is some way to query your router/modem/external-most endpoint for its WAN address. The only IP address your computer knows about is its own (internal IP).
Technical note: there is nothing non-"real" about the IP address 192.168.1.2 - this is your computer's address. It is simply local to your given internal network and all but useless to anything outside.
If your router supports uPnP, you will need to query GetExternalIPAddress (starting point Is there a UPnP Library for .NET (C# or VB.NET)?). However since uPnP is considered dangerous and many security-conscious users turn it off, I would not count on it being enabled.
Querying an external service will be your most reliable bet for getting your external IP, whether it is one you write, or a third party service (see also https://superuser.com/questions/420969/services-that-just-return-your-public-ip-as-text).
There are many methods and tutorials that teach you how to find an ip address,I use this tool http://www.ipgp.net/ to display information about any ip address, just enter any IP address into the box and you will get the country, city, region, ISP, street address and the satellite location map for every query.
you're trying to get your IP address from your computer or a server? It will make a lot of difference. If you're trying to get your IP address from your computer/laptop use http://www.howtochangeipaddress.com. Your IP will pop up in front of you soon you enter the site.

Can connect using loopback (127.0.0.1) but not using my IP Address

I am using a library called Lidgren network that handles all the network part of my application. It uses UDP.
I never had any problem with TCP applications and port forwarding (apache, FTP, SMTP, etc). I did some TCP applications using TCP and I could connect without a problem.
I am making an online game right now and I use UDP for network transmission. I port forwarded it in my router and I have it running. When I use my remote IP Address (190.136.243.40) to connect to it, I can't. When I use loopback (127.0.0.1) I can connect without any kind of problem.
I turned off Windows firewall, I rebooted the computer, but I still can't connect using my remote IP Address.
Here is part of my code:
public static class ServerInfo
{
static ServerInfo()
{
ServerAddress = Dns.GetHostAddresses("leboran.dyndns.org")[0];
}
public const short Port = 6483;
public static IPAddress ServerAddress;
public const string AppIdentifier = "UnnamedGameNet";
public static byte[] MagicByte
{
get
{
return new byte[4] { 0xF1, 0x64, 0x83, 0xC4 };
}
}
}
This is the part where I start my server, probably not important:
public void StartServer()
{
this.listenThread = new Thread(ListenForClients);
peerConfig = new NetPeerConfiguration(SharedConstants.ServerInfo.AppIdentifier);
peerConfig.Port = SharedConstants.ServerInfo.Port;
peerConfig.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
peerConfig.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
peerConfig.ConnectionTimeout = 2144124;
netServer = new NetServer(peerConfig);
listenThread.Start();
}
The port I am using is 6483. I am running a Minecraft server at port 25565 and it works perfectly, so it is not a bad configuration on the router
Some computers have multiple ips in fact they all do, what you are probably experiencing is you've bound you app to listen to port 127.0.0.1 rather than 64.323.23.234 or ideally 0.0.0.0 which would bind all your ips. Check to see if peerConfig lets you pick what ip you are binding to the default may be 127.0.0.1
Also could be a problem with a Router. Try making your computer the DMZ or enabling port forwarding.
I dont think this is code related.
Your router would need to have the host configured because you are trying to connect directly to the public end of your router from the private side of it.
If you just want to be able to connect to the dyndns address so you dont have to change code you can also alter the windows hosts file.
Many consumer routers use the source of a packet to determine whether to treat the data as internal or external. Put another way, if you are on your internal network and try to access any of the IP addresses of your router, your router will treat it as being internal (i.e. responding to port 80 with a customization website, most others with no response).
Unfortunately solutions are hard to come by.

How can I tell which of multiple network cards is in use using C#?

How can I know which client IP is used for communication to the server in C# if I have multiple network cards in client system (so that there will be multiple IP addresses too) ?
I would like to get private IP (not the public IP) .
OR
Please be patient to read the details...
Suppose I have multiple network cards connected to my PC, how can I know which card is used for communication. For example, if I use
ping 192.168.x.xxx
I would like to know which card (and which IP address) used for communication.
The command
netstat
will tell us about the active connections. I am not interested in active connections, but I would like to know which card will be used for next connection. That is, if we use ping which card (or IP address) will be used ?
How can I get the same IP address using C# (because I would like to do some stuff in WCF which needs endpoint IP addresses )?
For example, I would like to use the IP(as LocalIP) and port(as ListeningPort) to create the following
net.tcp:// + LocalIP + ":" + ListeningPort + "/Service/ep1;
A reference of the query can be found at...
https://superuser.com/questions/304372/how-can-i-tell-which-of-multiple-network-cards-is-in-use
You can connect to remote host using Socket and set specific IP address using Bind method.

Categories

Resources