Head First C ―頭とからだで覚えるCの基本
(オライリージャパン)
David Griffiths (著) Dawn Griffiths (著)
中田 秀基(監訳)(翻訳) 木下 哲也 (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の11章(ソケットとネットワーキング)、コードマグネット(p.495)を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
- C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、 谷口 功 (翻訳)、 オライリージャパン)
コードマグネット(p.495)
コード(BBEdit, Emacs)
sample480.c
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include "sample494include.h"
int main(int argc, char *argv[])
{
int d_sock;
d_sock = open_socket("en.wikipedia.org", "80");
char buf[255];
sprintf(buf, "GET /wiki/%s http/1.1\r\n", argv[1]);
say(d_sock, buf);
say(d_sock, "Host: en.wikipedia.org\r\n\r\n");
char rec[256];
int byteRcvd = recv(d_sock, rec, 245, 0);
while (byteRcvd) {
if (byteRcvd == -1) {
error("サーバから読み込めません");
}
rec[byteRcvd] = '\0';
printf("%s", rec);
byteRcvd = recv(d_sock, rec, 255, 0);
}
close(d_sock);
return 0;
}
Makefile
all: sample494 sample494: sample494include.o sample494.c cc -g -o sample494 sample494.c sample494include.o sample494include.o: sample494include.c sample494include.h cc -c sample494include.c clean: rm sample494
入出力結果(Terminal)
$ make && ./sample494 O\'Reilly cc -g -o sample494 sample494.c sample494include.o HTTP/1.1 200 OK Server: Apache X-Powered-By: PHP/5.3.10-1ubuntu3.9+wmf1 X-Content-Type-Options: nosniff Content-language: en Vary: Accept-Encoding,Cookie X-Vary-Options: Accept-Encoding;list-contains=gzip,Cookie;string-contains=enwikiToken;string-contains=enwikiLoggedOut;string-contains=forceHTTPS;string-contains=enwikiSession;string-contains=centralauth_Token;string-contains=centralauth_Session;string-contains=centralauth_LoggedOut;string-contains=mf_useformat;string-contains=stopMobileRedirect Expires: Thu, 01 Jan 1970 00:00:00 GMT Last-Modified: Mon, 17 Feb 2014 11:44:43 GMT Content-Type: text/html; charset=UTF-8 X-Varnish: 1286745747, 1286721918, 1568661418 Via: 1.1 varnish, 1.1 varnish, 1.1 varnish Transfer-Encoding: chunked Date: Mon, 03 Mar 2014 05:34:55 GMT Age: 0 Connection: keep-alive X-Cache: cp1067 miss (0), cp4008 miss (0), cp4016 frontend miss (0) Cache-Control: private, s-maxage=0, max-age=0, must-revalidate 008000 <!DOCTYPE html> <html lang="en" dir="ltr" class="client-nojs"> … </html> 0 $
0 コメント:
コメントを投稿