濮阳杆衣贸易有限公司

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

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

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

客戶端代碼

復(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客戶端(遠(yuǎn)程發(fā)送和接收數(shù)據(jù))
  • Visual C#.Net 網(wǎng)絡(luò)程序開發(fā)-Socket篇
  • .net的socket異步通訊示例分享

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

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《.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)民參考!
  • 推薦文章
    贵德县| 青海省| 绥江县| 青冈县| 涟水县| 扎兰屯市| 旌德县| 张家口市| 本溪| 犍为县| 泊头市| 汉寿县| 盘山县| 宜宾市| 祁门县| 建始县| 镇原县| 桐梓县| 库尔勒市| 苏尼特右旗| 沐川县| 华亭县| 巫溪县| 嘉义县| 和田县| 湾仔区| 闽侯县| 黄山市| 乐亭县| 治多县| 平潭县| 盐山县| 乐至县| 集安市| 荔波县| 太康县| 浪卡子县| 敖汉旗| 漳平市| 休宁县| 洪泽县|