控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

.net Socket客户端

作者:佚名 出处:互联网 2011年08月10日 阅读:

.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(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
    }
}

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统