通信(Socket)

来源:互联网 发布:阿里妈妈淘宝客 编辑:程序博客网 时间:2024/05/22 11:52

这里写图片描述

这里写图片描述

代码部分:
Client:

        Socket sClient;        private void txtConnect_Click(object sender, EventArgs e)        {            sClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            sClient.Connect(txtIp.Text, int.Parse(txtPort.Text));            txtMsg.Text += "连接成功\r\n";            Thread t = new Thread(() =>            {                byte[] bs = new byte[1024];                while (true)                {                    int len = sClient.Receive(bs);                    txtMsg.Invoke(new Action<string>((msg) =>                    {                        txtMsg.Text += msg + "\r\n";                    }), Encoding.UTF8.GetString(bs, 0, len));                }            });            t.IsBackground = true;            t.Start();        }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)        {            sClient.Send(new byte[] {                (byte)2            });            sClient.Shutdown(SocketShutdown.Both);            sClient.Dispose();            sClient.Close();        }        private void btnSend_Click(object sender, EventArgs e)        {            //byte[] bs = new[]            //{            //    (byte)0            //};            byte[] bs = new byte[1024];            bs[0] = 0;            byte[] bs2 = Encoding.UTF8.GetBytes(txtSend.Text);            Array.Copy(bs2, 0, bs, 1, bs2.Length);            sClient.Send(bs);        }        private void btnSP_Click(object sender, EventArgs e)        {            byte[] bs = new[]            {                (byte)1            };            sClient.Send(bs);        }

Server:

        Dictionary<string, Socket> dicClient = new Dictionary<string, Socket>();        private void btnStart_Click(object sender, EventArgs e)        {            Socket sServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            sServer.Bind(new IPEndPoint(IPAddress.Parse(txtIp.Text), int.Parse(txtPort.Text)));            sServer.Listen(10);            txtMsg.Text += "启动成功\r\n";            //启动新线程,用于接收客户端请求。            Thread tAccept = new Thread(() =>            {                int index = 0;                while (true)                {                    Socket sClient = sServer.Accept();                    index++;                    string key = "用户" + index;                    dicClient.Add(key, sClient);                    lbClient.Invoke(new Action<string>((clientName) =>                    {                        lbClient.Items.Add(clientName);                    }), key);                    Thread tReceive = new Thread(() =>                    {                        byte[] bs = new byte[1024];                        while (true)                        {                            int len = sClient.Receive(bs);                            if (bs[0] == 1)                            {                                //闪屏                                Point old = this.Location;                                Random r = new Random();                                for (int i = 0; i < 10; i++)                                {                                    this.Invoke(new Action(() =>                                    {                                        this.Location = new Point(old.X + r.Next(-10, 10), old.Y + r.Next(-10, 10));                                    }));                                    Thread.Sleep(100);                                    this.Invoke(new Action(() =>                                    {                                        this.Location = new Point(old.X + r.Next(-10, 10), old.Y + r.Next(-10, 10));                                    }));                                    Thread.Sleep(10);                                }                            }                            else if (bs[0] == 0)                            {                                //文本                                txtMsg.Invoke(new Action<string>((msg) =>                                {                                    txtMsg.Text += "\r\n"+msg;                                }), sClient.RemoteEndPoint + ":" + Encoding.UTF8.GetString(bs, 1, len - 1));                            }                            else if (bs[0] == 2)                            {                                break;                            }                        }                    });                    tReceive.IsBackground = true;                    tReceive.Start();                }            });            tAccept.IsBackground = true;            tAccept.Start();        }        private void btnSend_Click(object sender, EventArgs e)        {            Socket client = dicClient[lbClient.SelectedItem.ToString()];            client.Send(Encoding.UTF8.GetBytes(txtSend.Text));        }
0 0
原创粉丝点击