質問編集履歴
3
上司がコードをある程度作ってきてくれました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,139 @@
|
|
1
|
+
課題で
|
2
|
+
今回悩んでいたところ
|
3
|
+
上司がUDP通信のソースを作ってきてくれました
|
4
|
+
このソースは受信と送信を同時に行っているんでしょうか?
|
5
|
+
見てもあんまり意味がわかりませんでした
|
6
|
+
ただ16進数にしなくちゃいけないが10進数になっているといわれました。
|
7
|
+
```using System.Collections;
|
8
|
+
using System.Collections.Generic;
|
9
|
+
using UnityEngine;
|
10
|
+
using System.Net;
|
11
|
+
using System.Net.Sockets;
|
12
|
+
using System.Threading;
|
13
|
+
|
14
|
+
public class UdpSocket : MonoBehaviour
|
15
|
+
{
|
16
|
+
private string _MulticastAddr = "224.0.23.0"; // マルチキャストアドレス
|
17
|
+
private string _RemoteHost = ""; // 送信相手アドレス
|
18
|
+
private int _SendPort = 3610; // 送信ポート
|
19
|
+
private int _RecvPort = 3610; // 受信ポート
|
20
|
+
|
21
|
+
private UdpClient _UdpClient; // UDP
|
22
|
+
private IPEndPoint _IpEndPoint; // IPEndPoint
|
23
|
+
private Thread _RecvThread; // 受信スレッド
|
24
|
+
|
25
|
+
// 接続
|
26
|
+
public void Connect()
|
27
|
+
{
|
28
|
+
IPAddress grpAddr = IPAddress.Parse(_MulticastAddr);
|
29
|
+
|
30
|
+
if (_IpEndPoint != null) _IpEndPoint = null;
|
31
|
+
_IpEndPoint = new IPEndPoint(grpAddr, _RecvPort);
|
32
|
+
|
33
|
+
// マルチキャストグループに参加
|
34
|
+
Disconnect();
|
35
|
+
_UdpClient = new UdpClient(_RecvPort);
|
36
|
+
_UdpClient.JoinMulticastGroup(grpAddr);
|
37
|
+
|
38
|
+
// 受信スレッド生成
|
39
|
+
_RecvThread = new Thread(ReceiveMulticastThread);
|
40
|
+
_RecvThread.Start();
|
41
|
+
|
42
|
+
// ノードプロファイル通知送信
|
43
|
+
SendNodeProfile();
|
44
|
+
}
|
45
|
+
|
46
|
+
// 切断
|
47
|
+
public void Disconnect()
|
48
|
+
{
|
49
|
+
if (_RecvThread != null)
|
50
|
+
{
|
51
|
+
_RecvThread.Abort();
|
52
|
+
_RecvThread = null;
|
53
|
+
}
|
54
|
+
|
55
|
+
if (_UdpClient != null)
|
56
|
+
{
|
57
|
+
IPAddress grpAddr = IPAddress.Parse(_MulticastAddr);
|
58
|
+
_UdpClient.DropMulticastGroup(grpAddr);
|
59
|
+
_UdpClient.Close();
|
60
|
+
_UdpClient = null;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
// ノードプロファイル通知送信
|
65
|
+
public void SendNodeProfile()
|
66
|
+
{
|
67
|
+
byte[] pack = BuildNodeProfileInfo();
|
68
|
+
SendPacket(pack, _MulticastAddr);
|
69
|
+
}
|
70
|
+
|
71
|
+
// 送信
|
72
|
+
public void SendPacket(byte[] packet, string host)
|
73
|
+
{
|
74
|
+
_UdpClient.Send(packet, packet.Length, host, _SendPort);
|
75
|
+
}
|
76
|
+
|
77
|
+
// 受信スレッド
|
78
|
+
public void ReceiveMulticastThread()
|
79
|
+
{
|
80
|
+
byte[] packet;
|
81
|
+
|
82
|
+
int i = 0;
|
83
|
+
System.Text.StringBuilder s = new System.Text.StringBuilder();
|
84
|
+
|
85
|
+
packet = _UdpClient.Receive(ref _IpEndPoint);
|
86
|
+
if (packet != null)
|
87
|
+
{
|
88
|
+
// 受信できた!
|
89
|
+
s.Remove(0, s.Length);
|
90
|
+
for (i=0; i<packet.Length; i++)
|
91
|
+
{
|
92
|
+
s.Append(System.Convert.ToString(packet[i], 16).PadLeft(2, '0'));
|
93
|
+
}
|
94
|
+
Debug.Log(s.ToString());
|
95
|
+
}
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
// ノードプロファイル通知パケット作成
|
100
|
+
private byte[] BuildNodeProfileInfo()
|
101
|
+
{
|
102
|
+
byte[] pack;
|
103
|
+
pack = new byte[17];
|
104
|
+
|
105
|
+
pack[0] = 0x10; // EHD1
|
106
|
+
pack[1] = 0x81; // EHD2
|
107
|
+
pack[2] = 0x00; //
|
108
|
+
pack[3] = 0x01; // ID
|
109
|
+
|
110
|
+
pack[4] = 0x0E; // 送信元「ノードプロファイルクラス」
|
111
|
+
pack[5] = 0xF0; // EOJ = 0x0E F0 01
|
112
|
+
pack[6] = 0x01; //
|
113
|
+
|
114
|
+
pack[7] = 0x0E; // 送信先「ノードプロファイルクラス」
|
115
|
+
pack[8] = 0xF0; // EOJ = 0x0E F0 01
|
116
|
+
pack[9] = 0x01; //
|
117
|
+
|
118
|
+
pack[10] = 0x73; // ESV
|
119
|
+
|
120
|
+
pack[11] = 0x01; // OPC
|
121
|
+
|
122
|
+
pack[12] = 0xD5; // EPC
|
123
|
+
pack[13] = 0x04; // PDC
|
124
|
+
pack[14] = 0x01; // EDT
|
125
|
+
pack[15] = 0x05; //
|
126
|
+
pack[16] = 0xFF; //
|
127
|
+
pack[16] = 0x01; //
|
128
|
+
|
129
|
+
return pack;
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
コード
|
134
|
+
```
|
135
|
+
コード
|
1
|
-
### 前提・実現したいこと
|
136
|
+
```### 前提・実現したいこと
|
2
137
|
unityでUDP通信の送信をしたい
|
3
138
|
エラーはないのですが
|
4
139
|
ボタンを押しても送信されません
|
@@ -44,30 +179,4 @@
|
|
44
179
|
client.Close();
|
45
180
|
}
|
46
181
|
|
47
|
-
}
|
182
|
+
}
|
48
|
-
|
49
|
-
```ここに言語を入力
|
50
|
-
コード
|
51
|
-
受信用です
|
52
|
-
using UnityEngine;
|
53
|
-
using System.Collections;
|
54
|
-
using System.Net;
|
55
|
-
using System.Net.Sockets;
|
56
|
-
using System.Text;
|
57
|
-
using System.Threading;
|
58
|
-
public class UDPReceive : MonoBehaviour
|
59
|
-
{
|
60
|
-
int LOCA_LPORT = 3610;
|
61
|
-
static UdpClient udp;
|
62
|
-
Thread thread; // Use this for initialization
|
63
|
-
void Start (){ udp = new UdpClient(LOCA_LPORT);
|
64
|
-
udp.Client.ReceiveTimeout = 1000; thread = new Thread(new ThreadStart(ThreadMethod));
|
65
|
-
thread.Start(); } // Update is called once per frame void Update ()
|
66
|
-
{
|
67
|
-
} void OnApplicationQuit() { thread.Abort(); }
|
68
|
-
private static void ThreadMethod()
|
69
|
-
{
|
70
|
-
while (true)
|
71
|
-
{
|
72
|
-
IPEndPoint remoteEP = null; byte[] data = udp.Receive(ref remoteEP); string text = Encoding.ASCII.GetString(data); Debug.Log(text); } } }
|
73
|
-
```
|
2
初心者であることを強調しました
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|
1
受信用も一応載せました
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,7 +4,11 @@
|
|
4
4
|
ボタンを押しても送信されません
|
5
5
|
まず無反応です。何も起こりません
|
6
6
|
|
7
|
-
恐らくソースの何かがたらないです
|
7
|
+
恐らくソースの何かがたらないです```ここに言語を入力
|
8
|
+
コード
|
9
|
+
送信コードが遅れたことも無反応なため
|
10
|
+
何か足りていないと思いました。
|
11
|
+
```
|
8
12
|
|
9
13
|
using UnityEngine;
|
10
14
|
using System.Net.Sockets;
|
@@ -44,4 +48,26 @@
|
|
44
48
|
|
45
49
|
```ここに言語を入力
|
46
50
|
コード
|
51
|
+
受信用です
|
52
|
+
using UnityEngine;
|
53
|
+
using System.Collections;
|
54
|
+
using System.Net;
|
55
|
+
using System.Net.Sockets;
|
56
|
+
using System.Text;
|
57
|
+
using System.Threading;
|
58
|
+
public class UDPReceive : MonoBehaviour
|
59
|
+
{
|
60
|
+
int LOCA_LPORT = 3610;
|
61
|
+
static UdpClient udp;
|
62
|
+
Thread thread; // Use this for initialization
|
63
|
+
void Start (){ udp = new UdpClient(LOCA_LPORT);
|
64
|
+
udp.Client.ReceiveTimeout = 1000; thread = new Thread(new ThreadStart(ThreadMethod));
|
65
|
+
thread.Start(); } // Update is called once per frame void Update ()
|
66
|
+
{
|
67
|
+
} void OnApplicationQuit() { thread.Abort(); }
|
68
|
+
private static void ThreadMethod()
|
69
|
+
{
|
70
|
+
while (true)
|
71
|
+
{
|
72
|
+
IPEndPoint remoteEP = null; byte[] data = udp.Receive(ref remoteEP); string text = Encoding.ASCII.GetString(data); Debug.Log(text); } } }
|
47
73
|
```
|