2012-07-30 48 views
0

我正在用android和kinect製作一個雙重應用程序。我想能夠從kinect發送通知給android應用程序。我被告知最好的方法是建立一個簡單的tcp服務器。我試圖通過使用這個< link>的教程進行設置。然而,該教程對我來說並不足夠描述,我無法使其工作。發佈它的人基本上發佈了幾段代碼,沒有任何關於組裝它們的說明。我需要有人或者通過設置此服務器來引導我,或者我需要一個指向詳細教程的鏈接。我已經在網上搜索了幾個小時,但我還沒有發現任何有用的東西,這就是我在這裏問的原因。設置一個tcp服務器與android和kinect通信

+0

您提供的鏈接似乎有比其中的代碼更多的解釋文本。你在看另一個帖子嗎? – matt5784 2012-07-30 15:37:46

+0

該文本解釋了發生的一切。那不是我的問題。然而,解釋文字並沒有說明如何將代碼放在一起。他作爲幾個不同的代碼片段,解釋了它們是如何工作的以及他們做了什麼,但他並沒有告訴如何實際將代碼放在一起,例如正確的順序,正確的類別等。 – 2012-07-30 15:57:09

+0

那麼,ListenForClients和HandleClientComm顯然是進入主服務器類的函數。您可以選擇如何處理代碼以發回內容。我會將我的解釋作爲答案。 – matt5784 2012-07-30 16:13:10

回答

1

這就是我以爲他說該教程中要做到:

using System; 
using System.Text; 
using System.Net.Sockets; 
using System.Threading; 
using System.Net; 

namespace TCPServerTutorial 
{ 
    class Server 
    { 
    private TcpListener tcpListener; 
    private Thread listenThread; 

    public Server() 
    { 
     this.tcpListener = new TcpListener(IPAddress.Any, 3000); 
     this.listenThread = new Thread(new ThreadStart(ListenForClients)); 
     this.listenThread.Start(); 
    } 


    private void HandleClientComm(object client) 
    { 
     TcpClient tcpClient = (TcpClient)client; 
     NetworkStream clientStream = tcpClient.GetStream(); 

     byte[] message = new byte[4096]; 
     int bytesRead; 

     while (true) 
     { 
     bytesRead = 0; 

     try 
     { 
      //blocks until a client sends a message 
      bytesRead = clientStream.Read(message, 0, 4096); 
     } 
     catch 
     { 
      //a socket error has occured 
      break; 
     } 

     if (bytesRead == 0) 
     { 
      //the client has disconnected from the server 
      break; 
     } 

     //message has successfully been received 
     ASCIIEncoding encoder = new ASCIIEncoding(); 
     System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead)); 
     } 

     tcpClient.Close(); 
    } 

    private void ListenForClients() 
    { 
     this.tcpListener.Start(); 

     while (true) 
     { 
     //blocks until a client has connected to the server 
     TcpClient client = this.tcpListener.AcceptTcpClient(); 

     //create a thread to handle communication 
     //with connected client 
     Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm)); 
     clientThread.Start(client); 
     } 
    } 

    //you'll have to find a way to pass this arg 
    private void SendBack(TcpClient tcpClient) 
    { 
    NetworkStream clientStream = tcpClient.GetStream(); 
    ASCIIEncoding encoder = new ASCIIEncoding(); 
    byte[] buffer = encoder.GetBytes("Hello Client!"); 

    clientStream.Write(buffer, 0 , buffer.Length); 
    clientStream.Flush(); 
    } 
    } 
} 

但是,這只是我的。他只講過一堂課,所以假設他所有的功能都屬於這個班級是合乎邏輯的。

對於客戶端代碼,他幾乎只是給你一個函數的代碼(當然是C#),它將發送一些字節到一個IP地址(也就是你的服務器運行的機器的IP) 。你可以把這個函數放在任何C#類中,並以你想要的任何方式調用它。他已經硬編碼了IP地址和要發送的消息,但這些可能很容易成爲傳遞給函數的參數。

private void SendToServer(){ 
    TcpClient client = new TcpClient(); 

    //IP of the server: currently loopback, change to whatever you want 
    IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000); 

    client.Connect(serverEndPoint); 

    NetworkStream clientStream = client.GetStream(); 

    ASCIIEncoding encoder = new ASCIIEncoding(); 

    //Message being sent: "Hello Server!" 
    byte[] buffer = encoder.GetBytes("Hello Server!"); 

    clientStream.Write(buffer, 0 , buffer.Length); 
    clientStream.Flush(); 
} 
+0

謝謝馬特。這正是我想要的。一旦我有完整的代碼,我通常可以很快地弄清楚。標記爲有用,並接受 – 2012-07-30 16:45:34

+0

如果你可以添加的東西的客戶端代碼太多,我將不勝感激。 – 2012-07-30 16:52:11