91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何實現.net socket客戶端

發布時間:2021-10-08 15:37:35 來源:億速云 閱讀:143 作者:iii 欄目:開發技術

本篇內容介紹了“如何實現.net socket客戶端”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

客戶端代碼

復制代碼 代碼如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data;

using System.Net;

using System.Threading;

using System.Net.Sockets;

namespace W.Common

{

    public class CacheSocket

    {

        public Socket skClient;

        public string ip = string.Empty;

        public int port = -1;

        public int netID;

        // public int timeSleep = 1;

        //每次接收發送的臨時信息

        private byte[] sendData;//發送的信息

        private byte[] receiveData = new byte[1024];//接收信息

        private int receiveN;

        private bool isErr = false;

        //--------

        public CacheSocket(int pNetID)

        {

            this.netID = pNetID;

            GetConfig();

            Connection();

            Cmd("netid:" + this.netID);

        }

        public CacheSocket(int pNetID, string pIP, int pPort)

        {

            this.ip = pIP;

            this.port = pPort;

            Connection();

            Cmd("netid:" + pNetID);

        }

        public string Cmd(string key)

        {

            lock (this)//一個信息發送后再接收為一次完成過程

            {

                this.sendData = Encoding.UTF8.GetBytes(key);

                try

                {

                    this.skClient.Send(this.sendData);

                }

                catch (Exception ex)

                {

                    isErr = true;

                    ("Send" + ex.Message).WriteLine();

                    ReSocket(() => { this.skClient.Send(this.sendData); });

                }

                try

                {

                    this.receiveN = this.skClient.Receive(this.receiveData);

                }

                catch (Exception ex)

                {

                    isErr = true;

                    ReSocket(() => { this.receiveN = this.skClient.Receive(this.receiveData); });

                    ("Receive" + ex.Message).WriteLine();

                }

                return Encoding.UTF8.GetString(this.receiveData, 0, this.receiveN);

            }

        }

        public delegate void ReSocket_D();

        private void ReSocket(ReSocket_D d)

        {

            if (isErr)

            {

                Connection();

                this.sendData = Encoding.UTF8.GetBytes("netid:" + this.netID);

                this.skClient.Send(this.sendData);

                this.receiveN = this.skClient.Receive(this.receiveData);

                if (Encoding.UTF8.GetString(this.receiveData, 0, this.receiveN) != "1")

                {

                }

                d();

                this.isErr = false;

            }

        }

        #region 獲取IP和端口

        private void GetConfig()

        {

            this.ip = "127.0.0.1";   

            this.port = 1234;

        }

        #endregion

        #region 連接套接字

        private void Connection()

        {

            this.skClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPEndPoint ie = new IPEndPoint(IPAddress.Parse(this.ip), this.port);//服務器的IP和端口

            skClient.Connect(ie);

            byte[] data = new byte[7];

            this.receiveN = this.skClient.Receive(data);

            string s = Encoding.UTF8.GetString(data, 0, this.receiveN);

            if (s != "success")

            {

                throw new Exception("連接不成功" + s);

            }

        }

        #endregion

    }

}

使用方法

復制代碼 代碼如下:

 public static readonly CacheSocket cac=new CacheSocket(2);

 cac.Cmd("發送內容");

“如何實現.net socket客戶端”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

梅州市| 科技| 武夷山市| 金秀| 勐海县| 渭源县| 布拖县| 长沙市| 甘南县| 鸡东县| 安岳县| 招远市| 西乌| 东源县| 花莲市| 长垣县| 道真| 黑山县| 年辖:市辖区| 桐乡市| 定安县| 桃园市| 论坛| 深州市| 隆尧县| 海盐县| 阜平县| 伽师县| 金川县| 湖口县| 清流县| 朝阳县| 搜索| 临沂市| 浦城县| 东辽县| 潼关县| 胶南市| 宁晋县| 洞口县| 合水县|