Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.44/5 (7 votes)
See more:
How to write my own firewall to detect the infiltrator ip . It is not a web application .

How to make application like Wireshark or Ethereal

How to Detect IP address in console/windows application in c# ?

Doing it in Web site is easy but i am not getting how to detect the ip of incoming packet / request in the Console/ windows application.

How to get the ip of an unknown machine if it is saving some file on my machine in a console / Windows application
Posted
Updated 7-Oct-12 20:17pm
v8
Comments
Bernhard Hiller 17-Sep-12 4:44am    
It is very unclear what you want to do.
What's that console/Windows application? Is that something you want to write and use as a monitor for such accesses?
Or is the issue something totally different. E.g. someone accesses your machine's hard disk, e.g. by writing to a shared directory, and you want to see that access somehow? Like the Network tab on Windows 7's Resource Monitor?
Member 8476555 17-Sep-12 5:04am    
want to find ip address of a machine saving a file on my pc

Try this code:
using System.Net;
C#
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
foreach(IPAddress address in localIPs)
   MessageBox.Show(address.ToString());
 
Share this answer
 
Comments
AmitGajjar 14-Sep-12 9:08am    
5+
Member 8476555 16-Sep-12 23:37pm    
Doing it in Web site is easy but i am not getting how to detect the ip of incoming packet / request in the Console/ windows application.
Ekta Mehta 6-Jan-15 5:36am    
please tell me which namespace need to write before use ipendpoint and IPaddress because i get error in name space missing... :(
JF2015 6-Jan-15 5:40am    
As I already wrote: The "System.Net" namespace must be used.
Member 13738163 1-Jul-18 7:57am    
How can I get only ipv4 address only?
1.1 Framework:
C#
StringBuilder sb = new StringBuilder();
string strHostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
IPAddress[] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
    sb.Append(addr[i].ToString());
}
string myip = sb.ToString();


2.0 or later framework
C#
StringBuilder sb = new StringBuilder();
// Get the hostname
string myHost = System.Net.Dns.GetHostName();
System.Net.IPHostEntry myIPs = System.Net.Dns.GetHostEntry(myHost);
foreach(System.Net.IPAddress myIP in myIPs.AddressList)
sb.Append(myIP.ToString());

string myip = sb.ToString();
 
Share this answer
 
v2
Comments
Member 8476555 16-Sep-12 23:37pm    
Doing it in Web site is easy but i am not getting how to detect the ip of incoming packet / request in the Console/ windows application.
hi dear

try below code

C#
public void getLocalIPAddress()
{
   try
   {
     string sHostName = Dns.GetHostName();
     IPHostEntry ipE = Dns.GetHostByName(sHostName);
     IPAddress[] IpA = ipE.AddressList;
     for (int i = 0; i < IpA.Length; i++)
     {
         Console.WriteLine("IP Address {0}: {1} ", i, IpA[i].ToString());
     }
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message.ToString(), "getLocalIPAddress");
   }
}
 
Share this answer
 
Comments
Member 8476555 16-Sep-12 23:38pm    
Doing it in Web site is easy but i am not getting how to detect the ip of incoming packet / request in the Console/ windows application.
You would have to write your own firewall to do this. You would also need to write a filter driver to insert into the network stack so you can pick out all the inbound traffic and get the addresses you're interested in.

Oh, and you'll probably need to write some heuristics into your "filter" so you can get a better idea of what is and is not "unknown".

Good Luck!
 
Share this answer
 
Comments
Member 8476555 8-Oct-12 0:36am    
How to write your own firewall ?
Dave Kreskowiak 8-Oct-12 8:06am    
That would take an entire book to discuss. That's FAR more information than you're going to get in a couple of forum posts.
 
Share this answer
 
Comments
Unareshraju 14-Sep-12 7:25am    
http://www.aspdotnet-suresh.com/2012/07/how-to-get-find-ip-address-of-client.html
Kuthuparakkal 14-Sep-12 7:35am    
ASP.Net solution link for Windows Form Application related question!!!
Unareshraju 14-Sep-12 8:32am    
we don't get built in answer for every question , we need to change and develop the code .........
Member 8476555 16-Sep-12 23:38pm    
Doing it in Web site is easy but i am not getting how to detect the ip of incoming packet / request in the Console/ windows application.

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