您的位置:首页 > 其它

通过ip地址及端口连接服务器

2016-08-17 14:08 260 查看
try
{
int port = Convert.ToInt32(txtPortNumber.Text);
string host = txtServerIP.Text;
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
c.Connect(ipe);//连接到服务器
//c.SendFile(@"C:\数据库升级\aaa.txt");
//c.Shutdown(SocketShutdown.Both);
MessageBox.Show("连接服务器成功!","提示");//显示服务器返回信息
c.Close();
}
catch (ArgumentNullException ex)
{
MessageBox.Show("ArgumentNullException: "+ex);
}
catch (SocketException ex)
{
MessageBox.Show("SocketException: " +ex);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ip 服务器