質問編集履歴
4
完成版の更新方法を誤っていました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -138,6 +138,10 @@
|
|
138
138
|
|
139
139
|
}
|
140
140
|
|
141
|
+
|
142
|
+
|
143
|
+
```
|
144
|
+
|
141
145
|
|
142
146
|
|
143
147
|
|
3
お二人にご指摘いただいた点をもとに修正し、無事に動かすことができましたので更新します。
test
CHANGED
File without changes
|
test
CHANGED
@@ -28,7 +28,7 @@
|
|
28
28
|
|
29
29
|
#include <stdlib.h>
|
30
30
|
|
31
|
-
#include
|
31
|
+
#include <string.h>
|
32
32
|
|
33
33
|
#include <sys/types.h>
|
34
34
|
|
@@ -38,242 +38,238 @@
|
|
38
38
|
|
39
39
|
#include <errno.h>
|
40
40
|
|
41
|
-
|
41
|
+
|
42
42
|
|
43
43
|
#define KEY 2020
|
44
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
|
-
e
|
118
|
-
|
119
|
-
printf("
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
45
|
+
|
46
|
+
|
47
|
+
typedef struct{
|
48
|
+
|
49
|
+
long mtype;
|
50
|
+
|
51
|
+
int send_size;
|
52
|
+
|
53
|
+
char image[1024];
|
54
|
+
|
55
|
+
}DATA;
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
int main()
|
60
|
+
|
61
|
+
{
|
62
|
+
|
63
|
+
DATA data;
|
64
|
+
|
65
|
+
data.mtype=1;
|
66
|
+
|
67
|
+
data.send_size=0;
|
68
|
+
|
69
|
+
char mtext[256];
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
int msgid;
|
74
|
+
|
75
|
+
FILE *fp;
|
76
|
+
|
77
|
+
extern int errno;
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
if((msgid=msgget(KEY,0666|IPC_CREAT))==-1){
|
84
|
+
|
85
|
+
printf("MsgGet Error=%d\n",errno);
|
86
|
+
|
87
|
+
exit(EXIT_FAILURE);
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
if((fp=fopen("gazou.png","rb"))==NULL){
|
94
|
+
|
95
|
+
printf("ファイルが開けませんでした");
|
96
|
+
|
97
|
+
exit(EXIT_FAILURE);
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
while(feof(fp)==0){
|
108
|
+
|
109
|
+
data.send_size=fread(&data.image,1,1024,fp);
|
110
|
+
|
111
|
+
printf("%d\n",data.send_size);
|
112
|
+
|
113
|
+
printf("%ld\n",sizeof(data)-sizeof(long));
|
114
|
+
|
115
|
+
printf("msgid:%d\n",msgid);
|
116
|
+
|
117
|
+
if(msgsnd(msgid,&data,data.send_size+sizeof(int),0)==-1){
|
118
|
+
|
119
|
+
printf("errno=%d\n",errno);
|
120
|
+
|
121
|
+
perror(mtext);
|
122
|
+
|
123
|
+
fputs(mtext,stdout);
|
124
|
+
|
125
|
+
exit(EXIT_FAILURE);
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
fclose(fp);
|
134
|
+
|
135
|
+
exit(EXIT_FAILURE);
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
```C言語(メッセージ受信側)
|
146
|
+
|
147
|
+
#include <stdio.h>
|
148
|
+
|
149
|
+
#include <string.h>
|
150
|
+
|
151
|
+
#include <stdlib.h>
|
152
|
+
|
153
|
+
#include <sys/types.h>
|
154
|
+
|
155
|
+
#include <sys/ipc.h>
|
156
|
+
|
157
|
+
#include <sys/msg.h>
|
158
|
+
|
159
|
+
#include <errno.h>
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
#define KEY 2020
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
typedef struct{
|
168
|
+
|
169
|
+
long mtype;
|
170
|
+
|
171
|
+
int send_size;
|
172
|
+
|
173
|
+
char image[1024];
|
174
|
+
|
175
|
+
}DATA;
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
int main(int argc,char* argv[])
|
182
|
+
|
183
|
+
{
|
184
|
+
|
185
|
+
int status=EXIT_FAILURE;
|
186
|
+
|
187
|
+
int msgid;
|
188
|
+
|
189
|
+
extern int errno;
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
DATA data;
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
FILE *fp;
|
198
|
+
|
199
|
+
char mmtext[256];
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
data.send_size=0;
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
if((fp=fopen("gazou2.png","wb"))==NULL){
|
212
|
+
|
213
|
+
printf("ファイルが開けませんでした");
|
214
|
+
|
215
|
+
exit(EXIT_FAILURE);
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
if((msgid=msgget(KEY,0666|IPC_CREAT))==-1){
|
224
|
+
|
225
|
+
printf("MsgGet Error=%d\n",errno);
|
226
|
+
|
227
|
+
exit(status);
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
while(1){
|
236
|
+
|
237
|
+
if(msgrcv(msgid,&data,sizeof(data)-sizeof(long),0,0)==-1){
|
238
|
+
|
239
|
+
printf("recieve error=%d\n",errno);
|
240
|
+
|
241
|
+
perror(mmtext);
|
242
|
+
|
243
|
+
exit(EXIT_FAILURE);
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
fwrite(&data.image,1,data.send_size,fp);
|
248
|
+
|
249
|
+
printf("data.send_size=%d\n",data.send_size);
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
if(data.send_size<1024){
|
254
|
+
|
255
|
+
break;
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
printf("書込み終了\n");
|
262
|
+
|
263
|
+
fclose(fp);
|
264
|
+
|
265
|
+
exit(EXIT_SUCCESS);
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
|
134
270
|
|
135
271
|
```
|
136
272
|
|
137
|
-
```C言語(メッセージ受信側)
|
138
|
-
|
139
|
-
#include <stdio.h>
|
140
|
-
|
141
|
-
#include <string.h>
|
142
|
-
|
143
|
-
#include <stdlib.h>
|
144
|
-
|
145
|
-
#include <sys/types.h>
|
146
|
-
|
147
|
-
#include <sys/ipc.h>
|
148
|
-
|
149
|
-
#include <sys/msg.h>
|
150
|
-
|
151
|
-
#include <errno.h>
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
#define KEY 2020
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
int main(int argc,char* argv[])
|
160
|
-
|
161
|
-
{
|
162
|
-
|
163
|
-
FILE *fp=NULL;
|
164
|
-
|
165
|
-
int msgid;
|
166
|
-
|
167
|
-
int no;
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
typedef struct{
|
172
|
-
|
173
|
-
long mtype;
|
174
|
-
|
175
|
-
int send_size;
|
176
|
-
|
177
|
-
char data[1024];
|
178
|
-
|
179
|
-
} DATA;
|
180
|
-
|
181
|
-
DATA data;
|
182
|
-
|
183
|
-
int status=EXIT_FAILURE;
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
struct msgbuff{
|
190
|
-
|
191
|
-
long mtype;
|
192
|
-
|
193
|
-
char mtext[80];
|
194
|
-
|
195
|
-
}b;
|
196
|
-
|
197
|
-
extern int errno;
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
data.send_size=0;
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
if((fp=fopen("gazou","wb"))==NULL){
|
206
|
-
|
207
|
-
printf("ファイルが開けませんでした");
|
208
|
-
|
209
|
-
exit(EXIT_FAILURE);
|
210
|
-
|
211
|
-
}
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
if(argc<2){
|
216
|
-
|
217
|
-
printf("Argument required.\n");
|
218
|
-
|
219
|
-
exit(status);
|
220
|
-
|
221
|
-
}
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
printf("Recieve %d Start.\n",no=atoi(argv[1]));
|
226
|
-
|
227
|
-
if((msgid=msgget(KEY,0666|IPC_CREAT))==-1){
|
228
|
-
|
229
|
-
printf("MsgGet Error=%d\n",errno);
|
230
|
-
|
231
|
-
exit(status);
|
232
|
-
|
233
|
-
}
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
while(1){
|
240
|
-
|
241
|
-
if(msgrcv(msgid,&data,sizeof(data)-sizeof(long),0,0)==-1){
|
242
|
-
|
243
|
-
data.send_size=fwrite(&data,1,data.send_size,fp);
|
244
|
-
|
245
|
-
if(errno==ENOMSG){
|
246
|
-
|
247
|
-
printf("Receive %d End. \n",no);
|
248
|
-
|
249
|
-
status=EXIT_SUCCESS;
|
250
|
-
|
251
|
-
}else{
|
252
|
-
|
253
|
-
printf("MsgRcv Error=%d\n",errno);
|
254
|
-
|
255
|
-
status=EXIT_FAILURE;
|
256
|
-
|
257
|
-
}
|
258
|
-
|
259
|
-
break;
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
printf("Receive%2d:",no);
|
264
|
-
|
265
|
-
fputs(b.mtext,stdout);
|
266
|
-
|
267
|
-
}
|
268
|
-
|
269
|
-
exit(status);
|
270
|
-
|
271
|
-
}
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
```
|
276
|
-
|
277
273
|
|
278
274
|
|
279
275
|
Linuxのubuntu上で開発しています。
|
2
SHOMI様の追記をもとに編集してみました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -236,13 +236,13 @@
|
|
236
236
|
|
237
237
|
|
238
238
|
|
239
|
-
while(
|
239
|
+
while(1){
|
240
240
|
|
241
241
|
if(msgrcv(msgid,&data,sizeof(data)-sizeof(long),0,0)==-1){
|
242
242
|
|
243
|
-
data.send_size=fwrite(&data,1,
|
243
|
+
data.send_size=fwrite(&data,1,data.send_size,fp);
|
244
|
-
|
244
|
+
|
245
|
-
if(errno=ENOMSG){
|
245
|
+
if(errno==ENOMSG){
|
246
246
|
|
247
247
|
printf("Receive %d End. \n",no);
|
248
248
|
|
1
ご指摘いただいた点のコードを修正してみました。ただ、送信側を実行するとMsgsndError=22と表示されます。システムコールだと思いますが調べても対処法が見つけられずわかっていないです。
test
CHANGED
File without changes
|
test
CHANGED
@@ -68,9 +68,15 @@
|
|
68
68
|
|
69
69
|
DATA data;
|
70
70
|
|
71
|
-
mtype=0;
|
71
|
+
data.mtype=0;
|
72
|
-
|
72
|
+
|
73
|
-
|
73
|
+
data.send_size=0;
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
74
80
|
|
75
81
|
|
76
82
|
|
@@ -96,9 +102,9 @@
|
|
96
102
|
|
97
103
|
while(feof(fp)==0){
|
98
104
|
|
99
|
-
send_size=fread(&data,1,1024,fp);
|
105
|
+
data.send_size=fread(&data.data,1,1024,fp);
|
100
|
-
|
106
|
+
|
101
|
-
if(msgsnd(msgid,&data,send_size+sizeof(
|
107
|
+
if(msgsnd(msgid,&data,data.send_size+sizeof(int),0)==-1){
|
102
108
|
|
103
109
|
printf("MsgSnd Error=%d\n",errno);
|
104
110
|
|
@@ -190,6 +196,10 @@
|
|
190
196
|
|
191
197
|
extern int errno;
|
192
198
|
|
199
|
+
|
200
|
+
|
201
|
+
data.send_size=0;
|
202
|
+
|
193
203
|
|
194
204
|
|
195
205
|
if((fp=fopen("gazou","wb"))==NULL){
|
@@ -226,11 +236,11 @@
|
|
226
236
|
|
227
237
|
|
228
238
|
|
229
|
-
while(send_size<1024){
|
239
|
+
while(data.send_size<=1024){
|
230
|
-
|
231
|
-
|
240
|
+
|
232
|
-
|
233
|
-
if(msgrcv(msgid,&data,sizeof(data)-sizeof(long),0,0)==-1){
|
241
|
+
if(msgrcv(msgid,&data,sizeof(data)-sizeof(long),0,0)==-1){
|
242
|
+
|
243
|
+
data.send_size=fwrite(&data,1,1024,fp);
|
234
244
|
|
235
245
|
if(errno=ENOMSG){
|
236
246
|
|