
using System;
using SystemRuntimeInteropServices ;
using SystemCollectionsGeneric ;
using SystemNet ;
using SystemNetSockets ;
using SystemIO ;
using ESBasicHelpers ;
using ESBasicNetworkTcp;
namespace ESBasicNetwork
{
/// <summary>
/// NetHelper 。
/// </summary>
public static class NetHelper
{
#region IsPublicIPAddress
public static bool IsPublicIPAddress(string ip)
{
if(ipStartsWith("10")) //A类 10000到10255255255255
{
return false ;
}
if(ipStartsWith("172"))//B类 1721600到17231255255
{
if(ipSubstring(6 ,1) == "")
{
int secPart = intParse(ipSubstring(4 ,2)) ;
if((16 <= secPart) && (secPart <= 31) )
{
return false ;
}
}
}
if(ipStartsWith("192168"))//C类 19216800到192168255255
{
return false ;
}
return true ;
}
#endregion
#region ReceiveData
/// <summary>
/// ReceiveData 从网络读取指定长度的数据
/// </summary>
public static byte[] ReceiveData(NetworkStream stream ,int size)
{
byte[] result = new byte[size] ;
NetHelperReceiveData(stream ,result ,0 ,size) ;
return result ;
}
/// <summary>
/// ReceiveData 从网络读取指定长度的数据 ,存放在buff中offset处
/// </summary>
public static void ReceiveData(NetworkStream stream ,byte[] buff ,int offset ,int size)
{
int readCount = 0 ;
int totalCount = 0 ;
int curOffset = offset ;
while(totalCount < size)
{
int exceptSize = size - totalCount ;
readCount = streamRead(buff ,curOffset ,exceptSize) ;
if(readCount == 0)
{
throw new IOException("NetworkStream Interruptted !") ;
}
curOffset += readCount ;
totalCount += readCount ;
}
}
/// <summary>
/// ReceiveData 从网络读取指定长度的数据
/// </summary>
public static byte[] ReceiveData(ISafeNetworkStream stream ,int size)
{
byte[] result = new byte[size] ;
NetHelperReceiveData(stream ,result ,0 ,size) ;
return result ;
}
/// <summary>
/// ReceiveData 从网络读取指定长度的数据 ,存放在buff中offset处
/// </summary>
public static void ReceiveData(ISafeNetworkStream stream, byte[] buff, int offset, int size)
{
int readCount = 0 ;
int totalCount = 0 ;
int curOffset = offset ;
while(totalCount < size)
{
int exceptSize = size - totalCount ;
readCount = streamRead(buff ,curOffset ,exceptSize) ;
if(readCount == 0)
{
throw new IOException("NetworkStream Interruptted !") ;
}
curOffset += readCount ;
totalCount += readCount ;
}
}
#endregion
#region GetRemotingHanler
//前提是已经注册了remoting通道
public static object GetRemotingHanler(string channelTypeStr ,string ip ,int port ,string remotingServiceName ,Type destInterfaceType)
{
try
{
string remoteObjUri = stringFormat("{0}://{1}:{2}/{3}" ,channelTypeStr ,ip ,port ,remotingServiceName) ;
return ActivatorGetObject(destInterfaceType ,remoteObjUri);
}
catch
{
return null ;
}
}
#endregion
#region GetLocalIp
/// <summary>
/// </summary>
public static IPAddress[] GetLocalIp()
{
string hostName = DnsGetHostName() ;
IPHostEntry hEntry = DnsResolve(hostName) ;
return hEntryAddressList ;
}
public static IPAddress GetFirstLocalIp()
{
string hostName = DnsGetHostName();
IPHostEntry hEntry = DnsResolve(hostName);
return hEntryAddressList[0];
}
/// <summary>
/// GetLocalPublicIp 获取本机的公网IP地址
/// </summary>
public static string GetLocalPublicIp()
{
IPAddress[] list = NetHelperGetLocalIp();
foreach(IPAddress ip in list)
{
if(NetHelperIsPublicIPAddress(ipToString()))
{
return ipToString() ;
}
}
return null ;
}
#endregion
#region IsConnectedToInternet
/// <summary>
/// IsConnectedToInternet 机器是否联网
/// </summary>
public static bool IsConnectedToInternet()
{
int Desc=0;
return InternetGetConnectedState(Desc,0);
}
[DllImport("wininetdll")]
private extern static bool InternetGetConnectedState(int Description,int ReservedValue);
#endregion
#region GetMacAddress 获取网卡mac地址
/// <summary>
/// GetMacAddress 获取本机所有网卡的Mac地址
/// </summary>
public static IList<string> GetMacAddress()
{
return MachineHelperGetMacAddress();
}
#endregion
#region DownLoadFileFromUrl
/// <summary>
/// DownLoadFileFromUrl 将url处的文件下载到本地
/// </summary>
public static void DownLoadFileFromUrl(string url ,string saveFilePath)
{
FileStream fstream = new FileStream(saveFilePath ,FileModeCreate ,FileAccessWrite);
WebRequest wRequest = WebRequestCreate(url);
try
{
WebResponse wResponse = wRequestGetResponse();
int contentLength =(int)wResponseContentLength;
byte[] buffer = new byte[1024];
int read_count = 0 ;
int total_read_count = 0 ;
bool complete = false;
while (!complete )
{
read_count = wResponseGetResponseStream()Read(buffer,0,bufferLength);
if(read_count > 0)
{
fstreamWrite(buffer ,0 ,read_count) ;
total_read_count += read_count ;
}
else
{
complete = true ;
}
}
fstreamFlush() ;
}
finally
{
fstreamClose() ;
wRequest = null;
}
}
#endregion
}
}
这个类直接拿去用吧,命名空间改一下,里面有你想要的方法以及一些可能对你有用的方法
import javanet;
public class Test {
public static void main(String[] args) throws UnknownHostException {
String IP = null;
String host = null;
InetAddress ia;
ia = InetAddressgetLocalHost();
host = iagetHostName();// 获取计算机名字
IP = iagetHostAddress();// 获取IP
Systemoutprintln(host);
Systemoutprintln(IP);
}
}
1、鼠标右键单击桌面右下角的打开网络和共享中心,如图:
2、点击页面中的本地连接,如图:
3、在本地连接状态窗口中点击详细信息,如图:
4、在d出的网络连接详细信息窗口中,我们就能查看到自己的IP地址啦,如图:
以上就是查看IP地址的方法
版本 2
支持库 internet
子程序 取外网ip, 文本型, 公开
局部变量 网页, 字节集
局部变量 b, 整数型
局部变量 a, 整数型
局部变量 IP, 文本型
网页 = >
接口地址: >
打开命令行界面,Windows下按住Windows键和R键输入CMD,然后输入ipconfig /all可以查看到IP和Mac地址信息,Mac下打开Terminal,输入ifconfig可以查看到IP和Mac地址信息。
打开开始菜单,在d出的菜单中选择框框的这个地方,如果是xp的系统,那么你点击运行这个按钮。
在搜索框中输入cmd,如果是xp的系统,点击运行按钮后也一样输入cmd,然后回车。
在打开的窗口中输入ipconfig /all 可以看到好多连接,我的电脑是接路由器的,所以是本地连接,如果是直接拨号上网,那么看宽带连接,如果是无线连接的,看无线连接。
4
然后我们想看我们外网的ip地址怎么看呢。在浏览器的搜索窗口输入“ip地址查询”然后回车就能看见外网的ip地址了。
爬虫时,为了避免自己的IP被封,有时候需要设置反向代理,用其他的IP去爬。那么这里说的IP到底是电脑的什么IP呢?
首先看下 localhost 、 127001 、 192168xx 。
这里使用flask开启一个服务,这样本机可以通过 localhost 、 127001 、 192168xx (我的是 19216814 )等地址的5000端口(eg 19216814:5000 )访问服务器,而同一局域网的其它设备也可以通过 19216814:5000 访问我的服务器
这里的 localhost 一般默认指向 127001 ,所以只需要看IP
127001 是只能自己对自己使用的IP
192168xx 则是局域网下任何设备都可以访问其他设备使用的IP(包括自己对自己)
然后,如果我访问在线网站,或者爬虫,使用的IP并不是上面那两个IP,而是我的运营商分配给我的外网IP(public ip),作为我使用的这个网络的一个身份标识,一般反爬虫封的IP,也就是这个IP了
python版本:
nodejs版本:
参考链接:
以上就是关于C# 获取本机外网IP全部的内容,包括:C# 获取本机外网IP、我在本机上,想写一个java程序获取我本机的外网ip地址,如何搞、公司网用的是宽带,win7系统,怎么查外网ip和本地ip等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)