阅读背景:

这些IP地址来自哪里?

来源:互联网 
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>


int main(int argc, char *argv[])
{
    //set up hints

    struct addrinfo hints;
    struct addrinfo *res;

    struct sockaddr their_addr;
    socklen_t sin_size = sizeof their_addr;
    char ipaddr[INET6_ADDRSTRLEN];

    int sockfd, new_fd;

    int sent;


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

    getaddrinfo(NULL, argv[1], &hints, &res);

    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

    //bind a port to listen on

    bind(sockfd, res->ai_addr, res->ai_addrlen);
    listen(sockfd, 10);

    new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
    inet_ntop(AF_INET, 
                  their_addr.sa_data,
                  ipaddr, sizeof ipaddr);

    printf("Connection recieved from %s", ipaddr);

    sent = send(new_fd, "Hello, world!", 13, 0);
}
#include <stdio.h>
#include <string.h>
#include



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: