码迷,mamicode.com
首页 > 其他好文 > 详细

c# socket

时间:2014-07-01 23:36:57      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   2014   os   

好久没有写CS端代码,今天有空复习一下SOCKET。

功能说明:

1、服务端向客户端发送信息

2、客户端向服务端发送信息

效果如下图:

bubuko.com,布布扣

服务端代码:

Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint localPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000);
            serverSocket.Bind(localPoint);
            serverSocket.Listen(10);
            new Thread(s =>
            {
                while (true)
                {
                    Socket clientSocket = ((Socket)s).Accept();
                    clientSocket.Send(Encoding.ASCII.GetBytes("Server Say Hello"));
                    clients.Add(clientSocket);
                    new Thread((cS) =>
                    {
                        Socket myClientSocket = (Socket)clientSocket;
                        while (true)
                        {
                            try
                            {
                                byte[] result = new byte[1024 * 1024];
                                int receiveNumber = myClientSocket.Receive(result);
                                string realMsg = string.Format("接收客户端{0}消息{1}\r\n", myClientSocket.RemoteEndPoint.ToString(), Encoding.Default.GetString(result, 0, receiveNumber));
                                if (label1.InvokeRequired)
                                {
                                    label1.BeginInvoke(new Action<string>((msg) =>
                                    {
                                        label1.Text += msg;
                                    }), realMsg);
                                }
                            }
                            catch (Exception ex)
                            {
                                if (myClientSocket != null && myClientSocket.Connected)
                                {
                                    myClientSocket.Shutdown(SocketShutdown.Both);
                                    myClientSocket.Close();
                                }
                            }
                        }
                    }) { IsBackground = true }.Start(clientSocket);
                }
            }) { IsBackground = true }.Start(serverSocket);

客户端代码:

            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect("127.0.0.1", 5000);
                new Thread((s) =>
                {
                    Socket cS = (Socket)s;
                    while (true)
                    {
                        byte[] byteMsg = new byte[1024 * 1024];
                        int realResult = cS.Receive(byteMsg);
                        string msg = Encoding.Default.GetString(byteMsg, 0, realResult);
                        if (label1.InvokeRequired)
                        {
                            Action<string> a = (rm) =>
                            {
                                this.label1.Text += rm + "\r\n";
                            };
                            label1.BeginInvoke(a, msg);
                        }
                    }
                }) { IsBackground = true }.Start(socket);
                button1.Enabled = false;
            }
            catch (Exception ex)
            {
                button1.Enabled = true;
            }

 

 

c# socket,布布扣,bubuko.com

c# socket

标签:style   blog   http   color   2014   os   

原文地址:http://www.cnblogs.com/tom-zhu/p/3816461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!