回答編集履歴
3
winsock のコードのコメントの修正
test
CHANGED
@@ -90,7 +90,7 @@
|
|
90
90
|
|
91
91
|
#include <winsock2.h> // WSAStartup, WSACleanup, socket, closesocket, connect
|
92
92
|
|
93
|
-
#include <io.h> //
|
93
|
+
#include <io.h> // read, write
|
94
94
|
|
95
95
|
#include <stdio.h> // fprintf, perror
|
96
96
|
|
2
windows環境の説明を追加
test
CHANGED
@@ -71,3 +71,113 @@
|
|
71
71
|
また、file1.txt の open を実行せずに int fd1 = 0; として標準入力にして、
|
72
72
|
|
73
73
|
キーボードから入力したりしてみてください。
|
74
|
+
|
75
|
+
### 追記
|
76
|
+
|
77
|
+
最初の質問の、参考にした「こちらの記事」は Unix環境での
|
78
|
+
|
79
|
+
ネットワークプログラミングです。
|
80
|
+
|
81
|
+
Windows では、そのままコンパイルできません。
|
82
|
+
|
83
|
+
次のような書き換えが必要でしょう。
|
84
|
+
|
85
|
+
```C
|
86
|
+
|
87
|
+
#pragma comment(lib, "ws2_32.lib")
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
#include <winsock2.h> // WSAStartup, WSACleanup, socket, closesocket, connect
|
92
|
+
|
93
|
+
#include <io.h> // socket
|
94
|
+
|
95
|
+
#include <stdio.h> // fprintf, perror
|
96
|
+
|
97
|
+
#include <stdlib.h> // exit
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
void send_input_data(int sockfd);
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
int main(int argc, char *argv[])
|
106
|
+
|
107
|
+
{
|
108
|
+
|
109
|
+
if (argc != 2) {
|
110
|
+
|
111
|
+
fprintf(stderr, "usage: %s machine-name\n", argv[0]); exit(1);
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
WSADATA wsaData = { 0 };
|
118
|
+
|
119
|
+
WSAStartup(MAKEWORD(2, 0), &wsaData);
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
SOCKET sockfd = socket(PF_INET, SOCK_STREAM, 0);
|
124
|
+
|
125
|
+
printf("sockfd = %x\n", sockfd);
|
126
|
+
|
127
|
+
if (sockfd == INVALID_SOCKET) { perror("client: socket"); exit(1); }
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
struct sockaddr_in client_addr = { 0 }; // bzero は使えない
|
132
|
+
|
133
|
+
client_addr.sin_family = PF_INET;
|
134
|
+
|
135
|
+
client_addr.sin_addr.s_addr = inet_addr(argv[1]);
|
136
|
+
|
137
|
+
client_addr.sin_port = htons(8000);
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
if (connect(sockfd, (struct sockaddr *) &client_addr, sizeof(client_addr)) > 0) {
|
142
|
+
|
143
|
+
perror("client: connect"); closesocket(sockfd); exit(1);
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
send_input_data(sockfd);
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
closesocket(sockfd);
|
154
|
+
|
155
|
+
WSACleanup();
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
void send_input_data(int sockfd)
|
162
|
+
|
163
|
+
{
|
164
|
+
|
165
|
+
char buf[128];
|
166
|
+
|
167
|
+
int buf_len;
|
168
|
+
|
169
|
+
while (1) {
|
170
|
+
|
171
|
+
buf_len = read(0, buf, 1);
|
172
|
+
|
173
|
+
write(sockfd, buf, buf_len);
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
```
|
180
|
+
|
181
|
+
これはサーバマシンにデータを送信するクライアントアプリであり、
|
182
|
+
|
183
|
+
そのデータを受信するサーバアプリが別に必要です。
|
1
fd1, fd2 の表示の追加
test
CHANGED
@@ -29,6 +29,10 @@
|
|
29
29
|
int fd2 = open("file2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
30
30
|
|
31
31
|
if (fd2 < 0) { perror("write open"); return 2; }
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
printf("fd1 = %d, fd2 = %d\n", fd1, fd2);
|
32
36
|
|
33
37
|
|
34
38
|
|