.net socket客户端实例代码分享

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了.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(AddressF@R_406_2578@ly.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客户端实例代码分享全部内容,希望文章能够帮你解决.net socket客户端实例代码分享所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: