[导入]【原】C#里获取客户端IP,端口号的简单示例
2012-04-24 09:34
525 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;//为了IPEndPoint而添加的引用
using System.Net.Sockets;
namespace GetClntIP
{
class Program
{
static void Main(string[] args)
{
TcpListener tcpListener = new TcpListener(9000);//监听的端口号,可根据需要修改
tcpListener.Start();
//loop for listen
while (true)
{
Socket sock = tcpListener.AcceptSocket();
//服务器当前时间
DateTime connTime = DateTime.Now;
Console.WriteLine("The time is :"+connTime.ToString());
System.Net.IPAddress ipAdd;
int port;
//获得连接当前用户的IP以及端口号
ipAdd = (sock.RemoteEndPoint as IPEndPoint).Address;
port = (sock.RemoteEndPoint as IPEndPoint).Port;
Console.WriteLine("The client IP :"+ipAdd.ToString ());
Console.WriteLine("The client port :" + port.ToString());
}
tcpListener.Stop();
}
}
}
[附]获取本地IP:
//获得本地局域网IP地址
IPAddress [] AddressList=Dns.GetHostByName(Dns.GetHostName()).AddressList;
if(AddressList.Length<1)
{
retern "";
}
retern AddressList[0].ToString();//注意这里数组参数为0
//获得拨号动态分配IP地址
IPAdress [] AddressList=Dns.GetHostByName(Dns.GetHostName()).AddressList;
if(AddressList.Length<2)
{
return "";
}
return AddressList[1].ToString();//注意这里数组参数为1
文章来源:http://meymz.blog.163.com/blog/static/381209212007711021129
using System.Collections.Generic;
using System.Text;
using System.Net;//为了IPEndPoint而添加的引用
using System.Net.Sockets;
namespace GetClntIP
{
class Program
{
static void Main(string[] args)
{
TcpListener tcpListener = new TcpListener(9000);//监听的端口号,可根据需要修改
tcpListener.Start();
//loop for listen
while (true)
{
Socket sock = tcpListener.AcceptSocket();
//服务器当前时间
DateTime connTime = DateTime.Now;
Console.WriteLine("The time is :"+connTime.ToString());
System.Net.IPAddress ipAdd;
int port;
//获得连接当前用户的IP以及端口号
ipAdd = (sock.RemoteEndPoint as IPEndPoint).Address;
port = (sock.RemoteEndPoint as IPEndPoint).Port;
Console.WriteLine("The client IP :"+ipAdd.ToString ());
Console.WriteLine("The client port :" + port.ToString());
}
tcpListener.Stop();
}
}
}
[附]获取本地IP:
//获得本地局域网IP地址
IPAddress [] AddressList=Dns.GetHostByName(Dns.GetHostName()).AddressList;
if(AddressList.Length<1)
{
retern "";
}
retern AddressList[0].ToString();//注意这里数组参数为0
//获得拨号动态分配IP地址
IPAdress [] AddressList=Dns.GetHostByName(Dns.GetHostName()).AddressList;
if(AddressList.Length<2)
{
return "";
}
return AddressList[1].ToString();//注意这里数组参数为1
文章来源:http://meymz.blog.163.com/blog/static/381209212007711021129
相关文章推荐
- [导入]【原】C#里获取客户端IP,端口号的简单示例
- C#里获取客户端IP,端口号的简单示例
- C#获取本机外网IP最简单办法
- c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明
- 一个简单Spring+Hibernate+Struts2.0+Ajax整合获取客户端IP、URL和请求时间
- C#中如何获取服务器IP,名称,操作系统,客户端IP,名称!
- 简单介绍几种获取客户端真实IP可行方法
- 一个简单客户端获取IP,国家,城市,省份的代码
- C# 获取客户端IP
- js获取客户端外网ip的简单实例
- C#简单获取全屏中鼠标焦点位置坐标的方法示例
- C# 获取本地IP客户端IP地址以及MAC地址
- c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明
- c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明
- 牛腩购物:22 产品内容页制作 获取客户端ip,产品上一件,下一件,cte的简单使用
- c#中获取服务器IP,客户端IP以及其它
- C#中获取服务器IP,客户端IP以及网卡物理地址
- C# 获取 MAC 地址 IP 地址 最简单的
- [导入]ASP.NET获取IP与MAC[using C#]
- C# 之 获取服务器IP,客户端IP以及其它