Search Unity

Read a UDP package

Discussion in 'Scripting' started by Dazzid, Sep 12, 2016.

  1. Dazzid

    Dazzid

    Joined:
    Apr 15, 2014
    Posts:
    61
    Hi,
    I'm trying to use this code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Net;
    4. using System.Net.Sockets;
    5. using System.Threading;
    6. using System.IO;
    7. using System;
    8. using System.Text;
    9.  
    10. public delegate void ActivityEventHandlerUDP( string msg );
    11.  
    12. public class ActivityListener : MonoBehaviour
    13. {
    14.     private System.Threading.Thread ActivityListenerThread;
    15.     private bool run = true;
    16.     public event ActivityEventHandlerUDP activityData;
    17.  
    18.     private int receivePortUDP = 9901; //4252;
    19.     private UdpClient client;
    20.     private IPEndPoint remoteIpEndPoint;
    21.  
    22.     public string myMessage="";
    23.  
    24.  
    25.  
    26.     void Start(){
    27.         Debug.Log ("Start Activity remote listener");
    28.         //Only start listener if you are the main display
    29.         //if(XimDisplayNetworkSettings.getXimNetworkingMode() == XimDisplayNetworkSettings.XIMNetworkingmodes.SERVER){
    30.         //UDP
    31.         /*
    32.  
    33.  
    34.         */
    35.         //}
    36.  
    37.         client = new System.Net.Sockets.UdpClient( receivePortUDP );
    38.  
    39.         remoteIpEndPoint = new System.Net.IPEndPoint( System.Net.IPAddress.Any, receivePortUDP );
    40.  
    41.         ActivityListenerThread = new System.Threading.Thread(new System.Threading.ThreadStart( listenActivityUDPThread ) );    
    42.         ActivityListenerThread.Name = "Activity UDP listen thread";
    43.         ActivityListenerThread.Start();
    44.  
    45.     }
    46.  
    47.     void OnDestroy(){
    48.         run = false;
    49.         if (ActivityListenerThread != null) ActivityListenerThread.Abort();
    50.         if( client != null ) client.Close();
    51.     }
    52.  
    53.  
    54.     private void listenActivityUDPThread(){
    55.         while(run){
    56.  
    57.             try{
    58.                 //Read the bottle
    59.                 byte[] packet = client.Receive( ref remoteIpEndPoint );
    60.                 // send message //
    61.                 if( packet != null && packet.Length > 0 ){
    62.                     string message = ExtractString( packet, 0, packet.Length );
    63.                     Debug.Log (message);
    64.                     myMessage = message;
    65.                     if(activityData != null){
    66.                         activityData(message);
    67.                     }
    68.                 }
    69.                 Debug.Log(myMessage);
    70.             }
    71.             catch (Exception e)
    72.             {
    73.                 Debug.Log( e.ToString() );
    74.             }
    75.         }
    76.     }
    77.  
    78.     private string ExtractString( byte[] packet, int start, int length )
    79.     {
    80.         StringBuilder sb = new StringBuilder();
    81.         for (int i = 0; i < packet.Length; i++) {
    82.             sb.Append ((char)packet [i]);
    83.         }
    84.         return sb.ToString();
    85.     }
    86.  
    87.     void ReadData(UdpClient ci, String text){
    88.         Console.WriteLine("Received text message: "+text);
    89.     }
    90. }
    And it works kind of well, but I don't find the way to extract the info of the osc package I'm sending.
    The message is:
    /activity "0.6343149, 0.24177371, 0.124862194, 0.9113936, 0.12371367, 0.48712423, 0.42828846, 0.69702476, 0.92075056, 0.104008615, 0.34763572, 0.2345885, 0.059998512, 0.31695354, 0.6864477, 0.8406349, 0.82458776, 0.22075915, 0.36120734, 0.975904, ..."
    But in Unity I only read "/activity" as a message, the rest of the numbers doesn't appear.
    Thanks for the help
    David