-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDPReceive.cs
145 lines (114 loc) · 3 KB
/
UDPReceive.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class UDPReceive : MonoBehaviour {
public static UDPReceive instance;
Thread receiveThread;
UdpClient client;
public int port = 5000;
public string IP; // = "192.168.1.193";
// infos
public string lastReceivedUDPPacket="";
public string allReceivedUDPPackets=""; // clean up this from time to time!
//public VideoPlayerTest VPT;
public string LocalIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
}
private void Awake()
{
instance = this;
string host = System.Net.Dns.GetHostName();
IP = System.Net.Dns.GetHostEntry(host).AddressList[0].ToString();
IP = LocalIPAddress();
Debug.Log("Super Brand new IP: " + IP);
}
public void Start()
{
init();
}
private void init()
{
print("UDPSend.init()");
// define port
port = 5000;
// status
print("Sending to " + IP + " : " + port);
print("Test-Sending to this Port: nc -u \"" + IP + port + "");
receiveThread = new Thread(
new ThreadStart(ReceiveData));
receiveThread.IsBackground = true;
receiveThread.Start();
}
void OnDisable()
{
if ( receiveThread!= null)
receiveThread.Abort();
//client.Close();
}
// start from shell
private static void Main()
{
UDPReceive receiveObj=new UDPReceive();
receiveObj.init();
string text="";
do
{
text = Console.ReadLine();
}
while(!text.Equals("exit"));
}
// OnGUI
void OnGUI()
{
Rect rectObj=new Rect(40,10,200,400);
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.UpperLeft;
GUI.Box(rectObj,"# UDPReceive\n+"+IP+port+" #\n"
+ "shell> nc -u "+IP+port+" \n"
+ "\nLast Packet: \n"+ lastReceivedUDPPacket
+ "\n\nAll Messages: \n"+allReceivedUDPPackets
,style);
}
// receive thread
private void ReceiveData()
{
client = new UdpClient(port);
while (true)
{
try
{
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
string text = Encoding.UTF8.GetString(data);
print(">> " + text);
lastReceivedUDPPacket = text;
MainUI.instance.ProcessCommand(lastReceivedUDPPacket);
}
catch (Exception err)
{
print(err.ToString());
}
}
}
public string getLatestUDPPacket()
{
allReceivedUDPPackets="";
return lastReceivedUDPPacket;
}
}