Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9.  
  10. public class UdpReceive : MonoBehaviour
  11. {
  12. public int port = 2002;
  13. private UdpClient client;
  14. private IPEndPoint RemoteIpEndPoint;
  15. private Thread t_udp;
  16. //public ArrayList tmpList;
  17. public float[] maxValues;
  18.  
  19. void Start()
  20. {
  21. client = new UdpClient(port);
  22. RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
  23. t_udp = new Thread(new ThreadStart(UDPRead));
  24. t_udp.Name = "UDP thread";
  25. t_udp.Start();
  26.  
  27.  
  28. //FilterData(test);
  29. }
  30.  
  31. public void UDPRead()
  32. {
  33. while (true)
  34. {
  35. try
  36. {
  37. //Debug.Log("listening UDP port " + port);
  38. byte[] receiveBytes = client.Receive(ref RemoteIpEndPoint);
  39. string returnData = Encoding.ASCII.GetString(receiveBytes);
  40. // parsing //
  41. FilterData(returnData);
  42. }
  43. catch (Exception e)
  44. {
  45. Debug.Log("Not so good " + e.ToString());
  46. }
  47. Thread.Sleep(20);
  48. }
  49. }
  50.  
  51. void OnDisable()
  52. {
  53. if (t_udp != null) t_udp.Abort();
  54. client.Close();
  55. }
  56.  
  57. public float MaxValue(int index)
  58. {
  59. return maxValues[index];
  60. }
  61.  
  62. public void FilterData(string dataString)
  63. {
  64. string[] splitString = dataString.Split(":"[0]);
  65. maxValues = new float[splitString.Length];
  66.  
  67. for( int i=0; i<maxValues.Length; i++ ) {
  68. maxValues[i] = float.Parse(splitString[i]);
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement