婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > .net socket客戶端實例代碼分享

.net socket客戶端實例代碼分享

熱門標簽:怎么給超市做地圖標注入駐店 世紀佳緣地圖標注怎么去掉 外呼系統(tǒng)代理品牌 十堰正規(guī)電銷機器人系統(tǒng) 寧波自動外呼系統(tǒng)代理 手機地圖標注如何刪除 外呼系統(tǒng)費用一年 辦理400電話證件 巫師3為什么地圖標注的財寶沒有

客戶端代碼

復(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)容");
 

您可能感興趣的文章:
  • java.net.SocketException: Connection reset 解決方法
  • 基于Socket的網(wǎng)絡(luò)連接 Flex與.NET互操作(一)
  • Asp.net Socket客戶端(遠程發(fā)送和接收數(shù)據(jù))
  • Visual C#.Net 網(wǎng)絡(luò)程序開發(fā)-Socket篇
  • .net的socket異步通訊示例分享

標簽:天門 景德鎮(zhèn) 牡丹江 山西 通遼 泰州 嘉興

巨人網(wǎng)絡(luò)通訊聲明:本文標題《.net socket客戶端實例代碼分享》,本文關(guān)鍵詞  .net,socket,客戶端,實例,代碼,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《.net socket客戶端實例代碼分享》相關(guān)的同類信息!
  • 本頁收集關(guān)于.net socket客戶端實例代碼分享的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 海南省| 龙游县| 应城市| 社会| 宁化县| 子洲县| 卫辉市| 莱西市| 建阳市| 黄冈市| 淳安县| 策勒县| 印江| 横山县| 马尔康县| 鹤庆县| 屏南县| 汾西县| 湘西| 洛宁县| 琼结县| 雅安市| 云龙县| 张家界市| 通榆县| 淮南市| 卢湾区| 昭通市| 泸定县| 东城区| 巩留县| 定边县| 布尔津县| 辽宁省| 长宁区| 易门县| 漳州市| 泽库县| 福鼎市| 怀仁县| 苗栗县|