private void button1_Click
( object sender , System.EventArgs e )
{
//以下代码是判断是否和远程终结点成功连接
try
{
stSend = new Socket ( AddressFamily.InterNetwork ,
SocketType.Stream , ProtocolType.Tcp ) ;
//初始化一个Socket实例
IPEndPoint tempRemoteIP = new IPEndPoint
( IPAddress.Parse ( textBox1.Text ) , port ) ;
//根据IP地址和端口号创建远程终结点
EndPoint epTemp = ( EndPoint ) tempRemoteIP ;
stSend.Connect ( epTemp ) ;
//连接远程主机的8000端口号
statusBar1.Text = "成功连接远程计算机!" ;
tcpConnect = true ;
button1.Enabled = false ;
button2.Enabled = true ;
}
catch ( Exception )
{
statusBar1.Text = "目标计算机拒绝连接请求!" ;
}
}
|