111,120
社区成员
发帖
与我相关
我的任务
分享 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|
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
{
...................
}
//验证数据合法性
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
{ ........
}