用C#的SOCKET编写一个连接服务器并且能够发送请求

2025-03-10 16:52:09
推荐回答(1个)
回答1:

http://zhidao.baidu.com/question/1303275813342075619

IPEndPoint ip = new IPEndPoint(IPAddress.Parse(xml("IP")), int.Parse(xml("Port")));
            Socket ser = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                ser.Connect(ip);
            }
            catch { MessageBox.Show("请打开服务器!"); return; }
            byte[] bb = new byte[1024];
            bb = Encoding.BigEndianUnicode.GetBytes("user@"+textBox1.Text+"@"+textBox2.Text);
            ser.Send(bb, bb.Length, 0);
            client c = new client();
            c.User = textBox1.Text;
            c.Sock = ser;
            dioge_Form m = new dioge_Form(c,this);
            m.Show();
            this.Hide();