c# socket

來源:互聯網
上載者:User

標籤:style   blog   http   color   2014   os   

好久沒有寫CS端代碼,今天有空複習一下SOCKET。

功能說明:

1、服務端向用戶端發送資訊

2、用戶端向服務端發送資訊

效果如:

服務端代碼:

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;            }

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.