-
Notifications
You must be signed in to change notification settings - Fork 0
ソケット間通信
subaru edited this page Mar 23, 2024
·
12 revisions
【C言語】ソケット通信について解説
【C言語】簡単な「HTTPサーバー」を作る
Blocking
#include <iostream>
#include <unistd.h>
#include <cstring>
int main() {
int pipefd[2];
pipe(pipefd);
// デフォルトはブロッキングモード
pid_t pid = fork();
if (pid == 0) {
sleep(3);
const char *message = "Hello from child!";
write(pipefd[1], message, strlen(message) + 1);
close(pipefd[0]);
close(pipefd[1]);
exit(0);
} else {
char buffer[1024];
ssize_t bytesRead;
std::cout << "Waiting for data..." << std::endl;
bytesRead = read(pipefd[0], buffer, sizeof(buffer));
if (bytesRead > 0) {
std::cout << "Received: " << buffer << std::endl;
} else {
std::cerr << "Read error" << std::endl;
}
close(pipefd[0]);
close(pipefd[1]);
}
return 0;
}
non-Blocking
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <cerrno>
int main() {
int pipefd[2];
pipe(pipefd);
// ノンブロッキングの設定
int flags = fcntl(pipefd[0], F_GETFL, 0);
fcntl(pipefd[0], F_SETFL, flags | O_NONBLOCK);
pid_t pid = fork();
if (pid == 0) {
sleep(3);
const char *message = "Hello from child!";
write(pipefd[1], message, strlen(message) + 1);
close(pipefd[0]);
close(pipefd[1]);
exit(0);
} else {
char buffer[1024];
ssize_t bytesRead;
while (true) {
bytesRead = read(pipefd[0], buffer, sizeof(buffer));
if (bytesRead > 0) {
std::cout << "Received: " << buffer << std::endl;
break;
} else if (bytesRead == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
std::cout << "No data yet. Trying again in 1 second..." << std::endl;
sleep(1);
} else {
break;
}
}
close(pipefd[0]);
close(pipefd[1]);
}
return 0;
}
サイト
【C言語】ソケット通信について解説
【C言語】簡単な「HTTPサーバー」を作る
非ブロッキング入出力および select()
書籍
ネットワーク全体の動きがわかる
→ ネットワークはなぜ繋がるのか