質問編集履歴
3
上司がコードをある程度作ってきてくれました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,274 @@
|
|
1
|
+
課題で
|
2
|
+
|
3
|
+
今回悩んでいたところ
|
4
|
+
|
5
|
+
上司がUDP通信のソースを作ってきてくれました
|
6
|
+
|
7
|
+
このソースは受信と送信を同時に行っているんでしょうか?
|
8
|
+
|
9
|
+
見てもあんまり意味がわかりませんでした
|
10
|
+
|
11
|
+
ただ16進数にしなくちゃいけないが10進数になっているといわれました。
|
12
|
+
|
13
|
+
```using System.Collections;
|
14
|
+
|
15
|
+
using System.Collections.Generic;
|
16
|
+
|
17
|
+
using UnityEngine;
|
18
|
+
|
19
|
+
using System.Net;
|
20
|
+
|
21
|
+
using System.Net.Sockets;
|
22
|
+
|
23
|
+
using System.Threading;
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
public class UdpSocket : MonoBehaviour
|
28
|
+
|
29
|
+
{
|
30
|
+
|
31
|
+
private string _MulticastAddr = "224.0.23.0"; // マルチキャストアドレス
|
32
|
+
|
33
|
+
private string _RemoteHost = ""; // 送信相手アドレス
|
34
|
+
|
35
|
+
private int _SendPort = 3610; // 送信ポート
|
36
|
+
|
37
|
+
private int _RecvPort = 3610; // 受信ポート
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
private UdpClient _UdpClient; // UDP
|
42
|
+
|
43
|
+
private IPEndPoint _IpEndPoint; // IPEndPoint
|
44
|
+
|
45
|
+
private Thread _RecvThread; // 受信スレッド
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
// 接続
|
50
|
+
|
51
|
+
public void Connect()
|
52
|
+
|
53
|
+
{
|
54
|
+
|
55
|
+
IPAddress grpAddr = IPAddress.Parse(_MulticastAddr);
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
if (_IpEndPoint != null) _IpEndPoint = null;
|
60
|
+
|
61
|
+
_IpEndPoint = new IPEndPoint(grpAddr, _RecvPort);
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
// マルチキャストグループに参加
|
66
|
+
|
67
|
+
Disconnect();
|
68
|
+
|
69
|
+
_UdpClient = new UdpClient(_RecvPort);
|
70
|
+
|
71
|
+
_UdpClient.JoinMulticastGroup(grpAddr);
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
// 受信スレッド生成
|
76
|
+
|
77
|
+
_RecvThread = new Thread(ReceiveMulticastThread);
|
78
|
+
|
79
|
+
_RecvThread.Start();
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
// ノードプロファイル通知送信
|
84
|
+
|
85
|
+
SendNodeProfile();
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
// 切断
|
92
|
+
|
93
|
+
public void Disconnect()
|
94
|
+
|
95
|
+
{
|
96
|
+
|
97
|
+
if (_RecvThread != null)
|
98
|
+
|
99
|
+
{
|
100
|
+
|
101
|
+
_RecvThread.Abort();
|
102
|
+
|
103
|
+
_RecvThread = null;
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
if (_UdpClient != null)
|
110
|
+
|
111
|
+
{
|
112
|
+
|
113
|
+
IPAddress grpAddr = IPAddress.Parse(_MulticastAddr);
|
114
|
+
|
115
|
+
_UdpClient.DropMulticastGroup(grpAddr);
|
116
|
+
|
117
|
+
_UdpClient.Close();
|
118
|
+
|
119
|
+
_UdpClient = null;
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
// ノードプロファイル通知送信
|
128
|
+
|
129
|
+
public void SendNodeProfile()
|
130
|
+
|
131
|
+
{
|
132
|
+
|
133
|
+
byte[] pack = BuildNodeProfileInfo();
|
134
|
+
|
135
|
+
SendPacket(pack, _MulticastAddr);
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
// 送信
|
142
|
+
|
143
|
+
public void SendPacket(byte[] packet, string host)
|
144
|
+
|
145
|
+
{
|
146
|
+
|
147
|
+
_UdpClient.Send(packet, packet.Length, host, _SendPort);
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
// 受信スレッド
|
154
|
+
|
155
|
+
public void ReceiveMulticastThread()
|
156
|
+
|
157
|
+
{
|
158
|
+
|
159
|
+
byte[] packet;
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
int i = 0;
|
164
|
+
|
165
|
+
System.Text.StringBuilder s = new System.Text.StringBuilder();
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
packet = _UdpClient.Receive(ref _IpEndPoint);
|
170
|
+
|
171
|
+
if (packet != null)
|
172
|
+
|
173
|
+
{
|
174
|
+
|
175
|
+
// 受信できた!
|
176
|
+
|
177
|
+
s.Remove(0, s.Length);
|
178
|
+
|
179
|
+
for (i=0; i<packet.Length; i++)
|
180
|
+
|
181
|
+
{
|
182
|
+
|
183
|
+
s.Append(System.Convert.ToString(packet[i], 16).PadLeft(2, '0'));
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
Debug.Log(s.ToString());
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
// ノードプロファイル通知パケット作成
|
198
|
+
|
199
|
+
private byte[] BuildNodeProfileInfo()
|
200
|
+
|
201
|
+
{
|
202
|
+
|
203
|
+
byte[] pack;
|
204
|
+
|
205
|
+
pack = new byte[17];
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
pack[0] = 0x10; // EHD1
|
210
|
+
|
211
|
+
pack[1] = 0x81; // EHD2
|
212
|
+
|
213
|
+
pack[2] = 0x00; //
|
214
|
+
|
215
|
+
pack[3] = 0x01; // ID
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
pack[4] = 0x0E; // 送信元「ノードプロファイルクラス」
|
220
|
+
|
221
|
+
pack[5] = 0xF0; // EOJ = 0x0E F0 01
|
222
|
+
|
223
|
+
pack[6] = 0x01; //
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
pack[7] = 0x0E; // 送信先「ノードプロファイルクラス」
|
228
|
+
|
229
|
+
pack[8] = 0xF0; // EOJ = 0x0E F0 01
|
230
|
+
|
231
|
+
pack[9] = 0x01; //
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
pack[10] = 0x73; // ESV
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
pack[11] = 0x01; // OPC
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
pack[12] = 0xD5; // EPC
|
244
|
+
|
245
|
+
pack[13] = 0x04; // PDC
|
246
|
+
|
247
|
+
pack[14] = 0x01; // EDT
|
248
|
+
|
249
|
+
pack[15] = 0x05; //
|
250
|
+
|
251
|
+
pack[16] = 0xFF; //
|
252
|
+
|
253
|
+
pack[16] = 0x01; //
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
return pack;
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
コード
|
266
|
+
|
267
|
+
```
|
268
|
+
|
269
|
+
コード
|
270
|
+
|
1
|
-
### 前提・実現したいこと
|
271
|
+
```### 前提・実現したいこと
|
2
272
|
|
3
273
|
unityでUDP通信の送信をしたい
|
4
274
|
|
@@ -91,55 +361,3 @@
|
|
91
361
|
|
92
362
|
|
93
363
|
}
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
```ここに言語を入力
|
98
|
-
|
99
|
-
コード
|
100
|
-
|
101
|
-
受信用です
|
102
|
-
|
103
|
-
using UnityEngine;
|
104
|
-
|
105
|
-
using System.Collections;
|
106
|
-
|
107
|
-
using System.Net;
|
108
|
-
|
109
|
-
using System.Net.Sockets;
|
110
|
-
|
111
|
-
using System.Text;
|
112
|
-
|
113
|
-
using System.Threading;
|
114
|
-
|
115
|
-
public class UDPReceive : MonoBehaviour
|
116
|
-
|
117
|
-
{
|
118
|
-
|
119
|
-
int LOCA_LPORT = 3610;
|
120
|
-
|
121
|
-
static UdpClient udp;
|
122
|
-
|
123
|
-
Thread thread; // Use this for initialization
|
124
|
-
|
125
|
-
void Start (){ udp = new UdpClient(LOCA_LPORT);
|
126
|
-
|
127
|
-
udp.Client.ReceiveTimeout = 1000; thread = new Thread(new ThreadStart(ThreadMethod));
|
128
|
-
|
129
|
-
thread.Start(); } // Update is called once per frame void Update ()
|
130
|
-
|
131
|
-
{
|
132
|
-
|
133
|
-
} void OnApplicationQuit() { thread.Abort(); }
|
134
|
-
|
135
|
-
private static void ThreadMethod()
|
136
|
-
|
137
|
-
{
|
138
|
-
|
139
|
-
while (true)
|
140
|
-
|
141
|
-
{
|
142
|
-
|
143
|
-
IPEndPoint remoteEP = null; byte[] data = udp.Receive(ref remoteEP); string text = Encoding.ASCII.GetString(data); Debug.Log(text); } } }
|
144
|
-
|
145
|
-
```
|
2
初心者であることを強調しました
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
1
受信用も一応載せました
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,7 +10,15 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
-
恐らくソースの何かがたらないです
|
13
|
+
恐らくソースの何かがたらないです```ここに言語を入力
|
14
|
+
|
15
|
+
コード
|
16
|
+
|
17
|
+
送信コードが遅れたことも無反応なため
|
18
|
+
|
19
|
+
何か足りていないと思いました。
|
20
|
+
|
21
|
+
```
|
14
22
|
|
15
23
|
|
16
24
|
|
@@ -90,4 +98,48 @@
|
|
90
98
|
|
91
99
|
コード
|
92
100
|
|
101
|
+
受信用です
|
102
|
+
|
103
|
+
using UnityEngine;
|
104
|
+
|
105
|
+
using System.Collections;
|
106
|
+
|
107
|
+
using System.Net;
|
108
|
+
|
109
|
+
using System.Net.Sockets;
|
110
|
+
|
111
|
+
using System.Text;
|
112
|
+
|
113
|
+
using System.Threading;
|
114
|
+
|
115
|
+
public class UDPReceive : MonoBehaviour
|
116
|
+
|
117
|
+
{
|
118
|
+
|
119
|
+
int LOCA_LPORT = 3610;
|
120
|
+
|
121
|
+
static UdpClient udp;
|
122
|
+
|
123
|
+
Thread thread; // Use this for initialization
|
124
|
+
|
125
|
+
void Start (){ udp = new UdpClient(LOCA_LPORT);
|
126
|
+
|
127
|
+
udp.Client.ReceiveTimeout = 1000; thread = new Thread(new ThreadStart(ThreadMethod));
|
128
|
+
|
129
|
+
thread.Start(); } // Update is called once per frame void Update ()
|
130
|
+
|
131
|
+
{
|
132
|
+
|
133
|
+
} void OnApplicationQuit() { thread.Abort(); }
|
134
|
+
|
135
|
+
private static void ThreadMethod()
|
136
|
+
|
137
|
+
{
|
138
|
+
|
139
|
+
while (true)
|
140
|
+
|
141
|
+
{
|
142
|
+
|
143
|
+
IPEndPoint remoteEP = null; byte[] data = udp.Receive(ref remoteEP); string text = Encoding.ASCII.GetString(data); Debug.Log(text); } } }
|
144
|
+
|
93
145
|
```
|