我想得到本机的IP 写了一段代码:如下,
string hostname = Dns.GetHostName();
Response.Write("Hostname: "+hostname); IPAddress[] _ips = Dns.GetHostAddresses(Dns.GetHostName());
int i = 0;
foreach (IPAddress ip in _ips)
{
Response.Write((i++).ToString()+": |");
Response.Write(" |"+ ip+"| ");
}最后输出:Hostname: JZ-xinsiyu0: | |fe80::2000:51:8b18:eb19%13| 1: | |192.168.1.25| 2: | |2001:0:cf2e:3096:2000:51:8b18:eb19| 不知道|fe80::2000:51:8b18:eb19%13 和2001:0:cf2e:3096:2000:51:8b18:eb1分别代表什么啊?
string hostname = Dns.GetHostName();
Response.Write("Hostname: "+hostname); IPAddress[] _ips = Dns.GetHostAddresses(Dns.GetHostName());
int i = 0;
foreach (IPAddress ip in _ips)
{
Response.Write((i++).ToString()+": |");
Response.Write(" |"+ ip+"| ");
}最后输出:Hostname: JZ-xinsiyu0: | |fe80::2000:51:8b18:eb19%13| 1: | |192.168.1.25| 2: | |2001:0:cf2e:3096:2000:51:8b18:eb19| 不知道|fe80::2000:51:8b18:eb19%13 和2001:0:cf2e:3096:2000:51:8b18:eb1分别代表什么啊?
//验证数据合法性
if (!ValidateInfo())
{
return;
}
int port = int.Parse(myport);
//向服务器发出连接请求
TCPConnection conn = new TCPConnection(_ipAddr, port);
TcpClient _tcpc = conn.Connect();
if (_tcpc == null)
{
MessageBox.Show("无法连接到服务器,请重试!",
"错误",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
else
{ ........
}我想知道 我的 ipAddr 应该添哪个地址了,是{192.168.1.25}吗?谢谢楼上几们的关注。
貌似做socket的时候不能用本机给本机发啊?原来那样识别不出来。。
好像以下的就可以吧:
class TCPConnection
{
private IPAddress _ip = null;
private int _port;
private TcpClient _tcpc = null;//为 TCP 网络服务提供客户端连接 public TCPConnection(IPAddress ip, int port)
{
_ip = ip;
_port = port;
} public TcpClient Connect()
{
try
{
_tcpc = new TcpClient();
_tcpc.Connect(_ip, _port);//连接到服务
}
catch (Exception)
{
return null;
}
return _tcpc;
}
}
private IPAddress _ipAddr;
IPAddress.TryParse(myipstring, out _ipAddr);
int port = int.Parse(svrport_tb.Text);
//向服务器发出连接请求
TCPConnection conn = new TCPConnection(_ipAddr, port);
TcpClient _tcpc = conn.Connect();
if (_tcpc == null)
{
MessageBox.Show("无法连接到服务器,请重试!",
"错误",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
else
{
...................
}
可以放给本地的只不过你获取ip的时候应该这样
IPAddress[] ipad = Dns.GetHostAddresses("localhost");
使用的时候直接 ipad[0]就可以了 譬如TcpListener tl = new TcpListener(ipad[0],2000);