<address id="fzhbr"><delect id="fzhbr"></delect></address>
        <track id="fzhbr"><delect id="fzhbr"></delect></track>
        <strike id="fzhbr"></strike>

              <track id="fzhbr"></track>
                 
                設為首頁 加入收藏
                首 頁 企業簡介 項目案例 軟件定制 行業軟件 解決方案 企業資訊 服務專區 客服中心
                服務項目
                案例展示 更多 >>
                ·生產流程監控 2020/11/26
                ·上位機軟件定制開發 2020/9/29
                ·手機端公眾號集成開發 2020/6/5
                ·業務技能培訓學習軟件 2020/6/5
                ·潤宇司法行政管理綜合平… 2010/12/6
                ·連鎖店管理系統軟件 2010/7/31
                ·醫院管理系統軟件 2010/7/27
                ·駕校查詢系統軟件 2010/7/15
                ·生產管理系統軟件 2010/1/30
                ·人事管理系統軟件 2010/1/30
                聯系人:李先生
                電  話:029-87878512
                手  機:13468700578
                地  址:西安市絲路國際創意夢工廠4號樓
                在線咨詢:  762176615
                Email:junsoft@126.com
                 
                當前的位置 >> 返回首頁 >> 解決方案
                軟件定制開發中Socket通信
                發布者:西安軟件公司   發布時間:2023/4/1   閱讀:次

                可以看到,總的工作流程就是:

                1. 服務端監聽
                2. 客戶端(可以有多個)請求連接
                3. 確認連接進行通信

                針對服務器端的工作流程是:

                1. 建立Socket對象
                2. Bind(ip, port)綁定端口。比如說本地的(127.0.0.1, 6666)
                3. Listen()開始監聽
                4. Accept()接受客戶端連接
                5. Send()和Receive()進行通信
                6. Close()關閉套接字

                而針對客戶端的工作流程是:

                1. 建立Socket對象
                2. Connet(ip, port)向某個IP下的PORT端口發起連接請求,接受后開始通信
                3. Send()和Receive()進行通信
                4. Close() 關閉套接字

                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using System.Threading.Tasks;
                using System.Net.Sockets;
                using System.Net;
                using System.Threading;

                namespace socket
                {
                    class Program
                    {

                        static Socket serverSocket;
                      
                       

                        static void Main(string[] args)
                        {
                            SetListen();  //開始監聽

                        }

                        static void SetListen()
                        {
                            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                            try
                            {
                                //這里最好改成IPAddress.Any, 這樣放到服務器上就會同時監聽所有網卡上的端口,比如有內外雙網的服務器
                                serverSocket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888));
                                serverSocket.Listen(2);

                                Thread thread = new Thread(Listen);
                                thread.Start(serverSocket);

                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                           
                        }

                        static void Listen(object so)
                        {
                            Socket serverSocket = so as Socket;

                            while (true)
                            {
                                try
                                {
                                    Socket clientSocket = serverSocket.Accept(); //接受客戶端接入
                                    // 獲取鏈接IP地址
                                    string clientPoint = clientSocket.RemoteEndPoint.ToString();

                                    //開啟新線程來不停接受信息
                                    Thread rec = new Thread(Receive);
                                    rec.Start(clientSocket);

                                    //開啟新線程來不停發送信息
                                    Thread sen = new Thread(Send);
                                    sen.Start(clientSocket);
                                }
                                catch(Exception ex)
                                {
                                    Console.WriteLine(ex.Message, ex);
                                    break;
                                }
                            }
                            serverSocket.Close();
                        }

                        static void Receive(object so)
                        {
                            Socket clientSocket = so as Socket;

                            string clientPoint = clientSocket.RemoteEndPoint.ToString();
                            while (true)
                            {
                                try
                                {
                                    byte[] buffer = new byte[1024];
                                    int len = clientSocket.Receive(buffer);
                                    //Console.WriteLine(len.ToString(), buffer);
                                    if (len == 0) break;

                                    string msg = Encoding.UTF8.GetString(buffer, 0, len);
                                    Console.WriteLine("客戶端說:"+ msg);
                                }
                                catch(Exception ex)
                                {
                                    Console.WriteLine(ex);
                                }
                            }
                        }

                        static void Send(object so)
                        {
                            Socket clientSocket = so as Socket;

                            while (true)
                            {
                                //獲取控制臺輸入
                                string input = Console.ReadLine();

                                byte[] msg = Encoding.UTF8.GetBytes(input);
                                clientSocket.Send(msg);
                            }
                        }
                    }
                }

                 

                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using System.Threading.Tasks;
                using System.Net;
                using System.Net.Sockets;
                using System.Threading;

                 

                namespace socket_client
                {
                    class Program
                    {
                        static Socket clientSocket;

                        static void Main(string[] args)
                        {
                           
                            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                           
                            //連接
                            Connect();
                        }

                        //連接客戶端,連接后開始監聽發送過來的消息并且把輸入的信息發給服務端
                        static void Connect()
                        {
                            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                           
                            try
                            {
                                //連接
                                clientSocket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888));

                                //獨立線程來接受來自服務端的數據
                                Thread receive = new Thread(Receive);
                                receive.Start(clientSocket);

                                //獨立線程來發送數據給服務端
                                Thread send = new Thread(Send);
                                send.Start(clientSocket);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                        }

                        static void Receive(object so)
                        {
                            Socket clientSocket = so as Socket;


                            while (true)
                            {
                                Console.ForegroundColor = ConsoleColor.Green;  // 修改文字顏色為綠色
                                try
                                {
                                    byte[] buffer = new byte[1024];
                                    int len = clientSocket.Receive(buffer);
                                    if (len > 0)
                                    {
                                        string msg = Encoding.UTF8.GetString(buffer);
                                       
                                        Console.WriteLine("服務端說:"+ msg);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex);
                                    break;
                                }
                            }
                            clientSocket.Close();
                        }

                        static void Send(object so)
                        {
                            Socket clientSocket = so as Socket;

                            while (true)  // 監聽鍵盤輸入
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                //獲取鍵盤輸入
                                string input = Console.ReadLine();
                                //編碼
                                byte[] buffer = Encoding.UTF8.GetBytes(input);
                                //發送
                                clientSocket.Send(buffer);
                            }
                        }
                    }
                }

                 


                網站首頁 | 關于我們 | 售后服務 | 網站地圖 | 查看留言 | 在線留言 | 客服中心
                © 版權所有:西安潤宇軟件科技有限公司 
                公司地址:西安市絲路國際創意夢工廠4號樓 聯系電話:029-87878512 手機:13468700578 聯系人:李先生
                Copyright ® 2011-2023 Xbwbw.com Inc. All Rights Reserved 
                技術支持:西安潤宇軟件科技有限公司  陜ICP備11000720號
                日韩Av美女精品福利,自拍 制服 无码 日韩,超碰亚洲AV日韩AV无码,日韩亚州另类无码