程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#實時申請技術(2)

C#實時申請技術(2)

編輯:關於C語言

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();
 }
4. 點擊Load Data菜單,從文件中載入信息,然後把所有信息傳送到每個將與TcpServer相連接的客戶端,客戶端會自己更新它的listview。不管是TcpServer 還是 TcpClient ,它們都從運作中的線程之中獲取數據,再在主線程中更新ListvIEw control。下面則講述的是通過MethodInvoker實現該功能。

public void LoadThread()
{
MethodInvoker mi = new MethodInvoker(this.UpdateListVIEw);
string tmp = null;
StreamReader sr = File.OpenText("Issue.txt");
while((tmp = sr.ReadLine()) !=null )
{
if (tmp =="")
break;
isu.symbol= Mid(tmp, 0, 4);
isu.bid = Mid(tmp, 4, 5);
isu.offer = Mid(tmp, 9, 5);
isu.volume = Mid(tmp, 16, tmp.Length-16);
sendMsg ="\v" + tmp + "\n"; //add send message's head and end char
SendDataToAllClIEnt(tmp);
this.BeginInvoke(mi);
JobDone.WaitOne();
}
sr.Close();
fThd.Abort();
}
private void SendDataToAllClIEnt(string str)
{
foreach (Socket s in socketHolder.Values)
{
if(s.Connected)
{
Byte[] byteDateLine=ASCII.GetBytes(str.ToCharArray());
s.Send(byteDateLine, byteDateLine.Length, 0);
}
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved