2012-07-30 47 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