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.480)を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
- C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、 谷口 功 (翻訳)、 オライリージャパン)
長いエクササイズ(p.480)
コード(BBEdit, Emacs)
sample480.c
#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 listen_d;
void handle_shutdown(int sig)
{
if (listen_d)
close(listen_d);
error("さようなら!\n", 0);
}
int main(int argc, char *argv[])
{
catch_signal(SIGINT, handle_shutdown);
listen_d = open_listener_socket();
char buf[255];
bind_to_port(listen_d, 30000);
if (listen(listen_d, 10) == -1)
error("接続待ちできません。", 1);
while(1) {
struct sockaddr_storage client_addr;
unsigned int address_size = sizeof(client_addr);
int connct_d = accept(listen_d, (struct sockaddr *)&client_addr,
&address_size);
if (connct_d == -1)
error("第2のソケットを開けません。", 1);
if (say(connct_d, "Knock! Knock!\r\n") != -1) {
read_in(connct_d, buf, sizeof(buf));
if (strcmp("Who's there?", buf) != 0)
say(connct_d, "「Who's there?」と入力してください。\r\n");
else {
if(say(connct_d, "Oscar\r\n") != -1) {
read_in(connct_d, buf, sizeof(buf));
if (strcmp("Oscar who?", buf) != 0)
say(connct_d, "「Oscar who?」と入力してください。\r\n");
else
say(connct_d, "oscar silly question, you get a silly answer\r\n");
}
}
}
close(connct_d);
}
}
Makefile
CC=cc CFLAGS = -g -Wall SRC=sample480.c catch_signal.c open_listener_socket.c bind_to_port.c error.c read_in.c say.c OBJ=sample480.o catch_signal.o open_listener_socket.o bind_to_port.o error.o read_in.o say.o all: sample480 sample480: $(OBJ) $(CC) $(CFLAGS) $(OBJ) -o sample473 sample480.o: catch_signal.h open_listener_socket.h bind_to_port.h error.h read_in.h say.h $(CC) $(CFLAGS) -c sample480.c -o sample480.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 sample480 $(OBJ)
サーバー側の端末。
入出力結果(Terminal)
$ ./sample480 接続を待っています C-c C-cさようなら! $
クライアント側の端末。
入出力結果(Terminal)
$ telnet 127.0.0.1 30000 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Knock! Knock! Who's there? Oscar Oscar who? oscar silly question, you get a silly answer Connection closed by foreign host. $ telnet 127.0.0.1 30000 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Knock! Knock! Hi 「Who's there?」と入力してください Connection closed by foreign host. $ telnet 127.0.0.1 30000 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Knock! Knock! Who's there? Oscar Oscar? 「Oscar who?」と入力してください Connection closed by foreign host. $
0 コメント:
コメントを投稿