我正在用android和kinect製作一個雙重應用程序。我想能夠從kinect發送通知給android應用程序。我被告知最好的方法是建立一個簡單的tcp服務器。我試圖通過使用這個< link>的教程進行設置。然而,該教程對我來說並不足夠描述,我無法使其工作。發佈它的人基本上發佈了幾段代碼,沒有任何關於組裝它們的說明。我需要有人或者通過設置此服務器來引導我,或者我需要一個指向詳細教程的鏈接。我已經在網上搜索了幾個小時,但我還沒有發現任何有用的東西,這就是我在這裏問的原因。設置一個tcp服務器與android和kinect通信
0
A
回答
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
相關問題
- 1. node.js - 與TCP服務器通信(data == JSON)
- 2. 與TCP套接字(服務器)通信
- 3. Android與服務器通信
- 4. 設置一個TCP/IP客戶端和服務器通過網絡進行通信
- 5. C#Tcp服務器 - HTML5 Websocket通信
- 6. 客戶端 - 服務器TCP通信
- 7. TCP到服務器數據庫通信
- 8. PHP客戶端通過TCP與C++服務器通信
- 9. 安全的android與服務器通信
- 10. Android - Web服務器通信
- 11. 通過XML與服務器和Android客戶端進行通信
- 12. 與http服務器通信
- 13. java tcp服務器到android設備
- 14. 將服務器端口與客戶端通信? (TCP)
- 15. Android:與真實設備的客戶端 - 服務器通信
- 16. 設置替代Node.js服務與本地分析服務器通信
- 17. Android中的設備到設備通信的GCM服務器/發件人設置
- 18. C++:一個客戶端與多個服務器通信
- 19. MySQL啓用服務器通過TCP/IP進行通信
- 20. android如何與動態服務通信?
- 21. android和兩個php web服務器之間的通信
- 22. 如何通過Android與PHP服務器進行通信
- 23. Android應用程序通過插座與服務器通信
- 24. Twisted - UDP和TCP服務器在一起
- 25. TCP服務器和MemoryStreams/ByteArrays
- 26. Android客戶端服務器通信
- 27. Android:活動和服務通信
- 28. Android活動和服務通信
- 29. HTTP頭信息服務器設置
- 30. Linux C++服務器與Javascript通信
您提供的鏈接似乎有比其中的代碼更多的解釋文本。你在看另一個帖子嗎? – matt5784 2012-07-30 15:37:46
該文本解釋了發生的一切。那不是我的問題。然而,解釋文字並沒有說明如何將代碼放在一起。他作爲幾個不同的代碼片段,解釋了它們是如何工作的以及他們做了什麼,但他並沒有告訴如何實際將代碼放在一起,例如正確的順序,正確的類別等。 – 2012-07-30 15:57:09
那麼,ListenForClients和HandleClientComm顯然是進入主服務器類的函數。您可以選擇如何處理代碼以發回內容。我會將我的解釋作爲答案。 – matt5784 2012-07-30 16:13:10