https://koyo.kr/post/c-socket-example/


#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>

int main () {
    int ret;

    pid_t pid = fork();
    /* client */
    if (pid == 0) {
        sleep(1);
        /* create client socket */
        int client_sockfd;
        client_sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (client_sockfd == -1) { perror("[C] socket"); return -1; }
        printf("[C] socket\n");

        /* set client_sockaddr */
        struct sockaddr_in client_sockaddr;
        memset(&client_sockaddr, 0, sizeof(struct sockaddr_in));
        client_sockaddr.sin_family      = AF_INET;
        client_sockaddr.sin_port        = htons(4000);
        inet_pton(AF_INET, "127.0.0.1", &client_sockaddr.sin_addr.s_addr);

        /* connect */
        printf("[C] connect\n");
        ret = connect(client_sockfd, (struct sockaddr *)&client_sockaddr, sizeof(struct sockaddr_in));
        if (ret == -1) { perror("[C] connect"); return -1; }

        /* send */
        printf("[C] send\n");
        int writelen;
        writelen = send(client_sockfd, "hello, world!", strlen("hello, world!"), 0);

        /* recv */
        char buf[512];
        int readlen;
        readlen = recv(client_sockfd, buf, 512, 0);
        if (readlen < 1) { perror("[C] recv"); return -1; }
        printf("[C] recv\n");
        printf("[C] buf \"%s\"\n", buf);

        close(client_sockfd);
    }
    /* server */
    else {
        /* create socket socket */
        int server_sockfd;
        server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (server_sockfd == -1) { perror("[S] socket"); return -1; }
        printf("[S] socket\n");

        /* set server_sockaddr */
        struct sockaddr_in server_sockaddr;
        memset(&server_sockaddr, 0, sizeof(struct sockaddr_in));
        server_sockaddr.sin_family      = AF_INET;
        server_sockaddr.sin_port        = htons(4000);
        server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

        /* bind */
        ret = bind(server_sockfd, (struct sockaddr *)&server_sockaddr, sizeof(struct sockaddr_in));
        if (ret == -1) { perror("[S] bind"); return -1; }
        printf("[S] bind\n");

        /* listen */
        ret = listen(server_sockfd, 5);
        if (ret == -1) { perror("[S] listen"); return -1; }
        printf("[S] listen\n");

        /* loop */
        int client_sockfd;
        struct sockaddr_in client_sockaddr;
        int socklen, readlen, writelen;
        char buf[512];
        /* for (;;) { */
            /* accept */
            memset(&client_sockaddr, 0, sizeof(struct sockaddr_in));
            socklen = sizeof(struct sockaddr_in);
            client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_sockaddr, &socklen);
            if (client_sockfd == -1) { perror("[S] accept"); return -1; }
            printf("[S] accept\n");

            /* recv */
            readlen = recv(client_sockfd, buf, 512, 0);
            if (readlen == -1) { perror("[S] recv"); return -1; }
            printf("[S] recv\n");
            printf("[S] buf \"%s\"\n", buf);

            /* send */
            printf("[S] send\n");
            writelen = send(client_sockfd, buf, readlen, 0);

            /* close socket */
            close(client_sockfd);
        /* } */

        /* close server */
        close(server_sockfd);

        wait(&ret);
    }

    return 0;
}

실행 하면 이렇게 나온다.

$ ./socket
[S] socket
[S] bind
[S] listen
[C] socket
[C] connect
[C] send
[S] accept
[S] recv
[S] buf "hello, world!"
[S] send
[C] recv
[C] buf "hello, world!"
$