質問編集履歴
3
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -11,6 +11,8 @@
|
|
11
11
|
参考にしているコードはこちらです。http://qiita.com/nenjiru/items/b1ffd4a01b3d96b2bbb7
|
12
12
|
|
13
13
|
|
14
|
+
|
15
|
+
```ここに言語を入力
|
14
16
|
|
15
17
|
#define _WINSOCK_DEPRECATED_NO_WARNINGS
|
16
18
|
|
@@ -102,6 +104,8 @@
|
|
102
104
|
|
103
105
|
}
|
104
106
|
|
107
|
+
```
|
108
|
+
|
105
109
|
私はVisual Studioを使用しているため、サイトのコードを少し変更しました。
|
106
110
|
|
107
111
|
sendto(udp, (unsigned char*)data, sizeof data, 0, (struct sockaddr *)&addr, sizeof(addr));のところでエラーが出てしまいます。
|
2
ソースコードの修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
C
|
1
|
+
C言語のUDP通信について
|
test
CHANGED
File without changes
|
1
ソースコードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,17 +12,13 @@
|
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
-
|
15
|
+
#define _WINSOCK_DEPRECATED_NO_WARNINGS
|
16
16
|
|
17
17
|
#include <stdio.h>
|
18
18
|
|
19
19
|
#include <string.h>
|
20
20
|
|
21
|
-
#include <
|
21
|
+
#include <WinSock2.h>
|
22
|
-
|
23
|
-
#include <netdb.h>
|
24
|
-
|
25
|
-
#include <sys/socket.h>
|
26
22
|
|
27
23
|
|
28
24
|
|
@@ -30,13 +26,15 @@
|
|
30
26
|
|
31
27
|
|
32
28
|
|
33
|
-
void udpSend
|
29
|
+
void udpSend(char* address, int port);
|
34
30
|
|
35
|
-
int port = 20001;
|
31
|
+
int port = 20001;//ポート番号
|
36
32
|
|
37
|
-
char addr[] = "0.0.0.0";
|
33
|
+
char addr[] = "0.0.0.0";//配列を用意
|
38
34
|
|
39
|
-
double data[] = { 0, 1, 0.0005, -0.0009 };
|
35
|
+
double data[] = { 0, 1, 0.0005, -0.0009 };//送信するデータ
|
36
|
+
|
37
|
+
|
40
38
|
|
41
39
|
|
42
40
|
|
@@ -44,9 +42,17 @@
|
|
44
42
|
|
45
43
|
{
|
46
44
|
|
47
|
-
|
45
|
+
/* Windows 独自の設定 */
|
48
46
|
|
47
|
+
WSADATA data;
|
48
|
+
|
49
|
+
WSAStartup(MAKEWORD(2, 0), &data);
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
udpSend(addr, port);
|
54
|
+
|
49
|
-
|
55
|
+
return 0;
|
50
56
|
|
51
57
|
}
|
52
58
|
|
@@ -56,155 +62,51 @@
|
|
56
62
|
|
57
63
|
{
|
58
64
|
|
59
|
-
|
65
|
+
int udp = socket(AF_INET, SOCK_DGRAM, 0);
|
60
66
|
|
61
|
-
|
67
|
+
struct sockaddr_in addr;
|
62
68
|
|
63
|
-
|
69
|
+
struct hostent *host;
|
64
70
|
|
65
71
|
|
66
72
|
|
67
|
-
|
73
|
+
/*構造体のセット*/
|
68
74
|
|
69
|
-
|
75
|
+
memset(&addr, 0, sizeof(addr));
|
70
76
|
|
71
|
-
|
77
|
+
host = gethostbyname(address);
|
72
78
|
|
73
|
-
|
79
|
+
memcpy(host->h_addr, &addr.sin_addr, host->h_length);
|
74
80
|
|
81
|
+
addr.sin_family = AF_INET;
|
82
|
+
|
75
|
-
|
83
|
+
addr.sin_port = htons(port);
|
76
84
|
|
77
85
|
|
78
86
|
|
79
|
-
|
87
|
+
while (1)
|
80
88
|
|
81
|
-
|
89
|
+
{
|
82
90
|
|
83
|
-
|
91
|
+
sendto(udp, (unsigned char*)data, sizeof data, 0, (struct sockaddr *)&addr, sizeof(addr));
|
84
92
|
|
85
|
-
|
93
|
+
Sleep(INTERVAL_TIME);
|
86
94
|
|
87
|
-
|
95
|
+
}
|
88
96
|
|
89
97
|
|
90
98
|
|
91
|
-
|
99
|
+
closesocket(udp);
|
100
|
+
|
101
|
+
WSACleanup();
|
92
102
|
|
93
103
|
}
|
94
104
|
|
95
|
-
コ
|
105
|
+
私はVisual Studioを使用しているため、サイトのコードを少し変更しました。
|
106
|
+
|
107
|
+
sendto(udp, (unsigned char*)data, sizeof data, 0, (struct sockaddr *)&addr, sizeof(addr));のところでエラーが出てしまいます。
|
96
108
|
|
97
109
|
|
98
|
-
|
99
|
-
受信側(Unity)
|
100
|
-
|
101
|
-
UDPReceive.cs
|
102
|
-
|
103
|
-
using System;
|
104
|
-
|
105
|
-
using UnityEngine;
|
106
|
-
|
107
|
-
using System.Collections;
|
108
|
-
|
109
|
-
using System.Net;
|
110
|
-
|
111
|
-
using System.Net.Sockets;
|
112
|
-
|
113
|
-
using System.Threading;
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
public class UDPReceive : MonoBehaviour
|
118
|
-
|
119
|
-
{
|
120
|
-
|
121
|
-
private const int DATA_LENGTH = 4;
|
122
|
-
|
123
|
-
private const int DOUBLE_LENGTH = 8;
|
124
|
-
|
125
|
-
private UdpClient udp;
|
126
|
-
|
127
|
-
private Thread thread;
|
128
|
-
|
129
|
-
private byte[] data;
|
130
|
-
|
131
|
-
private double[] result = new double[DATA_LENGTH];
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
void Start ()
|
136
|
-
|
137
|
-
{
|
138
|
-
|
139
|
-
udp = new UdpClient (20001);
|
140
|
-
|
141
|
-
thread = new Thread (new ThreadStart (threadWork));
|
142
|
-
|
143
|
-
thread.Start ();
|
144
|
-
|
145
|
-
}
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
void Update()
|
150
|
-
|
151
|
-
{
|
152
|
-
|
153
|
-
if (data != null)
|
154
|
-
|
155
|
-
{
|
156
|
-
|
157
|
-
Debug.LogFormat ("{0}, {1}, {2}, {3}", result[0], result[1], result[2], result[3]);
|
158
|
-
|
159
|
-
}
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
void OnApplicationQuit()
|
166
|
-
|
167
|
-
{
|
168
|
-
|
169
|
-
udp.Close ();
|
170
|
-
|
171
|
-
thread.Abort ();
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
void threadWork()
|
178
|
-
|
179
|
-
{
|
180
|
-
|
181
|
-
while(true)
|
182
|
-
|
183
|
-
{
|
184
|
-
|
185
|
-
IPEndPoint remoteEP = null;
|
186
|
-
|
187
|
-
data = udp.Receive(ref remoteEP);
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
for (int i = 0; i<DATA_LENGTH; i++)
|
192
|
-
|
193
|
-
{
|
194
|
-
|
195
|
-
result[i] = BitConverter.ToDouble(data, i * DOUBLE_LENGTH);
|
196
|
-
|
197
|
-
}
|
198
|
-
|
199
|
-
}
|
200
|
-
|
201
|
-
}
|
202
|
-
|
203
|
-
}
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
私はVisual Studioを使用しているため、上記の送信プログラムでは動きませんでした。Winsock2を使うということは調べたのですが、どこを変えればいいかわかりません。
|
208
110
|
|
209
111
|
|
210
112
|
|