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 (コンパイラ)
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 <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include "catch_signal.h"
#include "open_listener_socket.h"
#include "bind_to_port.h"
#include "error.h"
#include "read_in.h"
#include "say.h"
int listener_d;
void handle_shutdown(int sig)
{
if (listener_d)
close(listener_d);
printf("さようなら!\n");
exit (0);
}
int main(int argc, char *argv[])
{
catch_signal(SIGINT, handle_shutdown);
listener_d = open_listener_socket();
char buf[255];
bind_to_port(listener_d, 30000);
if (listen(listener_d, 10) == -1)
error("接続待ちできません。", 1);
puts("接続を待っています。");
while(1) {
struct sockaddr_storage client_addr;
unsigned int address_size = sizeof(client_addr);
int connect_d = accept(listener_d, (struct sockaddr *)&client_addr,
&address_size);
if (connect_d == -1)
error("第2のソケットを開けません。", 1);
if (!fork()) {
close(listener_d);
if (say(connect_d, "Knock! Knock!\r\n") != -1) {
read_in(connect_d, buf, sizeof(buf));
if (strncmp("Who's there?", buf, 12) != 0)
say(connect_d, "「Who's there?」と入力してください。\r\n");
else {
if(say(connect_d, "Oscar\r\n") != -1) {
read_in(connect_d, buf, sizeof(buf));
if (strncmp("Oscar who?", buf, 10) != 0)
say(connect_d, "「Oscar who?」と入力してください。\r\n");
else
say(connect_d,
"oscar silly question, you get a silly answer\r\n");
}
}
}
close(connect_d);
exit (0);
}
close(connect_d);
}
}
Makefile
CC=cc CFLAGS = -g -Wall SRC=sample487.c catch_signal.c open_listener_socket.c bind_to_port.c error.c read_in.c say.c OBJ=sample487.o catch_signal.o open_listener_socket.o bind_to_port.o error.o read_in.o say.o all: sample487 sample487: $(OBJ) $(CC) $(CFLAGS) $(OBJ) -o sample487 sample487.o: catch_signal.h open_listener_socket.h bind_to_port.h error.h read_in.h say.h sample487.c $(CC) $(CFLAGS) -c sample487.c -o sample487.o catch_signal.o: catch_signal.h catch_signal.c $(CC) $(CFLAGS) -c catch_signal.c -o catch_signal.o open_listener_socket.o: error.h open_listener_socket.h open_listener_socket.c $(CC) $(CFLAGS) -c open_listener_socket.c -o open_listener_socket.o bind_to_port.o: error.h bind_to_port.h bind_to_port.c $(CC) $(CFLAGS) -c bind_to_port.c -o bind_to_port.o error.o: error.h error.c $(CC) $(CFLAGS) -c error.c -o error.o read_in.o: read_in.h read_in.c $(CC) $(CFLAGS) -c read_in.c -o read_in.o clean: rm -rf sample487 $(OBJ)
サーバー側の端末。
入出力結果(Terminal)
$ make cc -g -Wall -c sample487.c -o sample487.o cc -g -Wall sample487.o catch_signal.o open_listener_socket.o bind_to_port.o error.o read_in.o say.o -o sample487 $ ./sample487 接続を待っています。 C-c C-cさようなら! $
クライアント側の端末1。
入出力結果(Terminal)
$ 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。
入出力結果(Terminal)
$ 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 コメント:
コメントを投稿