科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件C#实时申请技术

C#实时申请技术

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文演示在用户申请与服务器申请之间使用Socket类的情况。

作者:crystal编译 来源:yesky 2007年11月14日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
3.TcpServer接收来自TcpClient的连接请求,并且将socket 实例保存到Hash表中,然后发射线程以便控制socket的通讯,同时将客户端信息在listview 控件中显示出来。

public void WaitingForClient()

{

 while(true)

 {

  // Accept will block until someone connects

  Socket sckt = tcpLsn.AcceptSocket();

  if (connectId < 10000)

   Interlocked.Increment(ref connectId);

  Else

   connectId = 1;

   if (socketHolder.Count < MaxConnected )
 
   {

    while (socketHolder.Contains(connectId) )

    {

     Interlocked.Increment(ref connectId);

    }

   Thread td = new Thread(new ThreadStart(ReadSocket));

   lock(this)

   {

    // it is used to keep connected Sockets

    socketHolder.Add(connectId, sckt);

    // it is used to keep the active thread

    threadHolder.Add(connectId, td);

   }

   td.Start();

  }

 }

}

// follow function handle the communication from the clients and close the

// socket and the thread when the socket connection is down

public void ReadSocket()

{

 // the connectId is keeping changed with new connection added. it can't

 // be used to keep the real connectId, the local variable realId will

 // keep the value when the thread started.

 long realId = connectId;

 int ind=-1;

 Socket s = (Socket)socketHolder[realId];

 while (true)

 {

  if(s.Connected)

  {

   Byte[] receive = new Byte[37] ;

   Try

   {

    // Receive will block until data coming

    // ret is 0 or Exception happen when Socket connection

    // is broken

    int ret=s.Receive(receive,receive.Length,0);

    if (ret > 0)

    {

     string tmp = null;

     tmp=System.Text.Encoding.ASCII.GetString(receive);

     if(tmp.Length > 0)

     {

      DateTime now1=DateTime.Now;

      String strDate;

      strDate = now1.ToShortDateString() + " " + now1.ToLongTimeString();

      ListViewItem newItem = new ListViewItem();

      string[] strArry=tmp.Split(':');

      int code = checkUserInfo(strArry[0]);

      if(code==2)

      {

       userHolder.Add(realId, strArry[0]);

       newItem.SubItems.Add(strArry[0]);

       newItem.ImageIndex = 0;

       newItem.SubItems.Add(strDate);

       this.listView2.Items.Add(newItem);

       ind=this.listView2.Items.IndexOf(newItem);

      }

      else if( code==1)

     }

   }

   else

   {

    this.listView2.Items[ind].ImageIndex=1;

    keepUser=false;

    break;

   }

  }

  catch (Exception e)

  {

   if( !s.Connected )

   {

    this.listView2.Items[ind].ImageIndex=1;

    keepUser=false;

    break;

   }

  }

 }

 }
 
 CloseTheThread(realId);

}

private void CloseTheThread(long realId)

{

 socketHolder.Remove(realId);

 if(!keepUser) userHolder.Remove(realId);

  lock(this)

  {

   Thread thd = (Thread)threadHolder[realId];

   threadHolder.Remove(realId);

  }

  thd.Abort();

 }
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章