一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net c#.net Tcp socket 接收数据代码

时间:2010-07-13 编辑:简简单单 来源:一聚教程网

asp教程.net c#.net tcp socket 接收数据代码
 //监听网络
      

  public bool opennet(string sport)
       {
            processor = new thread(new threadstart(startlistening));//新建监听线程
            processor.priority = threadpriority.normal;
            processor.isbackground = true;
            processor.start();//线程开始
            return true;
        }
        private void startlistening()
        {
            //获取端口号
            string sport = m_inifile.inireadvalue("net", "tcpport");
            int iport = int.parse(sport);
            //
            m_blisten = false;
            //
            try
            {
                m_endpoint = new ipendpoint(ipaddress.any, iport);//组合将访问主机的ip地址和端口号。
                _svrsock = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);//实例化socket对象。
                _svrsock.bind(m_endpoint);//绑定将访问的主机。
                _svrsock.listen(50);//开始监听,最大包长50。

                //while循环接收
                while (true)
                {
                    if (m_blisten == false)
                    {
                        accsock = _svrsock.accept();//接收客户端的服务请求套接字。
                    }
                    //
                    idatabuflen = (ushort)accsock.receive(bdatabuf, bdatabuf.length, 0);
                    //
                    if (idatabuflen > 0)
                    {
                        //处理数据
                        //同时m_blisten被置true
                    }
                }
            }
            catch (exception ex)
            {
                messagebox.show("监听异常:" + ex.message);
                return;
            }
        }

 /*
 服务器和客户端采用tcp方式连接。
服务器连续发送几条数据过来,客户端while(true)循环接收
结果只能接收到前面两条数据,后面还有数据接收不到。
服务器端确保没有问题,问题应处在客户端。

 */

热门栏目