Head First C ―頭とからだで覚えるCの基本
(オライリージャパン)
David Griffiths (著) Dawn Griffiths (著)
中田 秀基(監訳)(翻訳) 木下 哲也 (翻訳)
開発環境
- OS X Mavericks - Apple、たまにFreeBSD 10(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang/LLVM (コンパイラ)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の11章(ソケットとネットワーキング: 127.0.0.1という場所はない)、自分で考えてみよう(p.487)を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
- C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、 谷口 功 (翻訳)、 オライリージャパン)
自分で考えてみよう(p.487)
コード(BBEdit, Emacs)
sample487.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <string.h>
#include "catch_signal.h"
#include "sample478.h"
#include "error.h"
int main(int argc, char *argv[])
{
if (catch_signal(SIGINT, handle_shutdown) == -1)
error("プロセスの割り込みハンドラを設定できません。");
int listen_d = open_listener_socket();
bind_to_port(listen_d, 30000);
if (listen(listen_d, 10) == -1)
error("接続待ちできません。");
puts("接続を待っています。");
struct sockaddr_storage client_addr;
unsigned int address_size = sizeof(client_addr);
char buf[255];
size_t s;
while(1) {
int connect_d = accept(listen_d, (struct sockaddr *) &client_addr,
&address_size);
if (connect_d == -1)
error("第2のソケットを開けません。");
if (!fork()) {
close(listen_d);
if (say(connect_d, "Knock! Knock!\r\n") != -1) {
read_in(connect_d, buf, sizeof(buf));
s = 12;
if (strncmp("Who's there?", buf, s) == 0) {
if (say(connect_d, "Oscar\r\n") != -1) {
read_in(connect_d, buf, sizeof(buf));
s = 10;
if (strncmp("Oscar who?", buf, s) == 0)
say(connect_d,
"oscar silly question, you get a silly answer\r\n");
else
say(connect_d, "「Oscar who?」と入力してください。\r\n");
}
} else
say(connect_d, "「Who's there?」と入力してください。\r\n");
}
close(connect_d);
exit (0);
}
close(connect_d);
}
return (0);
}
Makefile
CC=cc CFLAGS=-g -Wall SRC=sample487.c sample478.c error.c catch_signal.c OBJ=sample487.o sample478.o error.o catch_signal.o all: sample487 sample487: $(OBJ) $(CC) $(CFLAGS) $(OBJ) -o sample487 sample487.o: sample478.h error.h sample487.c $(CC) $(CFLAGS) -c sample487.c -o sample487.o sample478.o: error.h sample478.c $(CC) $(CFLAGS) -c sample478.c -o sample478.o error.o: error.c $(CC) $(CFLAGS) -c error.c -o error.o catch_signal.o: catch_signal.h catch_signal.c $(CC) $(CFLAGS) -c catch_signal.c -o catch_signal.o clear: rm -rf sample487 $(OBJ)
入出力結果(Terminal)
サーバー
$ make cc -g -Wall -c sample487.c -o sample487.o cc -g -Wall sample487.o sample478.o error.o catch_signal.o -o sample487 $ ./sample487 接続を待っています。 C-c C-cさようなら! $
同時にやり取り。
クライアント1
$ telnet 127.0.0.1 30000 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Knock! Knock! Who's there? Who's there? Oscar Oscar who? Oscar who? oscar silly question, you get a silly answer Connection closed by foreign host. $
クライアント2
$ telnet 127.0.0.1 30000 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Knock! Knock! Who's there? Who's there? Oscar Oscar who? Oscar who? oscar silly question, you get a silly answer Connection closed by foreign host. $
0 コメント:
コメントを投稿