日期:2014-05-18  浏览次数:20434 次

asp.net或是浏览器如何获得外网IP
asp.net或是浏览器如何获得外网IP?不是内网的,是外网ip

------解决方案--------------------
将用到SharpPcap

using System;
using System.Collections.Generic;
using System.Text;
using Tamir.IPLib;
using Tamir.IPLib.Packets;

namespace PacketGeter
{
class Program
{
private static PcapDevice device;
private static string MyMAC = "本机MAC";
private static string MyIP = "本机IP";

public static void Main(string[] args)
{
string ver = Tamir.IPLib.Version.GetVersionString();
/* Print SharpPcap version */
Console.WriteLine("SharpPcap {0}, Example3.BasicCap.cs", ver);
/* Retrieve the device list */
PcapDeviceList devices = SharpPcap.GetAllDevices();
/*If no device exists, print error */
if (devices.Count < 1)
{
Console.WriteLine("No device found on this machine");
return;
}
Console.WriteLine();
Console.WriteLine("The following devices are available on this machine:");
Console.WriteLine("----------------------------------------------------");
Console.WriteLine();
int i = 0;
/* Scan the list printing every entry */
foreach (PcapDevice dev in devices)
{
/* Description */
Console.WriteLine("{0}) {1}", i, dev.PcapDescription);
i++;
}
Console.WriteLine();
Console.Write("-- Please choose a device to capture: ");
i = int.Parse(Console.ReadLine());
device = devices[i];
//Register our handler function to the 'packet arrival' event
device.PcapOnPacketArrival +=
new SharpPcap.PacketArrivalEvent(device_PcapOnPacketArrival);
//Open the device for capturing
//true -- means promiscuous mode
//1000 -- means a read wait of 1000ms
device.PcapOpen(false, 1000);
Console.WriteLine();
Console.WriteLine("-- Listenning on {0}, hit 'Enter' to stop...",
device.PcapDescription);
//Start the capturing process
device.PcapStartCapture();
//Wait for 'Enter' from the user.
Console.ReadLine();
//Stop the capturing process
device.PcapStopCapture();
Console.WriteLine("-- Capture stopped.");
Console.Read();
//Close the pcap device
device.PcapClose();
}
///<summary>
/// Prints the time and length of each received packet
///</summary>
private static void device_PcapOnPacketArrival(object sender, Packet packet)
{

if (packet is TCPPacket)
{
DateTime time = packet.Timeval.Date;
int len = packet.PcapHeader.PacketLength;
TCPPacket tcp = (TCPPacket)packet;

string srcIp = tcp.SourceAddress;
string dstIp = tcp.DestinationAddress;
int srcPort = tcp.SourcePort;
int dstPort = tcp.DestinationPort;
string desMAC = tcp.DestinationHwAddress;
string sMac = tcp.SourceHwAddress;
Console.WriteLine("{0}:{1}:{2},{3} Len={4} {5}:{6} -> {7}:{8}",
time.Hour, time.Minute, time.Second,
tim