自已C#寫的一個通訊網路服務端的類

來源:互聯網
上載者:User

此類還有不足的地方.請大家多意見便於改正!!      做好的類庫在些下載:  CSockets.rar 
SocketStart.cs 的代碼:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;

namespace CSockets
{
    public delegate void getClientID(object sender, ClientID e);
    public class SocketStart
    {       

        public event getClientID mOnConnected = null;
        public ArrayList ClinetList = ArrayList.Synchronized(new ArrayList());
        public AsyncCallback ClientCallBack;
        public Socket MaiSocket;
        public int ClientNum = 0;
        public void Start()   //啟動服務
        {
            try
            {
                MaiSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint LocIP = new IPEndPoint(IPAddress.Any, 1680);
                MaiSocket.Bind(LocIP);
                MaiSocket.Listen(100);
                MaiSocket.BeginAccept(new AsyncCallback(ClinetConnection), null);
            }
            catch (Exception E)
            {
            }
        }

        private void ClinetConnection(IAsyncResult asyn)  //實現多客戶串連
        {
            try
            {
                Socket ClientSocket = MaiSocket.EndAccept(asyn);
                Interlocked.Increment(ref ClientNum);
                ClinetList.Add(ClientSocket);
                mOnConnected(this,new ClientID(this));
                WaiData(ClientSocket, ClientNum);
                MaiSocket.BeginAccept(new AsyncCallback(ClinetConnection), null);
            }
            catch (Exception E)
            {
            }
        }

        public class SocketPack
        {
            public Socket cSocketName;
            public byte[] dataBuffer = new byte[64];
            public int CID;
            public SocketPack(Socket socket, int ID)
            {
                cSocketName = socket;
                CID = ID;
            }
        }

        private void WaiData(Socket socketData, int Num)   //等待資料
        {
            try
            {
                if (ClientCallBack == null)
                {
                    ClientCallBack = new AsyncCallback(onData);
                }
                SocketPack Sp = new SocketPack(socketData, Num);
                socketData.BeginReceive(Sp.dataBuffer, 0, Sp.dataBuffer.Length, SocketFlags.None, ClientCallBack, Sp);
            }
            catch (Exception E)
            {

            }
        }

        private void onData(IAsyncResult asny)    //接受資料
        {
            SocketPack Sp = (SocketPack)asny.AsyncState;
            try
            {               
                int iRx = Sp.cSocketName.EndReceive(asny);
                char[] chars = new char[iRx + 1];
                string IDData = new string(chars);
                string[] str = IDData.Split(new char[] { '|' });
                if (str[0] == "Mes")
                {

                }
                WaiData(Sp.cSocketName, Sp.CID);
            }
            catch (ObjectDisposedException SE)
            {
            }
            catch (SocketException E)
            {
                if (E.ErrorCode == 10054)
                {
                    ClinetList[Sp.CID - 1] = null;
                    mOnConnected(this, new ClientID(this));
                }
            }
        }

        public void SendMess(string SendText,int SendID)    //發送資料
        {
            try
            {
                byte[] sendData = System.Text.Encoding.UTF8.GetBytes(SendText);
                Socket SendSocket = (Socket)ClinetList[SendID];
                SendSocket.Send(sendData);
            }
            catch (Exception E)
            {
            }
        }
    }
}

第二個ClientID.cs代碼:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Collections;

namespace CSockets
{
    public class ClientID
    {
        public ClientID(SocketStart ss)
            {
             
            }

    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.