欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-28 21:57:34
客户端代码 复制代码 代码如下:using system; using system.collections.generic; using system.linq;...

客户端代码

复制代码 代码如下:

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(addressfamily.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("发送内容");