winform如何获得访问IP

mwsz_666 2010-11-02 11:41:13
我有个c/s程序,c#编写。

我现在有个问题:就是我在服务端如何获得客户端访问的公网IP,还有就是我如何知道访问的这个客户端和我在同一个局域网里。
...全文
228 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
herott632482577 2010-11-02
  • 打赏
  • 举报
回复
private void GroupInfo()
{
DirectoryEntry MainGroup = new DirectoryEntry("WinNT:");
foreach(DirectoryEntry domain in MainGroup.Children)
{
this.listView1.Items.Add(domain.Name);

}
}

private void ComputerInfo(String strname)
{
try
{
DirectoryEntry MainGroup = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry domain in MainGroup.Children)
{
if (domain.Name == strname)
{
foreach (DirectoryEntry pc in domain.Children)
{
IPAddress[] ip = Dns.GetHostAddresses(pc.Name);
this.listView2.Items.Add(pc.Name + ip[0].ToString());
}

}

}
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}

}


kokorenfeng 2010-11-02
  • 打赏
  • 举报
回复
System.Net.IPHostEntry localhost;
System.Net.IPAddress localaddr;

string hostname;
hostname=System.Net.Dns.GetHostName();
localhost=System.Net.Dns.GetHostByName(hostname);
localaddr=localhost.AddressList[0]; //localaddr中就是本机ip地址
你试试看,
kokorenfeng 2010-11-02
  • 打赏
  • 举报
回复
Request.UserHostAddress();
mwsz_666 2010-11-02
  • 打赏
  • 举报
回复
winform程序 有Request吗?
mayonglong 2010-11-02
  • 打赏
  • 举报
回复
Request.ServerVariables.Get("Remote_Addr").ToString();
dacan66 2010-11-02
  • 打赏
  • 举报
回复

public static string GetRealIp()
{
try
{
string strUrl = "http://www.ip138.com/ip2city.asp";
//获得IP的网址了
Uri uri = new Uri(strUrl);
System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);
System.IO.Stream s = wr.GetResponse().GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd();
//读取网站的数据
int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
string ip = tempip.Replace("]", "").Replace(" ", "");
return ip;
}
catch (Exception ex)
{
return "";
}
}
*********************************************************** 欢迎使用 CSDN 小秘书
 http://blog.csdn.net/whowhen21***********************************************************
jieon 2010-11-02
  • 打赏
  • 举报
回复
client.RemoteEndPoint 可以获得连接方的公网IP,如果是同一内网的则是内网IP。
内网IP可以遍历后传输给服务器。
内外网判断。可以通过测试连接对方内网IP
萨拉嘿 2010-11-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kokorenfeng 的回复:]
System.Net.IPHostEntry localhost;
System.Net.IPAddress localaddr;

string hostname;
hostname=System.Net.Dns.GetHostName();
localhost=System.Net.Dns.GetHostByName(hostname);
localaddr=localhost.A……
[/Quote]
在个补充下。。64位的机器AddressList[1]是IP
萨拉嘿 2010-11-02
  • 打赏
  • 举报
回复
TCPClient和UDPCLient的都有client.RemoteEndPoint代表连接的客户端IP地址 再个我认为外网IP相同就是在一个网段。。。
明哥说编程 2010-11-02
  • 打赏
  • 举报
回复

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧