日韩国产中文字幕_日韩视频中文免费观看综合一级毛片精品91_亚洲日韩在线图片卡通动漫_欧洲无码一区二区_欧美 中文 国产 高清_亚洲男女啪啪视频_免费人成网站在线观看不_精品无码国产污污污免费网站_波多野结衣绝伦老人_免费一级无码婬片AA片VA黄

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

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

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

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

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

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

  1. 建立Socket對象
  2. Connet(ip, port)向某個IP下的PORT端口發(fā)起連接請求,接受后開始通信
  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();  //開始監(jiān)聽

        }

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

            try
            {
                //這里最好改成IPAddress.Any, 這樣放到服務器上就會同時監(jiān)聽所有網(wǎng)卡上的端口,比如有內外雙網(wǎng)的服務器
                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);

                    //開啟新線程來不停發(fā)送信息
                    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();
        }

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

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

                //獨立線程來發(fā)送數(shù)據(jù)給服務端
                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)  // 監(jiān)聽鍵盤輸入
            {
                Console.ForegroundColor = ConsoleColor.Red;
                //獲取鍵盤輸入
                string input = Console.ReadLine();
                //編碼
                byte[] buffer = Encoding.UTF8.GetBytes(input);
                //發(fā)送
                clientSocket.Send(buffer);
            }
        }
    }
}

 


網(wǎng)站首頁 | 關于我們 | 售后服務 | 網(wǎng)站地圖 | 查看留言 | 在線留言 | 客服中心
© 版權所有:西安潤宇軟件科技有限公司 
公司地址:西安市絲路國際創(chuàng)意夢工廠4號樓 聯(lián)系電話:029-87878512 手機:13468700578 聯(lián)系人:李先生
Copyright ® 2011-2023 Xbwbw.com Inc. All Rights Reserved 
技術支持:西安潤宇軟件科技有限公司  陜ICP備11000720號