原文 获取外网IP, C#获取本机的MAC地址C#通过编程方式实现Ping

获取外网IP地址

思路是通过WebRequest连接一些网上提供IP查询服务的网站,下载到含有你的IP的网页,然后用正则表达式提取出IP来

class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetExportIP()); Console.ReadKey();
} public static string GetExportIP()
{
//获取外部IP
string strUrl = "http://www.ip.cn/getip.php?action=getip&ip_url=&from=web";
//string strUrl = "http://216.157.85.151/getip.php?action=getip&ip_url=&from=web";
Uri uri = new Uri(strUrl);
WebRequest webreq = WebRequest.Create(uri);
Stream s = webreq.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd();
all = Regex.Replace(all,@"(\d+)","000$1");
all = Regex.Replace(all, @"0+(\d{3})", "$1");
string reg = @"(\d{3}\.\d{3}\.\d{3}\.\d{3})";
Regex regex = new Regex(reg);
Match match = regex.Match(all);
string ip = match.Groups[].Value;
return Regex.Replace(ip,@"0*(\d+)","$1");
}
}

获取本机MAC

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management; namespace _17获取MAC地址
{
class Program
{
static void Main(string[] args)
{
ManagementObjectSearcher nisc = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration");
foreach (ManagementObject nic in nisc.Get())
{
if (Convert.ToBoolean(nic["ipEnabled"]) == true)
{
Console.WriteLine("{0} - {1}", nic["ServiceName"], nic["MACAddress"]);
}
} Console.ReadKey();
}
}
}

Ping

废话少说,具体代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation; namespace _20通过编程方式实现Ping
{
class Program
{
static void Main(string[] args)
{
Ping ping = new Ping();
PingOptions pingOpt = new PingOptions();
pingOpt.DontFragment = true;//是否设置分段数据
string myInfo = "Hello, world!";
byte[] bufferInfo = Encoding.ASCII.GetBytes(myInfo);
int timeOut = ;
string ipTarget = "192.168.1.102";
PingReply pingReply = ping.Send(ipTarget, timeOut, bufferInfo);
if (pingReply.Status == IPStatus.Success)
{
Console.WriteLine("耗费时间 - {0}\n路由节点数 - {1}\n数据分段 - {2}\n缓冲区大小 - {3}", pingReply.RoundtripTime, //耗费时间
pingReply.Options.Ttl, //路由节点数
pingReply.Options.DontFragment ? "发生分段" : "没有发生分段",//数据分段
pingReply.Buffer.Length//缓冲区大小
);
}
else
{
Console.WriteLine("无法ping通");
} Console.ReadKey();
}
}
}

C# 实现ping的功能

C#获取外网IP、本机MAC地址及Ping的实现的更多相关文章

  1. Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer 关于Newtonsoft.Json,LINQ to JSON的一个小demo mysql循环插入数据、生成随机数及CONCAT函数 .NET记录-获取外网IP以及判断该IP是属于网通还是电信 Guid的生成和数据修整(去除空格和小写字符)

    Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer   Advanced Installer :Free for 30 da ...

  2. c#获取外网IP地址的方法

    1.如果你是通过路由上网的,可以通过访问ip138之类的地址来获取外网IP 2.如果是通过PPPOE拨号上网的,可以使用以下代码获取IP //获取宽带连接(PPPOE拨号)的IP地址,timeout超 ...

  3. Curl之获取外网IP

    获取外网IP:curl -s ifconfig.me

  4. .NET记录-获取外网IP以及判断该IP是属于网通还是电信

    在工作时,需要获取服务器公网IP(外网IP),并且判断该IP地址是属于网通还是电信.花时间整理一下,自己实现的代码,以及后续遇到的问题. /// <summary> /// 获取外网IP ...

  5. JAVA 优先获取外网Ip,再获取内网Ip

    1.获取内网Ip private String getLocalhostIp(){ String hostAddress = ""; try { InetAddress addre ...

  6. linux获取外网ip

    引言:目前获取ip的方法中,ifconfig和ip获取函数得到的都是内网ip.有时候需要获取外网ip,目前通用的做法,是向外部服务器发送请求,解析外部服务器响应,从而得到的自己的外网ip.linux下 ...

  7. java获取外网ip地址

    转自:http://blog.163.com/houjunchang_daxue/blog/static/13037938320134543310451/ /** * 获取外网IP.归属地.操作系统 ...

  8. C# 获取外网IP地址

    很多情况下我们需要获取外网的IP地址,一般用自带的方法获取到的都是不准确,往往获取到的是内网的IP地址,所以需要采用外部网站接口来获取. 代码 通过访问第三方接口来获取真实的ip地址 public s ...

  9. Android 获取外网IP,实测有效

    网上有很多获取IP的例子,不过都是获取到的本地ip,还有的是因为走不通了,获取到的ip为空,下面看实测获取到外网IP的代码,注意需要在线程里面执行 /** * 获取外网的IP(要访问Url,要放到后台 ...

随机推荐

  1. LARAVEL 路由原理分析

    <?php class App {    protected $routes = [];    protected $responseStatus = '200 OK';    protecte ...

  2. 关于dyld: Library not loaded

    在接入智凡迪的sdk过程中,遇到以下问题: dyld: Library not loaded: @rpath/SDKFramework.framework/SDKFramework   Referen ...

  3. Oracle基本概念与数据导入

    Oracle基本概念 实例 一个Oracle实例(Oracle Instance)有一系列的后台进程(Backguound Processes)和内存结构(Memory Structures)组成.一 ...

  4. CF1109A Sasha and a Bit of Relax

    CF1109A Sasha and a Bit of Relax 用 \(xorsum[l,r]\) 表示 \(a[l] \oplus a[l+1] \oplus a[l+2]... a[r-1] \ ...

  5. 为什么委托的减法(- 或 -=)可能出现非预期的结果?(Delegate Subtraction Has Unpredictable Result)

    当我们为一个委托写 -= 的时候,ReSharper 会提示“Delegate Subtraction Has Unpredictable Result”,即“委托的减法可能出现非预期的结果”.然而在 ...

  6. Ubuntu搭建 Apache、MySQL、PHP环境

    以Ubuntu 16.04为例: 1.安装MysSQL 打开命令行输入 :sudo apt-get install mysql-server 输入管理员密码 选择Y 在安装的中间会出现输入Mysql的 ...

  7. python 开始学习

    "人生苦短, 我用python" ---------以此敬意伟大的生产力创造!

  8. vs2013 boost signals

    #include "stdafx.h" #include <boost/signals2/signal.hpp> #include <iostream> u ...

  9. grpc rust 项目基本使用

    1. 安装依赖(rust 基本依赖就不说了,需要配置环境变量) protoc 参考: https://github.com/google/protobuf/releases/tag/v3.5.1 2. ...

  10. 40+个对初学者非常有用的PHP技巧

    1.不要使用相对路径,要定义一个根路径 这样的代码行很常见: require_once('../../lib/some_class.php'); 这种方法有很多缺点: 它首先搜索php包括路径中的指定 ...