2014-09-05 46 views
-2

我有一個程序,我需要從多個客戶端偵聽,然後將數據從網絡插入到數據庫中的特定時間間隔後發送數據。我寫了一個多線程的程序,但是在線程中,一個線程隨機地運行到無限循環,而沒有實際讀取客戶端的數據。 這裏是我的代碼:c#線程繼續與套接字運行

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      TcpListener serverSocket = new TcpListener(IPAddress.Parse("my.ip.add.ress"),myport); 
      TcpClient clientSocket = default(TcpClient); 
      int counter = 0; 

      serverSocket.Start(); 
      Console.WriteLine(" >> " + "Server Started"); 

      counter = 0; 
      while (true) 
      { 
       counter += 1; 
       clientSocket = serverSocket.AcceptTcpClient(); 
       Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!"); 
       handleClinet client = new handleClinet(); 
       client.startClient(clientSocket, Convert.ToString(counter)); 
      } 
      clientSocket.Close(); 
      serverSocket.Stop(); 
      Console.WriteLine(" >> " + "exit"); 
      Console.ReadLine(); 
     } 
    } 

    //Class to handle each client request separatly 
    public class handleClinet 
    { 
     TcpClient clientSocket; 
     string clNo; 
     public void startClient(TcpClient inClientSocket, string clineNo) 
     { 
      Console.WriteLine("In start client"); 
      this.clientSocket = inClientSocket; 
      this.clNo = clineNo; 
      Thread ctThread = new Thread(doChat); 
      ctThread.Start(); 
     } 
     private void doChat() 
     { 
      Console.WriteLine("In doChat"); 
      byte[] bytesFrom = new byte[251]; 
      string data = null; 
      string connectionString = null; 
      SqlConnection cnn ; 
      connectionString = "some connection string"; 
      while ((true)) 
      { 
       try 
       {  
        NetworkStream networkStream = clientSocket.GetStream(); 
        networkStream.Read(bytesFrom, 0, bytesFrom.Length); 
        data = System.Text.Encoding.ASCII.GetString(bytesFrom); 
        //networkStream.Flush(); 
        //Console.WriteLine(" >> " + "From client-" + clNo + data.Trim()); 
        //get message type from device 
        String gMsgType = data.Slice(13,17); 
        Console.WriteLine("{0}",gMsgType); 
        //if the msg BP05 use it else continue 
        if(String.Compare("BP05",gMsgType) !=0) 
         throw new Exception(); 
        //we got bp05 message parse the inputs 
        String gId = data.Slice(18,32); 
        String gDate = data.Slice(32,38); 
        String gStatus = data.Slice(38,39); 
        String gLat = data.Slice(39,48); 
        String gLon = data.Slice(49,59); 
        String gSpeed = data.Slice(60,65); 
        String gTime = data.Slice(65,71); 
        String gInputs = data.Slice(77,85); 
        String gMileData = data.Slice(86,92); 
        if(String.Compare("A", gStatus)==0) 
        { 
         //sanitize inputs before insertion 
//some operations 
         Console.WriteLine("{0}",data); 
         //connect to database 
         cnn = new SqlConnection(connectionString);   
         cnn.Open(); 
         SqlCommand com = new SqlCommand(); 
         com.Connection = cnn; 
         com.CommandText = "some insert query" 
         com.ExecuteNonQuery(); 
         cnn.Close(); 
        } 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine(" >> " + ex.ToString()); 
        break; 
       } 
      } 
     } 
    } 
} 

回答

0

首先,去掉而(真)的循環並使用某種條件來代替。也許是一個「isRunning」布爾值,您可以根據需要更改以結束程序。其次,

networkStream.Read(bytesFrom, 0, bytesFrom.Length); 

如果我沒有記錯,應該返回從套接字中讀取的字節數。如果客戶端已斷開連接,則可以從套接字讀取0個字節。如果從套接字讀取0個字節,我會從那裏開始並中斷。

0

也許networkStream.Read是一個阻塞函數,爲什麼不嘗試爲Read函數設置超時?