Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity is only printing first message from server 0 Answers

Best way to learn Networking in general 0 Answers

Is there any benefit to calling NetworkTransport.Receive multiple times per tick? 0 Answers

Connecting SQL with Unity 0 Answers

c# .net sockets sending method over network 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges