質問編集履歴

5

ソースコードの詳細化

2020/10/21 09:58

投稿

veratail
veratail

スコア2

test CHANGED
File without changes
test CHANGED
@@ -38,114 +38,220 @@
38
38
 
39
39
  ```C++
40
40
 
41
+ //構造体、共用体定義抜粋
42
+
41
43
  struct CommandSet{
42
44
 
43
45
  uint8_t command_set[42]
44
46
 
45
47
  }
46
48
 
49
+
50
+
51
+ union ControlSet{
52
+
53
+ AngleSet angle;
54
+
55
+ CommandSet command;
56
+
57
+ };
58
+
59
+
60
+
61
+ struct SocketSet{
62
+
63
+ int sockFromSvr;
64
+
65
+ }
66
+
47
67
  ```
48
68
 
49
69
 
50
70
 
51
71
  ```C++
52
72
 
53
- union ControlSet{
54
-
55
- AngleSet angle;
73
+ SocketSet sock;
74
+
56
-
75
+ void *SvrListner(void *arg) {
76
+
77
+
78
+
79
+  std::unique_ptr<ControlSet> CommandsFromSvr(new ControlSet);
80
+
81
+
82
+
83
+  while (true) {
84
+
85
+ ssize_t rec;
86
+
87
+ do {
88
+
89
+ rec = read(sock.sockFromSvr, &CommandsFromSvr.get()->command.command_set[0], 1);
90
+
91
+  if (rec < 0) {
92
+
57
- CommandSet command;
93
+   throw SysCallError(errno);
58
-
94
+
59
- };
95
+ }
96
+
97
+ } while (rec <= 0);
98
+
99
+
100
+
101
+ switch ((int)CommandsFromSvr.get()->command.command_set[0]) {
102
+
103
+    case 1: {
104
+
105
+ ssize_t rec;
106
+
107
+    do {
108
+
109
+     rec = read(sock.sockFromSvr, &CommandsFromSvr.get()->command.command_set[0], 1);
110
+
111
+
112
+
113
+ if (rec < 0) {
114
+
115
+ throw SysCallError(errno);
116
+
117
+   }
118
+
119
+    } while (rec <= 0);
120
+
121
+
122
+
123
+ size_t len = 0;
124
+
125
+
126
+
127
+ do {
128
+
129
+ ssize_t rec = read(sock.sockFromSvr , &CommandsFromSvr.get()->command.command_set[len + 1], (int)CommandsFromSvr.get()->command.command_set[0] * 2) - len);
130
+
131
+ if (rec < 0) {
132
+
133
+                        throw SysCallError(errno);
134
+
135
+ } else if (rec == 0) {
136
+
137
+                     //エラーメッセージ
138
+
139
+ }
140
+
141
+ len += rec;
142
+
143
+   } while (len < ((int)CommandsFromSvr.get()->command.command_set[0] * 2));
144
+
145
+
146
+
147
+                  break;
148
+
149
+               }
150
+
151
+
152
+
153
+      case 2:{
154
+
155
+                  //case 2 の処理
156
+
157
+                  break;
158
+
159
+               }
160
+
161
+     }
162
+
163
+
164
+
165
+ }
166
+
167
+ return nullptr;
168
+
169
+ }
170
+
171
+
172
+
173
+ int NewTCPSocketConnectingTo(char const *addr, uint16_t port)
174
+
175
+ throw (SysCallError) {
176
+
177
+ struct sockaddr_in server;
178
+
179
+ memset(&server, 0, sizeof server); //struct sockaddr_in serverの初期化
180
+
181
+
182
+
183
+ /*接続先指定用構造体の準備*/
184
+
185
+ server.sin_family = AF_INET;
186
+
187
+ server.sin_port = htons(port);
188
+
189
+
190
+
191
+ inet_pton(AF_INET, addr, &server.sin_addr.s_addr);
192
+
193
+
194
+
195
+ /*サーバに接続*/
196
+
197
+ int sock = socket(AF_INET, SOCK_STREAM, 0); /*ソケットの作成*/
198
+
199
+
200
+
201
+ //sockから負の場合のエラー処理
202
+
203
+ if (sock < 0) {
204
+
205
+ throw SysCallError(errno);
206
+
207
+ }
208
+
209
+
210
+
211
+ // connectの戻り値のチェックとエラー処理
212
+
213
+ int conn = connect(sock, (struct sockaddr *) &server, sizeof(server));
214
+
215
+ if (conn < 0) {
216
+
217
+ throw SysCallError(errno);
218
+
219
+ }
220
+
221
+ return sock;
222
+
223
+
224
+
225
+ }
226
+
227
+
228
+
229
+ int main() {
230
+
231
+ sock.sockFromSvr = NewTCPSocketConnectingTo("***.***.***.***",
232
+
233
+ PortNum);
234
+
235
+
236
+
237
+ Thread threadForSvr(SvrListener, "SvrListener");
238
+
239
+ threadForSvr.Run(nullptr);
240
+
241
+
242
+
243
+ threadForSvr.Join();
244
+
245
+
246
+
247
+ return 0;
248
+
249
+ }
60
250
 
61
251
  ```
62
252
 
63
253
 
64
254
 
65
- ```C++
66
-
67
- (略)
68
-
69
- std::unique_ptr<ControlSet> CommandsFromSvr(new ControlSet);
70
-
71
- (略)
72
-
73
- while (true) {
74
-
75
- ssize_t rec;
76
-
77
- do {
78
-
79
- rec = read(socket, &CommandsFromSvr.get()->command.command_set[0], 1);
80
-
81
- if (rec < 0) {
82
-
83
- //errnoを出力
84
-
85
- }
86
-
87
- } while (rec <= 0);
88
-
89
-
90
-
91
- switch ((int)CommandsFromSvr.get()->command.command_set[0]) {
92
-
93
- case 1: {
94
-
95
- ssize_t rec;
96
-
97
-    do {
98
-
99
-     rec = read(socket, &CommandsFromSvr.get()->command.command_set[0], 1);
100
-
101
-
102
-
103
- if (rec < 0) {
104
-
105
- //errnoを出力
106
-
107
- }
108
-
109
-    } while (rec <= 0);
110
-
111
-
112
-
113
- size_t len = 0;
114
-
115
-
116
-
117
- do {
118
-
119
- ssize_t rec = read(socket , &CommandsFromSvr.get()->command.command_set[len + 1], (int)CommandsFromSvr.get()->command.command_set[0] * 2) - len);
120
-
121
- if (rec < 0) {
122
-
123
-                        //errnoを出力
124
-
125
- } else if (rec == 0) {
126
-
127
-                        //エラーメッセージ
128
-
129
- }
130
-
131
- len += rec;
132
-
133
-   } while (len < ((int)CommandsFromSvr.get()->command.command_set[0] * 2));
134
-
135
-
136
-
137
-                  break;
138
-
139
-                }
140
-
141
- (以下略)
142
-
143
- }
144
-
145
- ```
146
-
147
-
148
-
149
255
  ### 試したこと
150
256
 
151
257
 

4

誤記訂正

2020/10/21 09:58

投稿

veratail
veratail

スコア2

test CHANGED
File without changes
test CHANGED
@@ -50,7 +50,7 @@
50
50
 
51
51
  ```C++
52
52
 
53
- union{
53
+ union ControlSet{
54
54
 
55
55
  AngleSet angle;
56
56
 

3

省略しない書き方に修正

2020/10/21 07:36

投稿

veratail
veratail

スコア2

test CHANGED
File without changes
test CHANGED
@@ -38,9 +38,35 @@
38
38
 
39
39
  ```C++
40
40
 
41
+ struct CommandSet{
42
+
43
+ uint8_t command_set[42]
44
+
45
+ }
46
+
47
+ ```
48
+
49
+
50
+
51
+ ```C++
52
+
53
+ union{
54
+
55
+ AngleSet angle;
56
+
57
+ CommandSet command;
58
+
59
+ };
60
+
61
+ ```
62
+
63
+
64
+
65
+ ```C++
66
+
41
67
  (略)
42
68
 
43
- uint8_t buf[42]; // 10/21 追記 veratail
69
+ std::unique_ptr<ControlSet> CommandsFromSvr(new ControlSet);
44
70
 
45
71
  (略)
46
72
 
@@ -50,7 +76,7 @@
50
76
 
51
77
  do {
52
78
 
53
- rec = read(socket, buf[0], 1);
79
+ rec = read(socket, &CommandsFromSvr.get()->command.command_set[0], 1);
54
80
 
55
81
  if (rec < 0) {
56
82
 
@@ -62,7 +88,7 @@
62
88
 
63
89
 
64
90
 
65
- switch (buf[0]) {
91
+ switch ((int)CommandsFromSvr.get()->command.command_set[0]) {
66
92
 
67
93
  case 1: {
68
94
 
@@ -70,7 +96,7 @@
70
96
 
71
97
     do {
72
98
 
73
-     rec = read(socket, buf[0], 1);
99
+     rec = read(socket, &CommandsFromSvr.get()->command.command_set[0], 1);
74
100
 
75
101
 
76
102
 
@@ -90,7 +116,7 @@
90
116
 
91
117
  do {
92
118
 
93
- ssize_t rec = read(socket , buf[len + 1], buf[0] * 2) - len);
119
+ ssize_t rec = read(socket , &CommandsFromSvr.get()->command.command_set[len + 1], (int)CommandsFromSvr.get()->command.command_set[0] * 2) - len);
94
120
 
95
121
  if (rec < 0) {
96
122
 
@@ -104,7 +130,7 @@
104
130
 
105
131
  len += rec;
106
132
 
107
-   } while (len < (buf[0] * 2));
133
+   } while (len < ((int)CommandsFromSvr.get()->command.command_set[0] * 2));
108
134
 
109
135
 
110
136
 

2

bufの定義を追記しました。

2020/10/21 07:33

投稿

veratail
veratail

スコア2

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,13 @@
38
38
 
39
39
  ```C++
40
40
 
41
+ (略)
42
+
43
+ uint8_t buf[42]; // 10/21 追記 veratail
44
+
45
+ (略)
46
+
41
- while (true) {
47
+ while (true) {
42
48
 
43
49
  ssize_t rec;
44
50
 
@@ -108,7 +114,7 @@
108
114
 
109
115
  (以下略)
110
116
 
111
- }
117
+ }
112
118
 
113
119
  ```
114
120
 

1

タグの追加

2020/10/21 07:17

投稿

veratail
veratail

スコア2

test CHANGED
File without changes
test CHANGED
File without changes