|
private void button1_Click(object sender, System.EventArgs e)
{
IPAddress ipRemote ;
try
{
ipRemote = IPAddress.Parse ( textBox1.Text ) ;
}
catch //判断给定的IP地址的合法性
{
MessageBox.Show ( "输入的IP地址不合法!" , "错误提示!" ) ;
return ;
}
IPHostEntry ipHost ;
try
{
ipHost = Dns.Resolve ( textBox1.Text ) ;
}
catch //判断IP地址对应主机是否在线
{
MessageBox.Show ("远程主机不在线!" , "错误提示!" ) ;
return ;
}
string sHostName = ipHost.HostName ;
try
{
TcpClient tcpClient = new TcpClient(sHostName,8000);//对远程主机的8000端口提出TCP连接申请
nsStream = tcpClient.GetStream();//通过申请,并获取传送数据的网络基础数据流
swWriter = new StreamWriter(nsStream);//使用获取的网络基础数据流来初始化StreamWriter实例
button1.Enabled = false ;
button2.Enabled = true ;
tcpConnect = true ;
statusBar1.Text = "已经连接!" ;
}
catch
{
MessageBox.Show ( "无法和远程主机8000端口建立连接!" , "错误提示!" ) ;
return ;
}
}
//发送
private void button2_Click(object sender, System.EventArgs e)
{
if (textBox2.Text !="")
{
swWriter.WriteLine(textBox2.Text);//刷新当前数据流中的数据
swWriter.Flush();
}
else
{
MessageBox.Show("发送信息不能为空!","错误提示!");
}
}
// 清理所有正在使用的资源。
protected override void Dispose( bool disposing )
{
if ( tcpConnect )
{
swWriter.WriteLi 上一页 [1] [2] [3] [4] [5] [6] 下一页 |