書籍「Linuxシステムコール」(塚越 一雄 (著))のサンプルコードを掲載します。
./ch_03
./ch_03/lsc03030101.c
./ch_03/lsc03030102.c
./ch_03/lsc03040201.c
./ch_03/lsc03040401.c
./ch_03/lsc03050101.c
./ch_04
./ch_04/lsc04020801.c
./ch_04/lsc04021001.c
./ch_04/lsc04030301.c
./ch_04/lsc04040101.c
./ch_04/lsc04040201.c
./ch_05
./ch_05/lsc05070201.c
./ch_05/lsc05070601.c
./ch_05/lsc05080101.c
./ch_05/lsc05090301.c
./ch_05/lsc05090401.c
./ch_06
./ch_06/lsc06030301.c
./ch_06/lsc06040901.c
./ch_06/lsc06070501.c
./ch_06/lsc06080501.c
./ch_06/lsc06100301.c
./ch_07
./ch_07/lsc07030201.c
./ch_07/lsc07060201.c
./ch_08
./ch_08/lsc08030301_rec.c
./ch_08/lsc08030401_sed.c
./ch_09
./ch_09/lsc09070101_sed.c
./ch_09/lsc09090101_clt.c
./ch_09/lsc09100801_sed.c
./ch_09/lsc09100901_clt.c
./ch_09/lsc09101101_sed.c
./ch_09/lsc09110301_sed.c
----------------------------------------
./ch_03/lsc03030101.c
----------------------------------------
#include
#include
int main()
{
int i;
printf("example startn");
printf("example PID = %dn", getpid());
printf("fork startn");
fork();
printf("fork endn");
for (i = 0; i < 4; i++){
printf("%02d: PID = %d running...n", i, getpid());
sleep(1);
}
return 0;
}
----------------------------------------
./ch_03/lsc03030102.c
----------------------------------------
#include
#include
int main()
{
pid_t pid;
printf("example PID = %dn", getpid());
printf("fork startn");
if ((pid = fork()) == -1){
perror("fork");
return 1;
}
else if (pid > 0) {
printf("parent: PID = %dn", getpid());
printf("my child PID = %dn", pid);
return 0;
}
else {
printf("child: PID = %dn", getpid());
return 0;
}
}
----------------------------------------
./ch_03/lsc03040201.c
----------------------------------------
#include
#include
int main()
{
int i = 0;
pid_t pid;
printf("fork startn");
if ((pid = fork()) == -1){
perror("fork");
return 1;
}
else if (pid > 0) {
printf("parent: waitting ....n");
wait(NULL);
printf("parent : my child just finishedn");
}
else {
for (i = 0; i < 4; i++) {
printf("%02d: child running....n", i);
sleep(1);
}
}
return 0;
}
----------------------------------------
./ch_03/lsc03040401.c
----------------------------------------
#include
#include /* for wait macro */
#include
int main()
{
int i = 0;
pid_t pid;
int status;
printf("fork startn");
if ((pid = fork()) == -1){
perror("fork");
return 1;
}
else if (pid > 0) {
printf("parent: waitting ....n");
wait(&status);
if (WIFEXITED(status)) {
printf("parent : my child just finishedn");
printf("return status = %dn",WEXITSTATUS(status));
}
else if (WIFSIGNALED(status))
printf("my child received signal %dn", WTERMSIG(status));
}
else {
for (i = 0; i < 4; i++) {
printf("%02d: child running....n", i);
sleep(1);
}
return 123;
}
return 0;
}
----------------------------------------
./ch_03/lsc03050101.c
----------------------------------------
#include
int main()
{
pid_t pid;
printf("example start.....n");
printf("PID = %dn", getpid());
if ((pid = fork()) == -1){
perror("fork");
return 1;
}
else if (pid > 0) {
printf("parent: waitting ....n");
wait(NULL);
printf("child process just finishedn");
}
else {
printf("child process start.....n");
printf("PID = %dn", getpid());
printf("Hit Enter key ...");
getchar();
printf("child : execlp start ...n");
if (execlp("bc", "bc", NULL) == -1) {
perror("execlp");
return 1;
}
return 0;
}
return 0;
}
----------------------------------------
./ch_04/lsc04020801.c
----------------------------------------
#include /* for sleep() */
#include
#include
/* シグナルハンドラ */
void sig_int(int sig)
{
printf("signal %d receivedn", sig);
}
int main()
{
int i = 0;
signal(SIGINT ,sig_int);
for (;; i++){
printf("%02d: prog running ...n", i);
sleep(1);
}
return 0;
}
----------------------------------------
./ch_04/lsc04021001.c
----------------------------------------
#include /* for sleep() */
#include
#include
/* シグナルハンドラ */
void sig_int(int sig)
{
printf("signal %d receivedn", sig);
signal(SIGINT ,sig_int);
}
int main()
{
int i = 0;
signal(SIGINT ,sig_int);
for (;; i++){
printf("%02d: prog running ...n", i);
sleep(1);
}
return 0;
}
----------------------------------------
./ch_04/lsc04030301.c
----------------------------------------
/* 指定秒数後にベルを鳴らす */
#include
#include
#include
#include /* atoi */
/* シグナルSIGALRMに対するシグナルハンドラ */
void sig_alarm(int sig)
{
printf("signal %d receivedan", sig);
}
int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "usage: %s time(sec)n",
basename(argv[0]));
return 1;
}
if (signal(SIGALRM ,sig_alarm) == SIG_ERR) {
perror("signal");
return 1;
}
alarm(atoi(argv[1]));
printf("pause : startn");
pause();
printf("pause : endn");
return 0;
}
----------------------------------------
./ch_04/lsc04040101.c
----------------------------------------
#include
#include
#include
int main()
{
pid_t pid;
if ((pid = fork()) == -1){
perror("fork");
return 1;
}
else if (pid > 0) {/* 親プロセス */
printf("Hit Enter for kill the child processn");
getchar();
if (kill(pid, SIGKILL) == -1) {
perror("kill");
return 1;
}
printf("my child process just killedn");
}
else/* 子プロセス */
for (;;) {
printf("child process running...n");
sleep(1);
}
return 0;
}
----------------------------------------
./ch_04/lsc04040201.c
----------------------------------------
#include
#include
#include
#include /* atoi */
int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "usage: example pidn");
return 1;
}
if (kill(atoi(argv[1]), SIGKILL) == -1) {
perror("kill");
return 1;
}
return 0;
}
----------------------------------------
./ch_05/lsc05070201.c
----------------------------------------
#include
#include
#include
int main(int argc, char *argv[])
{
int fi, fo;
ssize_t len;
char c;
if (argc < 3) {
fprintf(stderr, "usage: %s file(from) file(to)n",
basename(argv[0]));
return 1;
}
if ((fi = open(argv[1], O_RDONLY)) == -1) {
perror("open for read");
return 0;
}
if ((fo = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1) {
perror("open for write");
close(fi);
return 1;
}
/* whileを抜けるとき
正常なら:len == 0
readエラー:len == -1
writeエラー:len == -2*/
while ((len = read(fi, &c, 1)) > 0)
if (write(fo, &c, 1) == -1) {
len = -2;
break;
}
close(fi);
if (len < 0) {
close(fo);
if (len == -1)
perror("read");
else if (len == -2)
perror("write");
return 1;
}
/* 正常に書き込めたときだけclose(fo)のエラーをチェック */
if (close(fo) == -1){
perror("close");
return 1;
}
else
return 0;
}
----------------------------------------
./ch_05/lsc05070601.c
----------------------------------------
#include
#include
#include
int main(int argc, char *argv[])
{
int fi, fo;
ssize_t len;
char buff[BUFSIZ];
if (argc < 3) {
fprintf(stderr, "usage: %s file(from) file(to)n",
basename(argv[0]));
return 1;
}
if ((fi = open(argv[1], O_RDONLY)) == -1) {
perror("open for read");
return 0;
}
if ((fo = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1) {
perror("open for write");
close(fi);
return 1;
}
/* whileを抜けるとき
正常なら:len == 0
readエラー:len == -1
writeエラー:len == -2*/
while ((len = read(fi, buff, BUFSIZ)) > 0)
if (write(fo, buff, len) == -1) {
len = -2;
break;
}
close(fi);
if (len < 0) {
close(fo);
if (len == -1)
perror("read");
else if (len == -2)
perror("write");
return 1;
}
/* 正常に書き込めたときだけclose(fo)のエラーをチェック */
if (close(fo) == -1){
perror("close");
return 1;
}
else
return 0;
}
----------------------------------------
./ch_05/lsc05080101.c
----------------------------------------
#include
#include
#include
int main(int argc, char *argv[])
{
ssize_t len;
char buff[BUFSIZ];
/* whileを抜けるとき
正常なら:len == 0
readエラー:len == -1
writeエラー:len == -2*/
while ((len = read(0, buff, BUFSIZ)) > 0)
if (write(1, buff, len) == -1) {
perror("write");
return 1;
}
if (len < 0) {
perror("read");
return 1;
}
else
return 0;
}
----------------------------------------
./ch_05/lsc05090301.c
----------------------------------------
#include
#include
#include
int main(int argc, char *argv[])
{
ssize_t len;
char buff[BUFSIZ];
int fd;
if (argc > 1) {
/* ファイル名が指定された */
if ((fd = open(argv[1], O_RDONLY)) == -1) {
perror("open");
return 1;
}
/* 指定されたファイルを標準入力にリダイレクト */
close(0);
if (dup(fd) == -1) {
perror("dup");
return 1;
}
close(fd);
}
/* 後は普通に標準入力を標準出力へコピーするだけ */
while ((len = read(0, buff, BUFSIZ)) > 0)
if (write(1, buff, len) == -1) {
perror("write");
return 1;
}
if (len < 0) {
perror("read");
return 1;
}
else
return 0;
}
----------------------------------------
./ch_05/lsc05090401.c
----------------------------------------
#include
#include
#include
int main(int argc, char *argv[])
{
ssize_t len;
char buff[BUFSIZ];
int fd;
if (argc > 1) {
/* ファイル名が指定された */
if ((fd = open(argv[1], O_RDONLY)) == -1) {
perror("open");
return 1;
}
/* 指定されたファイルを標準入力にリダイレクト */
if (dup2(fd, 0) == -1) {
perror("dup");
return 1;
}
if (close(fd) == -1) {
perror("close");
return 1;
};
}
/* 後は普通に標準入力を標準出力へコピーするだけ */
while ((len = read(0, buff, BUFSIZ)) > 0)
if (write(1, buff, len) == -1) {
perror("write");
return 1;
}
if (len < 0) {
perror("read");
return 1;
}
else
return 0;
}
----------------------------------------
./ch_06/lsc06030301.c
----------------------------------------
#include
#include /* HP-UX */
#include
/* ------------------------------
ioctl isatty
------------------------------
success01
failure -10
------------------------------ */
int my_isatty(int fd)
{
struct termio tty;
return ioctl(fd, TCGETA, &tty) ? 0 : 1;
}
int main()
{
char buff[BUFSIZ];
if (my_isatty(0) == 0 || my_isatty(1) == 0) {
fprintf(stderr, "端末で実行してくださいn");
return 1;
}
printf("あなたの名前は?");
fgets(buff, BUFSIZ, stdin);
/* 入力したnを消す */
buff[strlen(buff) -1] = '0';
printf("こんにちわ, %s 様 n", buff);
return 0;
}
----------------------------------------
./ch_06/lsc06040901.c
----------------------------------------
#include
#include /* HP-UX */
#include
int main()
{
char buff[BUFSIZ];
struct termio tty, tty_save;
ioctl(0, TCGETA, &tty);/* 端末の状態を保存する */
tty_save = tty;
tty.c_lflag &= ~ECHO;/* エコーを止める */
tty.c_lflag |= ECHONL;/* 改行だけはエコーする */
ioctl(0, TCSETAF, &tty);
printf("パスワード ?");
fgets(buff, BUFSIZ, stdin);
printf("パスワード = %s", buff);
ioctl(0, TCSETAF, &tty_save);
return 0;
}
----------------------------------------
./ch_06/lsc06070501.c
----------------------------------------
#include
#include /* HP-UX */
#include
int main()
{
char c;
struct termio tty, tty_save;
/* 端末の状態を保存する */
ioctl(0, TCGETA, &tty);
tty_save = tty;
tty.c_lflag &= ~ICANON;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
ioctl(0, TCSETAF, &tty);
printf("1文字入力 ?");
fflush(stdout);
read(0, &c, 1);
printf("n 入力文字 = %cn", c);
ioctl(0, TCSETAF, &tty_save);
return 0;
}
----------------------------------------
./ch_06/lsc06080501.c
----------------------------------------
#include
#include /* HP-UX */
#include
int main()
{
char c, n, result;
struct termio tty, tty_save;
printf("終了は Ctrl-Xn");
/* 端末の状態を保存する */
ioctl(0, TCGETA, &tty);
tty_save = tty;
/* 端末の設定 */
tty.c_lflag &= ~(ECHO | ICANON);
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = 1;
ioctl(0, TCSETAF, &tty);
for (;;)
if ((n = read(0, &c, 1)) == 0)
printf("入力文字なし n");
else if(n == -1) {
perror("read");
result = 1;
break;
}
else if (c == 'x18')/* C-x */ {
result = 0;
break;
}
else
printf("%cn", c);
ioctl(0, TCSETAF, &tty_save);
return result;
}
----------------------------------------
./ch_06/lsc06100301.c
----------------------------------------
#include
#include /* HP-UX */
#include
int main()
{
char c, n, result;
struct termio tty, tty_save;
printf("終了は Ctrl-Xn");
/* 端末の状態を保存する */
ioctl(0, TCGETA, &tty);
tty_save = tty;
/* 端末の設定 */
tty.c_lflag &= ~(ECHO | ICANON | ECHONL);
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
tty.c_iflag &= ~ICRNL;/* r → nの変換をしない */
tty.c_oflag &= ~ONLCR;/* n → rの変換をしない */
tty.c_lflag &= ~ISIG;/* シグナルの割り込みキーを無視する */
tty.c_iflag &= ~IXOFF;/* C-s、C-qの制御(入力) */
tty.c_iflag &= ~IXON;/* C-s、C-qの制御(出力) */
ioctl(0, TCSETAF, &tty);
for (;;)
if (read(0, &c, 1) == -1) {
perror("read");
result = 1;
break;
}
else if (c == 'x18')/* C-x */ {
result = 0;
break;
}
else
printf("入力文字 = %02x : %crn", c, c);
ioctl(0, TCSETAF, &tty_save);
return result;
}
----------------------------------------
./ch_07/lsc07030201.c
----------------------------------------
#include
#include
int main()
{
char buff[BUFSIZ];
int pp[2];
pid_t pid;
char *msg = "Linux Programmer's Manual!!!!";
/* 親プロセスがパイプをオープンする */
if (pipe(pp) == -1) {
perror("pipe");
return 1;
}
/* 子プロセスを作る */
if ((pid = fork()) == -1){
perror("fork");
return 1;
}
/* 親プロセス : この例では送信側になる */
else if (pid > 0) {
/* 読み出し用のファイルディスクリプタは使わない */
close(pp[0]);
if (write(pp[1], msg, strlen(msg) + 1) == -1) {
perror("write");
close(pp[1]);
return 1;
}
printf("parent: message to pipe : %sn", msg);
close(pp[1]); /* 送信に成功 */
}
/* 子プロセス : この例では受信側になる */
else {
/* 書き込み用のファイルディスクリプタは使わない */
close(pp[1]);
if (read(pp[0], buff, BUFSIZ) == -1) {
perror("read");
close(pp[0]);
return 1;
}
printf("child: message from pipe : %sn", buff);
close(pp[0]); /* 受信に成功 */
}
return 0;
}
----------------------------------------
./ch_07/lsc07060201.c
----------------------------------------
/* lsコマンドの出力をlessに接続する */
#include
#include
int main()
{
int pp[2];
pid_t pid;
if (pipe(pp) == -1) {
perror("pipe");
return 1;
}
if ((pid = fork()) == -1){
perror("fork");
return 1;
}
/* 親プロセス */
else if (pid > 0) {
/* 標準入力をパイプへ */
close(0);
dup(pp[0]);
close(pp[0]);
close(pp[1]);
if (execlp("more", "more", "-d", NULL) == -1) {
perror("execlp");
return 1;
}
}
/* 子プロセス */
else {
/* 標準出力をパイプへ */
close(1);
dup(pp[1]);
close(pp[1]);
close(pp[0]);
if (execlp("ls", "ls", "-al", NULL) == -1) {
perror("execlp");
return 1;
}
}
return 0;
}
----------------------------------------
./ch_08/lsc08030301_rec.c
----------------------------------------
/* lsc08030301_rec.c --名前つきパイプを読む */
#include
#include /* S_IFIFO */
#include /* for O_RDONLY */
#include
#define FIFO "/usr/tmp/FIFO"
int main()
{
int ff, nbyte;
char buff[BUFSIZ];
/* FIFOがなければ作成する */
if (access(FIFO, F_OK) == -1)
if (mknod(FIFO, 0666 | S_IFIFO, (dev_t) 0) == -1) {
perror("mknod");
return 1;
}
printf("receiver: データを待っていますn");
/* 読み込みモードでオープン
相手がオープンするまでブロックされる */
if ((ff = open(FIFO, O_RDONLY)) == -1){
perror("open");
unlink(FIFO);
return 1;
}
/* データがなくなるまでFIFOを読む */
while ((nbyte = read(ff, buff, BUFSIZ)) > 0)
write(1, buff, nbyte);
close(ff);
unlink(FIFO);
if (nbyte == -1) {
perror("write");
return 1;
}
return 0;
}
----------------------------------------
./ch_08/lsc08030401_sed.c
----------------------------------------
/* lsc08030401_sed.c --データの送信用 */
#include
#include /* for open flags */
#include
#define FIFO "/usr/tmp/FIFO"
int main()
{
int ff, nbyte;
char buff[BUFSIZ];
/* 書き込みモードでオープン */
if ((ff = open(FIFO, O_WRONLY)) == -1){
perror("open");
return 1;
}
/* 標準入力からデータをファイル末まで送信 */
while ((nbyte = read(0, buff, BUFSIZ)) > 0)
if ((write(ff, buff, nbyte)) !=nbyte) {
perror("write");
close(ff);
return 1;
}
if (close(ff) == -1) {
perror("close");
return 1;
}
return 0;
}
----------------------------------------
./ch_09/lsc09070101_sed.c
----------------------------------------
/* lsc09070101_sed.c --商品番号から商品名を返す */
#include
#include
#include
#include /* struct sockaddr_un */
#include
#include /* strcpy, strlen */
#include /* atoi */
#define SOCKET_NAME "server_socket"
static char *lang[] = {
"C",
"Perl",
"Tcl/Tk"
};
int main()
{
int fd_socket, fd_accept, index;
int len;
struct sockaddr_un addr;
char buff[BUFSIZ];
if ((fd_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("server: socket");
return 1;
}
/* 使用するャPットと同じ名前のファイルが存在する場合は削除
同じ名前が残っているとbind実行時にエラーになる */
if (!access(SOCKET_NAME, F_OK))
unlink(SOCKET_NAME);
/* addrのメンバーにデータを設定 */
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, SOCKET_NAME);
if (bind(fd_socket, (struct sockaddr *) &addr,
sizeof(addr)) == -1) {
perror("server:bind");
return 1;
}
if (listen(fd_socket, 5) == -1) {
perror("server:listen");
return 1;
}
len = sizeof(addr);
if ((fd_accept = accept(fd_socket,
(struct sockaddr *) &addr, &len)) == -1) {
perror("server:accept");
return 1;
}
if (read(fd_accept, buff, BUFSIZ) == -1) {
perror("server:read");
return 1;
}
index = atoi(buff);
if (index == 0 || index > 3)
strcpy(buff, "無効なインデックスです");
else
strcpy(buff, lang[--index]);
if (write(fd_accept, buff, strlen(buff) + 1) == -1) {
perror("server:write");
return 1;
}
close(fd_accept);
close(fd_socket);
unlink(SOCKET_NAME);
return 0;
}
----------------------------------------
./ch_09/lsc09090101_clt.c
----------------------------------------
/* lsc09090101_clt.c --サーバーに商品名を問い合わせる */
#include
#include
#include
#include /* struct sockaddr_un */
#include
#include /* strcpy, strlen */
#define SOCKET_NAME "server_socket"
int main(int argc, char *argv[])
{
int fd_socket;
struct sockaddr_un addr;
char buff[BUFSIZ];
if (argc < 2) {
fprintf(stderr, "usage: %s indexn",
basename(argv[0]));
return 1;
}
if ((fd_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("client: socket");
return 1;
}
/* addrのメンバーにデータを設定 */
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, SOCKET_NAME);
if (connect(fd_socket, (struct sockaddr *) &addr,
sizeof(addr)) == -1) {
perror("client:connect");
return 1;
}
if (write(fd_socket, argv[1], strlen(argv[1]) + 1) == -1) {
perror("client:write");
return 1;
}
if (read(fd_socket, buff, BUFSIZ) == -1) {
perror("client:read");
return 1;
}
printf("data from server = %sn", buff);
close(fd_socket);
return 0;
}
----------------------------------------
./ch_09/lsc09100801_sed.c
----------------------------------------
/* lsc09100801_sed.c --INET版 */
#include
#include
#include
#include /* struct sockaddr_in */
#include /* inet_addr */
#include
#include /* strcpy, strlen */
#include /* atoi */
#define PORT 3000
static char *lang[] = {
"C",
"Perl",
"Tcl/Tk"
};
int main()
{
int fd_socket, fd_accept, index;
int len;
struct sockaddr_in addr;
char buff[BUFSIZ];
if ((fd_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("server: socket");
return 1;
}
/* addrのメンバーにデータを設定 */
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
if (bind(fd_socket, (struct sockaddr *) &addr,
sizeof(addr)) == -1) {
perror("server:bind");
return 1;
}
if (listen(fd_socket, 5) == -1) {
perror("server:listen");
return 1;
}
len = sizeof(addr);
if ((fd_accept = accept(fd_socket,
(struct sockaddr *) &addr, &len)) == -1) {
perror("server:accept");
return 1;
}
if (read(fd_accept, buff, BUFSIZ) == -1) {
perror("server:read");
return 1;
}
index = atoi(buff);
if (index == 0 || index > 3)
strcpy(buff, "無効なインデックスです");
else
strcpy(buff, lang[--index]);
if (write(fd_accept, buff, strlen(buff) + 1) == -1) {
perror("server:write");
return 1;
}
close(fd_accept);
close(fd_socket);
return 0;
}
----------------------------------------
./ch_09/lsc09100901_clt.c
----------------------------------------
/* lsc09090101_clt.c --サーバーに商品名を問い合わせる */
#include
#include
#include
#include /* struct sockaddr_in */
#include /* inet_addr */
#include
#include /* strcpy, strlen */
#define PORT 3000
int main(int argc, char *argv[])
{
int fd_socket;
struct sockaddr_in addr;
char buff[BUFSIZ];
if (argc < 2) {
fprintf(stderr, "usage: %s indexn",
basename(argv[0]));
return 1;
}
if ((fd_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("client: socket");
return 1;
}
/* addrのメンバーにデータを設定 */
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(PORT);
if (connect(fd_socket, (struct sockaddr *) &addr,
sizeof(addr)) == -1) {
perror("client:connect");
return 1;
}
if (write(fd_socket, argv[1], strlen(argv[1]) + 1) == -1) {
perror("client:write");
return 1;
}
if (read(fd_socket, buff, BUFSIZ) == -1) {
perror("client:read");
return 1;
}
printf("data from server = %sn", buff);
close(fd_socket);
return 0;
}
----------------------------------------
./ch_09/lsc09101101_sed.c
----------------------------------------
/* lsc09101101_sed.c --INET版 */
#include
#include
#include
#include /* struct sockaddr_in */
#include /* inet_addr */
#include
#include /* strcpy, strlen */
#include /* atoi */
#define PORT 3000
static char *lang[] = {
"C",
"Perl",
"Tcl/Tk"
};
int main()
{
int fd_socket, fd_accept, index;
int len;
struct sockaddr_in addr;
char buff[BUFSIZ];
if ((fd_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("server: socket");
return 1;
}
index = 1;
setsockopt(fd_socket, SOL_SOCKET, SO_REUSEADDR, &index,
sizeof(index));
/* addrのメンバーにデータを設定 */
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
if (bind(fd_socket, (struct sockaddr *) &addr,
sizeof(addr)) == -1) {
perror("server:bind");
return 1;
}
if (listen(fd_socket, 5) == -1) {
perror("server:listen");
return 1;
}
len = sizeof(addr);
if ((fd_accept = accept(fd_socket,
(struct sockaddr *) &addr, &len)) == -1) {
perror("server:accept");
return 1;
}
if (read(fd_accept, buff, BUFSIZ) == -1) {
perror("server:read");
return 1;
}
index = atoi(buff);
if (index == 0 || index > 3)
strcpy(buff, "無効なインデックスです");
else
strcpy(buff, lang[--index]);
if (write(fd_accept, buff, strlen(buff) + 1) == -1) {
perror("server:write");
return 1;
}
close(fd_accept);
close(fd_socket);
return 0;
}
----------------------------------------
./ch_09/lsc09110301_sed.c
----------------------------------------
/* lsc09110301_sed.c --INET版 */
#include
#include
#include
#include /* struct sockaddr_in */
#include /* inet_addr */
#include
#include /* strcpy, strlen */
#include /* atoi */
#define PORT 3000
static char *lang[] = {
"C",
"Perl",
"Tcl/Tk"
};
int main()
{
int fd_socket, fd_accept, index;
int len;
struct sockaddr_in addr;
char buff[BUFSIZ];
if ((fd_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("server: socket");
return 1;
}
index = 1;
setsockopt(fd_socket, SOL_SOCKET, SO_REUSEADDR, &index,
sizeof(index));
/* addrのメンバーにデータを設定 */
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
if (bind(fd_socket, (struct sockaddr *) &addr,
sizeof(addr)) == -1) {
perror("server:bind");
return 1;
}
if (listen(fd_socket, 5) == -1) {
perror("server:listen");
return 1;
}
len = sizeof(addr);
for(;;) {
if ((fd_accept = accept(fd_socket,
(struct sockaddr *) &addr, &len)) == -1) {
perror("server:accept");
return 1;
}
if (read(fd_accept, buff, BUFSIZ) == -1) {
perror("server:read");
return 1;
}
index = atoi(buff);
if (index == 9)
strcpy(buff, "server exit");
else if (index == 0 || index > 3)
strcpy(buff, "無効なインデックスです");
else
strcpy(buff, lang[--index]);
if (write(fd_accept, buff, strlen(buff) + 1) == -1) {
perror("server:write");
return 1;
}
close(fd_accept);
if (index == 9)
break;
}
close(fd_socket);
return 0;
}
0 件のコメント:
コメントを投稿