socket クライアント

daytimeに接続.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>

int main()
{
    struct addrinfo info, *res;
    int sock;
    FILE *f;
    char buf[1024];
    int err;

    // ゼロリセット
    memset(&info, 0, sizeof(struct addrinfo));
    // ipv4, ipv6
    info.ai_family = AF_UNSPEC;
    // tcp
    info.ai_socktype = SOCK_STREAM;
    if ((err = getaddrinfo("localhost", "daytime" &info, &res)) != 0) {
        fprintf(stderr, "getaddrinfo(3): %s\n", gai_strerror(err));
        exit(1);
    }

    sock = socket(res->ai_family, res->ai_socktype, 0);
    if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
        close(sock);
    }

    f = fdopen(sock, "r");
    if (!f) {
        perror("fdopen(3)");
        exit(1);
    }
    fgets(buf, sizeof buf, f);
    fclose(f);
    fputs(buf, stdout);
    exit(0);

    return 0;
}
05 APR 2009 21:28:04 JST

一応straceとかしてみた.

% strace -e trace=open,read,write,close,socket,connect ./a.out
open("/etc/ld.so.cache", O_RDONLY)      = 3
close(3)                                = 0
open("/lib/libc.so.6", O_RDONLY)        = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0000_\1\0004\0\0\0"..., 512) = 512
close(3)                                = 0
open("/etc/nsswitch.conf", O_RDONLY)    = 3
read(3, "#\n# /etc/nsswitch.conf\n#\n# An ex"..., 4096) = 1696
read(3, "", 4096)                       = 0
close(3)                                = 0
open("/etc/ld.so.cache", O_RDONLY)      = 3
close(3)                                = 0
open("/lib/libnss_files.so.2", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\300\30\0\0004\0\0\0"..., 512) = 512
close(3)                                = 0
open("/etc/services", O_RDONLY)         = 3
read(3, "# /etc/services:\n# $Id: services"..., 4096) = 4096
close(3)                                = 0
socket(PF_FILE, SOCK_STREAM, 0)         = 3
connect(3, {sa_family=AF_FILE, path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
close(3)                                = 0
socket(PF_FILE, SOCK_STREAM, 0)         = 3
connect(3, {sa_family=AF_FILE, path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
close(3)                                = 0
open("/etc/resolv.conf", O_RDONLY)      = 3
read(3, "; generated by /sbin/dhclient-sc"..., 4096) = 81
read(3, "", 4096)                       = 0
close(3)                                = 0
open("/etc/hosts", O_RDONLY)            = 3
read(3, "# Do not remove the following li"..., 4096) = 246
read(3, "", 4096)                       = 0
close(3)                                = 0
open("/etc/hosts", O_RDONLY)            = 3
read(3, "# Do not remove the following li"..., 4096) = 246
close(3)                                = 0
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
connect(3, {sa_family=AF_INET, sin_port=htons(13), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
read(3, "05 APR 2009 21:28:30 JST\r\n", 4096) = 26
close(3)                                = 0
write(1, "05 APR 2009 21:28:30 JST\r\n", 2605 APR 2009 21:28:30 JST
) = 26


こうやって動き見るのは面白いな.ようやくシステムコールとかどうしてんのかうらでなにしてんのかとか少しわかってきた.
ふつうのLinuxプログラミングはわかりやすいし勉強になりまする..