ネットワークプログラミング

Contents

プロセス内通信

プロセス間通信ではない。sockpair(2) を始めて使ったのでメモ。 select で寝たスレッドを起こすのに使える。:

#include <stdio.h>
#include <string.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>

int
main()
{
        int             error;
        int             sv[2];

        char *          msg = "Hello, world!!";
        char            buf[128];

        if ((error = socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) < 0) {
                perror("socketpair");
                exit(1);
        }

        if ((error = write(sv[0], msg, strlen(msg) + 1)) < 0) {
                perror("write");
                exit(1);
        }

        if ((error = read(sv[1], buf, sizeof(buf))) < 0) {
                perror("read");
                exit(1);
        }

        printf("%s\n", buf);
}

Littel Endian、Big Endian、Netowrk Byte Order

Littel Endian と Big Endian

ネットワークでお仕事しながら、いっつも忘れるこのはなし。 "1" は、:

               Byte 1   Byte 2   Byte 3   Byte 4
--------------------------------------------------
Little Endian: 00000001 00000000 00000000 00000000
Big Endian:    00000000 00000000 00000000 00000001
--------------------------------------------------

Network Byte Order

これは Big Endian。以下は TCP のヘッダ。Sequence Number が 1 の ときには、:

0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Source Port          |       Destination Port        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                        Sequence Number                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Acknowledgment Number                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Data |           |U|A|P|R|S|F|                               |
| Offset| Reserved  |R|C|S|S|Y|I|            Window             |
|       |           |G|K|H|T|N|N|                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|           Checksum            |         Urgent Pointer        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options                    |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                             data                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

となるので忘れないように。メモメモ。