Current IP address in use - c#

I have a C# Win 7 desktop application that sends web requests using HttpClient. Is there any way to find out which network adapter or NetworkInterface is used to send these requests? (There could be multiple LAN, WAN and virtual adapters.)
I don't really want to use TcpClient, UdpClient or any socket level program as it more low level and user may decide to block the ports.
One route I am trying to pursue is to find IP address of the current request and then try to match it with the IP address from the NetworkInterface (using NetworkInterface.GetAllNetworkInterfaces), but HttpClient don't tell you your IP that is used to send the requests.

This is what I ended up doing:
IPAddress localAddr = null;
try
{
using (UdpClient udpClient = new UdpClient("8.8.8.8", 0)) //connect to google dns
{
localAddr = ((IPEndPoint)udpClient.Client.LocalEndPoint).Address;
udpClient.Client.Close();
udpClient.Close();
}
}
catch (SocketException sex)
{
Console.WriteLine("Failed to make UDP connection, no connection to DNS ? ");
}
//find all network interfaces
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
NetworkInterface activeAdapter = null;
foreach (NetworkInterface adapter in nics.Where(n => n.OperationalStatus == OperationalStatus.Up))
{
//wifi adapter will be 'up' only if its associated with a hotspot
IPInterfaceProperties properties = adapter.GetIPProperties();
var match = properties.UnicastAddresses.Any(a => a.Address.Equals(localAddr));
if (match)
{
activeAdapter = adapter;
break;
}
}
I had to rely on UDP client, can't find a way to get ip from HttpClient
This will get you the current 'active' network adapter that is being used to connect to internet.

Related

Get local IP address of ethernet interface in C#

Is there a reliable way to get the IPv4 address of the first local Ethernet interface in C#?
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{...
This finds the local IP address associated with the Ethernet adapter but also find the Npcap Loopback Adapter (installed for use with Wireshark).
Similarly, there seems to be no way to tell the difference between the loopback address and the Ethernet address using the following code:
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{....
Any other suggestions?
The following code gets the IPv4 from the preferred interface. This should also work inside virtual machines.
using System.Net;
using System.Net.Sockets;
public static void getIPv4()
{
try
{
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
{
socket.Connect("10.0.1.20", 1337); // doesnt matter what it connects to
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
Console.WriteLine(endPoint.Address.ToString()); //ipv4
}
}
catch (Exception)
{
Console.WriteLine("Failed"); // If no connection is found
}
}

Other computers can't connect my program

I'm building a C# chat program, yet I'm facing a problem with outside connection.
When the same computer connects both as server and as client, there seems to be no problem, yet when I try to host the connection on one computer, the other can't connect as a client.
here's the relevant code:
class Server:
public void Connect(string ipAddr, string port)
{
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, Convert.ToInt32(port));
server.Bind(ipLocal);//bind to the local IP Address...
server.Listen(5);//start listening...
// create the call back for any client connections...
server.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
public void Disconnect()
{
server.Close();
server = null;
tempSocket = null;
}
public void OnClientConnect(IAsyncResult asyn)
{
try
{
if (server != null)
{
tempSocket = server.EndAccept(asyn);
WaitForData(tempSocket);
server.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
}
catch (ObjectDisposedException)
{
Debugger.Log(0, "1", "OnClientConnect: Socket has been closed.");
}
catch (Exception e)
{
MessageBox.Show(e.Message, "OnClientConnect Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Client class:
public void Connect(string ipAddr, string port)
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ipAddr), Convert.ToInt32(port));
client.Connect(ipe);
clientListener = new Thread(OnDataReceived);
isEndClientListener = false;
clientListener.Start();
}
I have no idea what's wrong here. Hope you can tell me what's wrong.
Your issue is probably not code related. In order for other people outside your network to conenect to you, you need to port forward the port that you are connecting through on your router. You can find many tutorials here. You may also check to see if your connection is open through this tool.
From Wikipedia:
Port forwarding allows remote computers (for example, computers on the Internet) to connect to a specific computer or service within a private local-area network (LAN).
You must allow connections through your router to be able to connect to your chat server.
You need to give your computer a public IP address (maybe your router has this option) or implement port forwarding on your router.
A Public IP address would be the one of your router. Check out this site to find out your public IP whatismyipaddress.com. Your router can or cannot support the option to give its public IP address to your computer, however, your router should be able to do port forwarding. (Forwarding the data from a specific port to a specific computer, so when someone connects to your public IP. For example 93.93.93.93:3333 will be forwarded to your PC.)

C# Get IP assigned by router

I don't know if I'm just not searching using the right keywords, I want to be able to find the IP address of my local computer that was assigned by my router.
I was using:
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach(IPAddress ip in host.AddressList)
{
if(ip.addressfamily.tostring() == "InterNetwork")
{
return ip;
}
}
The problem is I have multiple InterNetwork ip addresses because I use virtual services, so I need to be able to identify which one was assigned by my router.
How about loopback?
if (IPAddress.IsLoopback(ip)) return ip; //localhost
Or try pinging the local machine
Ping pingSender = new Ping ();
IPAddress address = IPAddress.Loopback;
PingReply reply = pingSender.Send (address);
if (reply.Status == IPStatus.Success){..}
also this might help you
Showing The External IP-Address In C#

Using TCP outside the LAN

I'm using the the examples provided by Microsoft to learn how to use TCP servers in C#. For TCPListener I use this http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx , and for TCPCLient I use this http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx (the examples are at the bottom of the page).
Until now I've managed to connect and send messages to other PCs connected to the same router. What I want now is to connect it to a PC outside my LAN network. How can I do that ?
I should also mention that this is the way that I use to connect PCs in LAN :
on ther server side:
public string LocalIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
}
private void Form1_Load(object sender, EventArgs e)
{
TcpListener server = null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
String localAddrString = LocalIPAddress();
Console.WriteLine(localAddrString);
IPAddress localAddr = IPAddress.Parse(localAddrString);
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
}
}
on the client side:
Int32 port = 13000;
String server = "192.168.X.X"; // here I manually introduce the IP provided by the server in the console
TcpClient client = new TcpClient(server, port);
I wish I could use a simple comment to give you this information but I cannot due to me only recently joining SO. You should ensure you have port forwarded (http://portforward.com/ will help you port forward), if you don't know how to you could use this easy-to-use port checker: http://www.yougetsignal.com/tools/open-ports/.

Receiving udp packet on designated network card c#

I have 3 different network cards each with their individual responsibility. Two of the cards are receiving packets from a similar device (plugged directly into each individual network card) which sends data on the same port. I need to save the packets knowing which device they came from.
Given that I am required to not specify the ip address of the devices sending me the packets, how can I listen on a given network card? I am allowed to specify a static ip address for all 3 nics if needed.
Example: nic1 = 169.254.0.27, nic2 = 169.254.0.28, nic3 = 169.254.0.29
Right now I have this receiving the data from nic1 and nic2 without knowing which device it came from.
var myClient = new UdpClient(2000) //Port is random example
var endPoint = new IPEndPoint(IPAddress.Any, 0):
while (!finished)
{
byte[] receivedBytes = myClient.Receive(ref endPoint);
doStuff(receivedBytes);
}
I can't seem to specify the static ip address of the network cards in a manner which will allow me to capture the packets from just one of the devices. How can I separate these packets with only the knowledge that they are coming in on two different network cards?
Thank you.
You're not telling the UdpClient what IP endpoint to listen on. Even if you were to replace IPAddress.Any with the endpoint of your network card, you'd still have the same problem.
If you want to tell the UdpClient to receive packets on a specific network card, you have to specify the IP address of that card in the constructor. Like so:
var listenEndpoint = new IPEndPoint(IPAddress.Parse("192.168.1.2"), 2000);
var myClient = new UdpClient(listenEndpoint);
Now, you may ask "What's the ref endPoint part for when I'm calling myClient.Receive(ref endPoint)?" That endpoint is the IP endpoint of the client. I would suggest replacing your code with something like this:
IPEndpoint clientEndpoint = null;
while (!finished)
{
var receivedBytes = myClient.Receive(ref clientEndpoint);
// clientEndpoint is no longer null - it is now populated
// with the IP address of the client that just sent you data
}
So now you have two endpoints:
listenEndpoint, passed in through the constructor, specifying the address of the network card you want to listen on.
clientEndpoint, passed in as a ref parameter to Receive(), which will be populated with the client's IP address so you know who is talking to you.
Check this out this:
foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
Console.WriteLine("Name: " + netInterface.Name);
Console.WriteLine("Description: " + netInterface.Description);
Console.WriteLine("Addresses: ");
IPInterfaceProperties ipProps = netInterface.GetIPProperties();
foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses)
{
Console.WriteLine(" " + addr.Address.ToString());
}
Console.WriteLine("");
}
Then you can choose on which address start listening.
look, if you create your IPEndPoint in the following way it must work:
IPHostEntry hostEntry = null;
// Get host related information.
hostEntry = Dns.GetHostEntry(server);
foreach(IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
...
try to do not pass 0 as port but a valid port number, if you run this code and break the foreach after the first iteration you will have created only 1 IPEndPoint and you can use that one in your call to: myClient.Receive
notice that the UdpClient class has a member calledd Client which is a socket, try to explore the properties of that object as well to find out some details, I have found the code I gave you here: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

Categories