2016-12-30 62 views
1

我正在編寫TCP服務器以提高我對此協議的瞭解。最近,一位社區成員(RenéVogt先生)幫助我建立了我的服務器和多個客戶端之間的連接。將TCP C#數據包發送到特定客戶端

一切正常,答案(一個簡單的活動數據包)正在發送到所有連接的客戶端。但是現在,我想指定一個連接來發送數據包。我將給出一個簡短的解釋:

服務器運行在專用主機上。我的個人計算機以Client1的身份連接到服務器,發送活動數據包(「AA」)並接收服務器回覆。然後,另一個客戶端(Client2,client3 ...和on)連接到服務器。活着將被收到,服務器將回答這個客戶端。但是,另外,我希望向Client1發送一個類似「Client2已連接」(或其他任何內容)的數據包。

我的代碼如下:

using System; 
using System.Threading; 
using System.Net.Sockets; 
using MySql.Data.MySqlClient; 
using System.Net; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      TcpListener serverSocket = new TcpListener(50000); 
      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) 
     { 
       this.clientSocket = inClientSocket; 
       this.clNo = clineNo; 
       Thread ctThread = new Thread(doChat); 
       ctThread.Start(); 

     } 
     private void doChat() 
     { 
      bool LoopReceive = true; 
      bool LoopSend = false; 
      int requestCount = 0; 
      byte[] bytesFrom = new byte[256]; 
      string dataFromClient = null; 
      Byte[] sendBytes = null; 
      string serverResponse = null; 
      string rCount = null; 
      requestCount = 0; 
      string Packettosend = ""; 

      while ((true)) 
      { 
       try 
       { 
        requestCount = requestCount + 1; 
        NetworkStream networkStream = clientSocket.GetStream(); 
        networkStream.Flush(); 
        networkStream.Read(bytesFrom, 0, bytesFrom.Length); 
        dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom); 
        IPEndPoint remoteIpEndPoint = clientSocket.Client.RemoteEndPoint as IPEndPoint; 

        Console.WriteLine(" >> PACKET: " + dataFromClient); 



        if (dataFromClient.StartsWith("AA") 
        { 
         Packettosend = "ALIVE"; 
         Console.WriteLine("::ALIVE PACKET"); 
        } 


        //PACKETS TO SEND 
        if (Packettosend == "ALIVE") 
        { 
         Byte[] sendBytes1 = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }; 
         networkStream.Write(sendBytes1, 0, sendBytes1.Length); 
        } 

       } 
       catch (Exception ex) 
       { 
        Console.WriteLine(" >> " + ex.ToString()); 
       } 
      } 
     } 
    } 
} 

我尋找一些答案,但他們沒有工作。是否有可能將網絡流指向發送數據包的當前客戶端以外的其他任何東西?

在此先感謝!

+0

號相同的連接。您的代碼正在網絡的數據層發送數據。您可以編寫一個應用程序層來處理命令,如「將消息發送到客戶端2」,「列出當前連接」,「是否連接到Clinet2」。服務器然後會處理命令併發送回應。 – jdweng

+0

但是如何?我是一個初學者,如果可能的話,一個簡單的例子會很棒。 –

+0

代碼並不簡單。最好的學習方法是閱讀書籍以瞭解「網絡通信」和「7網絡層」的基本原理。我在網上發佈的大部分代碼都不瞭解基本的通信原理,也沒有將代碼正確地分離到適當的層中。我擁有電氣工程師學士學位,計算機科學碩士學位,博士學位24學分,以及40年工作經驗。我可以寫這本書並教授課程。但不能舉一個簡單的例子。 – jdweng

回答

1

簡單的準則是,存儲客戶端與ID或字典的客戶名單,

Dictionary<TcpClient, int> clintlist= new Dictionary<TcpClient, int>(); 

,當你想 發送特定的客戶端只搜索ID上不上客戶端列表

相關問題