Question by
ronnelsuan123 · Jul 29, 2016 at 04:41 PM ·
networkingserversockets
how can i transfer my leap motion data from serve to client ?
hi how can i send my leap motion data from my android . using socket server/ client socket connection. this my codes
for my server class MyTcpListener { public static void Main() {
//-------------------------------
TcpListener server = null;
try
{
string servermessage = "";
Int32 port = 1049;
IPAddress localAddr = IPAddress.Parse("192.168.1.38");
server = new TcpListener(localAddr, port);
server.Start();
Console.WriteLine("Server Started");
Byte[] bytes = new Byte[256];
String data = null;
int counter = 0;
while (true)
{
Console.Write("Waiting for a connection... ");
SampleListener listener = new SampleListener();
Controller controller = new Controller();
controller.AddListener(listener);
TcpClient client = server.AcceptTcpClient();
counter++;
//Console.WriteLine("#" + counter + " Connected!");
Console.WriteLine("Client:" + servermessage);
data = null;
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data + counter);
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data + "Client counter:" + counter);
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data + "Client counter:" + counter);
}
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
}
and this is for my client
using UnityEngine; using System.Collections; using System; using System.Net.Sockets; using System.IO; using System.Net; using UnityEngine.Networking;
public class Newnewclient : MonoBehaviour { public string tekst; public float rotatieX; public float rotatieY; public float rotatieZ; public Transform nek;
public byte[] bytesX = new byte[4];
public byte[] bytesY = new byte[4];
public byte[] bytesZ = new byte[4];
public byte[] bytesW = new byte[4];
public float xValue = (float)0.0;
public float yValue = (float)0.0;
public float zValue = (float)0.0;
public Quaternion rot = Quaternion.identity;
public Quaternion sensor = Quaternion.identity;
public float[] qq = new float[4];
bool socketReady = false;
TcpClient mySocket;
NetworkStream theStream;
StreamWriter theWriter;
StreamReader theReader;
String Host = "192.168.1.38";
Int32 Port = 1049;
void Start () {
}
void Update ()
{
//string receivedText = readSocket ();
/*(receivedText != "")
{
tekst = receivedText;
bytesX [0] = (byte)(HexToByte (tekst.Substring (0, 1)) * 16 + HexToByte (tekst.Substring (1, 1)));
bytesX [1] = (byte)(HexToByte (tekst.Substring (2, 1)) * 16 + HexToByte (tekst.Substring (3, 1)));
bytesX [2] = (byte)(HexToByte (tekst.Substring (4, 1)) * 16 + HexToByte (tekst.Substring (5, 1)));
bytesX [3] = (byte)(HexToByte (tekst.Substring (6, 1)) * 16 + HexToByte (tekst.Substring (7,1)));
bytesY [0] = (byte)(HexToByte (tekst.Substring (8, 1)) * 16 + HexToByte (tekst.Substring (9,1)));
bytesY [1] = (byte)(HexToByte (tekst.Substring (10,1)) * 16 + HexToByte (tekst.Substring (11,1)));
bytesY [2] = (byte)(HexToByte (tekst.Substring (12, 1)) * 16 + HexToByte (tekst.Substring (13, 1)));
bytesY [3] = (byte)(HexToByte (tekst.Substring (14,1)) * 16 + HexToByte (tekst.Substring (15,1)));
bytesZ[0] = (byte)(HexToByte (tekst.Substring (16,1)) * 16 + HexToByte (tekst.Substring (17,1)));
bytesZ [1] = (byte)(HexToByte (tekst.Substring (18, 1)) * 16 + HexToByte (tekst.Substring (19, 1)));
bytesZ [2] = (byte)(HexToByte (tekst.Substring (20,1)) * 16 + HexToByte (tekst.Substring (21,1)));
bytesZ [3] = (byte)(HexToByte (tekst.Substring (22,1)) * 16 + HexToByte (tekst.Substring (23,1)));
bytesW [0] = (byte)(HexToByte (tekst.Substring (24,1)) * 16 + HexToByte (tekst.Substring (25,1)));
bytesW [1] = (byte)(HexToByte (tekst.Substring (26,1)) * 16 + HexToByte (tekst.Substring (27,1)));
bytesW [2] = (byte)(HexToByte (tekst.Substring (28,1)) * 16 + HexToByte (tekst.Substring (29,1)));
bytesW [3] = (byte)(HexToByte (tekst.Substring (30,1)) * 16 + HexToByte (tekst.Substring (31,1)));
sensor.x = System.BitConverter.ToSingle (bytesX, 0);
sensor.y = System.BitConverter.ToSingle (bytesY, 0);
sensor.z = System.BitConverter.ToSingle (bytesZ, 0);
sensor.w = System.BitConverter.ToSingle (bytesW, 0);
qq [0] = System.BitConverter.ToSingle (bytesX, 0);
qq [1] = System.BitConverter.ToSingle (bytesY, 0);
qq [2] = System.BitConverter.ToSingle (bytesZ, 0);
qq [3] = System.BitConverter.ToSingle (bytesW, 0);
rotatieX = sensor.eulerAngles.x;
rotatieY = sensor.eulerAngles.y;
rotatieZ = sensor.eulerAngles.z;
rot.x = -qq [1];
rot.y =-qq [1];
rot.z = -qq [2];
rot.w =-qq [3];
rot *= Quaternion.Euler (Vector3.up * yValue);
rot *= Quaternion.Euler (Vector3.right * xValue);
rot *= Quaternion.Euler (Vector3.forward * zValue);
nek.rotation = rot;
} */
}
void OnGUI() {
if (!socketReady)
{
if(GUI.Button(new Rect(150,2,150,200),"Connect"))
{
setupSocket();
string msg = "Sending by Tuks";
writeSocket (msg);
readSocket ();
// writeSocket ("Server Stastus : ");
}
}
// (socketReady) {
//Value = GUI.HorizontalSlider (new Rect (25, 25, 360, 30), (float)xValue, 180.0f, -180.0f);
//Value = GUI.HorizontalSlider (new Rect (25, 25, 360, 30), (float)xValue, 180.0f, -180.0f);
//Value = GUI.HorizontalSlider (new Rect (25, 25, 360, 30), (float)xValue, 180.0f, -180.0f);
//
}
void OnApplicationQuit() {
closeSocket ();
}
public void setupSocket() {
try {
mySocket = new TcpClient (Host, Port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
theReader = new StreamReader(theStream);
socketReady = true;
}
catch (Exception e) {
Debug.Log ("Socket Error : " + e);
}
}
public void writeSocket (string theLine) {
if (!socketReady)
return;
String tmpString = theLine + "\r\n";
theWriter.Write(tmpString);
theWriter.Flush ();
}
public String readSocket() {
if (!socketReady)
return "";
if (theStream.DataAvailable)
return theReader.ReadLine();
return "";
}
public void closeSocket()
{
if ((socketReady) && (mySocket.Connected)) {
if (!socketReady)
return;
theWriter.Close ();
theReader.Close ();
mySocket.Close ();
socketReady = false;
}
}
public void maintainConnection () {
if (!theStream.CanRead) {
setupSocket();
}
}
/*private byte HexToByte(string hexVal) { if (hexVal == "0") return (0);
else if (hexVal == "1") return (1);
else if (hexVal == "2") return (2);
else if (hexVal == "3") return (3);
else if (hexVal == "4") return (4);
else if (hexVal == "5") return (5);
else if (hexVal == "6") return (6);
else if (hexVal == "7") return (7);
else if (hexVal == "8") return (8);
else if (hexVal == "9") return (9);
else if (hexVal == "A") return (10);
else if (hexVal == "B") return (11);
else if (hexVal == "C") return (12);
else if (hexVal == "D") return (13);
else if (hexVal == "E") return (14);
else if (hexVal == "F") return (15);
return(0);
} */
}
Comment