佳木斯湛栽影视文化发展公司

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

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

熱門標簽:電銷業(yè)務(wù) 人工智能 電商新玩法 客戶服務(wù) 科大訊飛語音識別系統(tǒng) 百度AI接口 網(wǎng)站排名優(yōu)化 國美全國運營中心

客戶端代碼

復(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異步通訊示例分享

標簽:拉薩 廈門 POS機 咸寧 棗莊 攀枝花 南平 益陽

巨人網(wǎng)絡(luò)通訊聲明:本文標題《.net socket客戶端實例代碼分享》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    夏津县| 天等县| 沁阳市| 阜阳市| 定远县| 通许县| 台州市| 长兴县| 黄骅市| 柳州市| 阜宁县| 西安市| 武陟县| 香河县| 尼玛县| 塘沽区| 兰州市| 慈溪市| 修武县| 江北区| 修文县| 德格县| 临猗县| 湖口县| 永州市| 政和县| 顺昌县| 肥东县| 交城县| 河曲县| 玛多县| 墨竹工卡县| 丁青县| 瑞昌市| 沙田区| 西安市| 利津县| 鄂托克旗| 宜阳县| 拉萨市| 黎城县|