C#TCP连接握手代码

来源:互联网 发布:孟庭苇离婚知乎 编辑:程序博客网 时间:2024/06/07 06:50

之前做了一个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"); });
        }

0 0