之前做了一个TCP握手的工具,找了一些资料总结出代码
private void ServerConnect()
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(MainGlobalData.IPAddress);
IPEndPoint point = new IPEndPoint(ip, int.Parse("445"));
try
{
//连接到服务器
client.Connect(point);
//client.Disconnect(true);
ShowMsg("连接成功");
ShowMsg("服务器:" + client.RemoteEndPoint.ToString());
ShowMsg("客户端:" + client.LocalEndPoint.ToString());
}
catch (Exception ex)
{
ShowMsg(ex.Message);
}
}
void ShowMsg(string msg)
{
this.BeginInvoke((MethodInvoker)delegate { rtb_ShowMsg.AppendText(msg + "\r\n"); });
}