真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

如何實(shí)現(xiàn).netsocket客戶端-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“如何實(shí)現(xiàn).net socket客戶端”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)建站2013年至今,先為安化等服務(wù)建站,安化等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為安化企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

客戶端代碼

復(fù)制代碼 代碼如下:

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;

        //每次接收發(fā)送的臨時信息

        private byte[] sendData;//發(fā)送的信息

        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)//一個信息發(fā)送后再接收為一次完成過程

            {

                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);//服務(wù)器的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

    }

}


使用方法



復(fù)制代碼 代碼如下:

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

 cac.Cmd("發(fā)送內(nèi)容");


“如何實(shí)現(xiàn).net socket客戶端”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!


網(wǎng)站標(biāo)題:如何實(shí)現(xiàn).netsocket客戶端-創(chuàng)新互聯(lián)
分享地址:http://weahome.cn/article/hippd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部