回 帖 发 新 帖 刷新版面

主题:c#编程2000例下载

[url=http://www.xduzi.cn/code/csharp2000.rar]http://www.xduzi.cn/code/csharp2000.rar[/url]  的确很不错的     我一直在看呢     估计不只1000个例子,几乎涵盖了C#所有方面的 不过是E文的[url=http://www.xduzi.cn/code/csharp2000.rar]http://www.xduzi.cn/code/csharp2000.rar[/url]

回复列表 (共94个回复)

51 楼

谢谢了!!!

52 楼

谢谢版主啦 o(∩_∩)o...哈哈

53 楼

老大,欠费了啊,没法下了。。。

54 楼

private TcpSvr tcpSvr;
        //ThreadRecDataDelegate = 
        //byte[] ReciveBt;
        //HexCoder hcd = new HexCoder();
        //Coder coder = new Coder();


        /// <summary>
        /// 回调函数:新连接到来
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ClientConn(object sender, NetEventArgs e)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(this.ReceiveData));
            //thread.Start(e.Client.ID);
            thread.Start(e);
        }

        /// <summary>
        /// 回调函数:服务器满
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ServerFull(object sender, NetEventArgs e)
        {
            tcpSvr.Send(e.Client, "server is full");
            e.Client.Close();   //服务器满了,必须关闭新来的客户端连接
        }

        /// <summary>
        /// 回调函数:客户端退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ClientClose(object sender, NetEventArgs e)
        {
            //string info;
            //if (e.Client.TypeOfExit == Session.ExitType.ExceptionExit)
            //    info = string.Format("客户端({0})异常退出", e.Client.ID);
            //else
            //    info = string.Format("客户端({0})正常退出", e.Client.ID);
            //info = info + "连接总数 " + m_server.SessionCount;
            //StateTS.Text = info;
        }

55 楼

/// <summary>
        /// 回调函数:接收到新数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //void RecvData(object sender, NetEventArgs e)
        //{
        //    //string info = e.Client.Datagram;
        //    ////ReciveBt = hcd.GetEncodingBytes(info);
        //    //ReciveBt = coder.GetEncodingBytes(info);
        //    //Session session = (Session)e.Client;
            
        //    //ReciveBt.
        //    //Listen(session);
        //}

        private void button1_Click(object sender, EventArgs e)
        {
            tcpSvr = new TcpSvr(5051, 10, new Coder(Coder.EncodingMethod.Default));
            //TcpSvr svr = new TcpSvr(9050,4);//默认使用Encoding.Default编码方式

            //svr.Resovlver = new DatagramResolver("##")
            //定义服务器的4个事件
            tcpSvr.ServerFull += new NetEvent(this.ServerFull);//服务器满
            tcpSvr.ClientConn += new NetEvent(this.ClientConn);//新客户端连接
            tcpSvr.ClientClose += new NetEvent(this.ClientClose);//客户端关闭
            //tcpSvr.RecvData += new NetEvent(this.RecvData);   //接收到数据

            try
            {
                tcpSvr.Start();
                richTextBox.Text = "服务器建立成功";
                //StateTS.Text = "服务器建立成功 端口号" + ComTxt.Text;
            }
            catch (Exception ex)
            {
                //AddToMemo("服务器建立失败\r\n 异常:" + ex.Message);
                MessageBox.Show(ex.Message);
            }
        }

56 楼

public void ReceiveData(object obj)
        { 
            NetEventArgs e = (NetEventArgs)obj;
            string info = string.Format("A Client:{0} connect server Session:{1}. Socket Handle:{2}",
              e.Client.ClientSocket.RemoteEndPoint.ToString(),
              e.Client.ID, e.Client.ClientSocket.Handle);
            //richTextBox.Text = info;
            UInt32 iMainState = 0;
            UInt32 iSubState = 0;
            UInt32 iinfoBytes = 0;
            UInt32 rcvLen;
            UInt32 iinfoLength = 0;
            //int Flat = 1;

            byte[] rcvByte = new byte[1];
            //byte pkgType;               //存放收到的消息类型
            byte[] pkgLen = new byte[4];//存放收到的长度消息
            byte[] pkgBody = new byte[128];//存放收到的消息体
            byte[] recData = new byte[128];
            //MessageBox.Show("f");
            while (true)
            {
                //MessageBox.Show("f");
                if (iMainState < 3)
                    rcvLen = (UInt32)e.Client.ClientSocket.Receive(rcvByte, 1, 0);
                else
                    rcvLen = (UInt32)e.Client.ClientSocket.Receive(pkgBody, (Int32)(iinfoLength - iSubState), 0);
                    //rcvLen= recv(sock, (pkgBody + iifoLength + iSubState), (pkgLen - iSubState), 0);
                //pkgBody.

57 楼

if (rcvLen > 0)
                {   
                    switch(iMainState)
                    {
                    case 0:     //报文标志信息
                        if (rcvByte[0] == 0xAA)
                        {
                            //MessageBox.Show("f");
                            iMainState++;
                        }
                        else
                        {
                              iMainState = 0;
                        }
                        break;

                    case 1:     //报文标志信息
                        if (rcvByte[0] == 0x55)
                        {
                            iMainState++;
                        }
                        else
                        {
                              iMainState = 0;
                        }
                        break;

                    case 2:     //长度信息
                        pkgLen[iinfoBytes]= rcvByte[0];
                        recData[iinfoBytes++] = rcvByte[0];
                        if (iinfoBytes == 4)
                        {//pkgLen.
                            Int32 temp = BitConverter.ToInt32(pkgLen, 0);
                            iinfoLength = (UInt32)IPAddress.NetworkToHostOrder(temp);
                            iMainState++;
                            iinfoBytes = 0;
                        }
                        break;

                    case 3:     //主体信息
                        iSubState += rcvLen;
                        for (int i = 0; i < rcvLen; i++)
                        {
                            recData[4 + iSubState + i-rcvLen] = pkgBody[i];
                        }
                        if (iSubState == iinfoLength)
                        {
                            e.Client.ClientSocket.Send(recData, (Int32)iinfoLength+4, 0);
                            iSubState = 0;
                            iMainState = 0;
                        }
                        break;

                    default:
                        break;
                    }
                }
                else//rcvLen <= 0:错误处理
                {}
                
            }
        }

58 楼

不能下哦!

59 楼

下来看看,呵呵,谢谢了

60 楼

感謝樓主了

我来回复

您尚未登录,请登录后再回复。点此登录或注册