C#实时申请技术

导读:
  Real time Application 实时申请技术在本文里是作为一个实例来演示在用户(Tcpclient)申请与服务器(TcpServer)申请之间使用Socket类的情况 。该项目同样也演示在实时项目中如何使用listview控制以及如何传递XML格式信息。
  TcpServer.exe 文件显示了在单独的thread当中(而不是在GUI 线程之中)TCP socket的相互通讯。
  TcpClient.exe文件同样也使用一条单独的线程 从Socket中读取数据,然后对表单中的list view控件进行更新。
  步聚如下:
  1.TcpServer 监听端口8002,并且发射线程等待客户端连结。
  Hashtable socketHolder = new Hashtable();
  Hashtable threadHolder = new Hashtable();
  public Form1()
  {
  // Required for Windows Form Designer support
  //
  InitializeComponent();
  tcpLsn = new TcpListener(8002);
  tcpLsn.Start();
  // tcpLsn.LocalEndpoint may have a bug, it only show 0.0.0.0:8002
  stpanel.Text = "Listen at: " + tcpLsn.LocalEndpoint.ToString();
  Thread tcpThd = new Thread(new ThreadStart(WaitingForClient));
  threadHolder.Add(connectId, tcpThd);
  tcpThd.Start() ;
  }
  2. TcpClient与TcpSrv连接上后,发送客户端信息数据包至TcpServer,然后发射线程,该线程是用来接收通过Socket传来的数据。
  private void menuConn_Click(object sender, System.EventArgs e)
  {
  ConnectDlg myDlg = new ConnectDlg();
  myDlg.ShowDialog(this);
  if( myDlg.DialogResult==DialogResult.OK)
  {
  s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp );
  IPAddress hostadd = IPAddress.Parse(myDlg.IpAdd);
  int port=Int32.Parse(myDlg.PortNum);
  IPEndPoint EPhost = new IPEndPoint(hostadd, port);
  Try
  {
  s.Connect(EPhost);
  if (s.Connected)
  {
  Byte[] bBuf;
  string buf;
  buf = String.Format("{0}:{1}", myDlg.UserName,myDlg.PassWord);
  bBuf=ASCII.GetBytes(buf);
  s.Send(bBuf, 0 , bBuf.Length,0);
  t = new Thread(new ThreadStart(StartRecieve));
  t.Start();
  sbar.Text="Ready to recieve data";
  }
  }
  catch (Exception e1)
  {
  MessageBox.Show(e1.ToString());
  }
  }
  }
  private void StartRecieve()
  {
  miv = new MethodInvoker(this.UpdateListView);
  int cnt=0;
  string tmp=null;
  Byte[] firstb= new Byte[1];
  while (true)
  {
  try
  {
  Byte[] receive = new Byte[1];
  int ret = s.Receive(receive, 1, 0);
  if (ret >0)
  {
  switch(receive[0])
  {
  case 11: //check start message
  cnt=0;
  break;
  case 10: // check end message
  cnt=0;
  if(firstb[0] == ':')
  HandleCommand(tmp);
  else if(firstb[0] == '  HandleXml(tmp);
  else
  HandleText(tmp);
  tmp=null;
  break;
  default:
  if (cnt == 0)
  firstb[0] = receive[0];
  tmp += System.Text.Encoding
  .ASCII.GetString(receive);
  cnt++;
  break;
  }
  }
  }
  catch (Exception e)
  {
  if( !s.Connected )
  {
  break;
  }
  }
  }
  t.Abort();
  }
  3.TcpServer接收来自TcpClient的连接请求,并且将socket 实例保存到Hash表中,然后发射线程以便控制socket的通讯,同时将客户端信息在listview 控件中显示出来。
  public void WaitingForClient()
  {
  while(true)
  {
  // Accept will block until someone connects
  Socket sckt = tcpLsn.AcceptSocket();
  if (connectId   Interlocked.Increment(ref connectId);
  Else
  connectId = 1;
  if (socketHolder.Count <maxconnected>  { <br>  while (socketHolder.Contains(connectId) ) <br>  { <br>  Interlocked.Increment(ref connectId); <br>  } <br>  Thread td = new Thread(new ThreadStart(ReadSocket)); <br>  lock(this) <br>  { <br>  // it is used to keep connected Sockets <br>  socketHolder.Add(connectId, sckt); <br>  // it is used to keep the active thread <br>  threadHolder.Add(connectId, td); <br>  } <br>  td.Start(); <br>  } <br>  } <br>  } <br>  // follow function handle the communication from the clients and close the <br>  // socket and the thread when the socket connection is down <br>  public void ReadSocket() <br>  { <br><br>本文转自 <br><a href="http://study.qqcf.com/web/224/23984.htm">http://study.qqcf.com/web/224/23984.htm</a></maxconnected>

你可能感兴趣的:(thread,C++,c,socket,C#)