天涼好個秋.

第几个 Hello World

2268 阅 0 评 IT手札

Hello World 情怀

每个人初学代码时肯定都写过 Hello World, 每一次接触一门新的技术&编程语言,都会用新语言写一个Hello World 程序,最近在看《Beej's Guide to Network Programming》,突发奇想又写了一个 Hello World

code

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

#define PORT "5964"
#define BACKLOG 10
char hello_world[] = "HTTP/1.1 200 OK\r\nServer: MarIxs\r\nContent-length: 13\r\nContent-Type: text/html\r\n\r\nHello, World!!";

void sigchld_handler(int s)
{
    while(waitpid(-1, NULL, WNOHANG) > 0);//等效于 wait
}

void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET){
        return &(((struct sockaddr_in*)sa)->sin_addr);
    }
    
    return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main(void)
{
    int sockfd, new_fd;
    struct addrinfo hints, *servinfo,*p;
    struct sockaddr_storage their_addr;
    socklen_t sin_size;
    struct sigaction sa;
    int yes=1;
    char s[INET6_ADDRSTRLEN];
    int rv;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;

    if((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    for(p = servinfo; p != NULL; p = servinfo->ai_next){
        if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
            perror("server:port");
            continue;
        }
        if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }
        if(bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("server:bind");
            continue;
        }
        break;

    }

    if (p == NULL) {
        fprintf(stderr, "server:failed to bind\n");
        return 2;
    }

    freeaddrinfo(servinfo);

    if (listen(sockfd, BACKLOG) == -1) {
        perror("server:listen");
        exit(1);
    }

    sa.sa_handler = sigchld_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;

    if (sigaction(SIGCHLD, &sa, NULL) == -1) {
        perror("sigaction");
        exit(1);
    }

    printf("server:waiting for connections... \n");

    while(1) {
        sin_size = sizeof their_addr;
        if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size))  == -1) {
            perror("server:accept\n");
            continue;
        }

        inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s);
        printf("server: got connection from %s\n",s);

        if (!fork()) {
        
            close(sockfd);

            if (send(new_fd, hello_world, strlen(hello_world) + 1, 0) == -1)
                perror("send");
            close(new_fd);
            exit(0);
        }
        close(new_fd);

    }

    return 0;

}

验证

root@NL:~# curl http://127.0.0.1:5964 
Hello, World!

在浏览器里打开 ip:5964 就会显示 Hello, World!

中间加上逗号: Hello, world! 才是正确的写法哦。

EOF