linux c例子 - 网络编程(1) - tcp

服务端

#include <stdio.h>          // printf perror
#include <sys/socket.h>     // socket 

#include <netinet/in.h>
#include <arpa/inet.h>

static void help()
{
    printf("./a.out port\n");
}

static void print_sockaddr(struct sockaddr_in *addr)
{
    in_port_t       port;
    char            buf[INET_ADDRSTRLEN], *ip;
    
    port = ntohs(addr->sin_port);
    
    if (inet_ntop(AF_INET, &addr->sin_addr, buf, INET_ADDRSTRLEN) == NULL) {
        perror("inet_ntop");
    }
    
    printf("%s:%d - connect\n", buf, port);
    
}

int main(int argc, char **argv)
{
    int                 n, rc, fd, afd, flag;
    unsigned short      port;
    char                buf[16];
    socklen_t           slen;
    
    struct sockaddr_in  ser_addr, cli_addr;
    
    if (argc != 2) {
        help();
        return -1;
    }
    
    port = atoi(argv[1]);
    
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd == -1) {
        perror("socket");
        return -1;
    }
    
    ser_addr.sin_family = AF_INET;
    ser_addr.sin_port = htons(port);
    ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    // 解决强制退出时,bind 报 Address already in use
    flag = 1;
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int));
    
    rc = bind(fd, (struct sockaddr *)&ser_addr, sizeof(ser_addr));
    if (rc == -1) {
        perror("bind");
        return -1;
    }
    
    rc = listen(fd, 1024);
    if (rc == -1) {
        perror("listen");
        return -1;
    }
    
    slen = sizeof(cli_addr);
    
    while (1) {
    
        afd = accept(fd, (struct sockaddr *)&cli_addr, &slen);
        if (afd == -1) {
            perror("socket");
            return -1;
        }
        
        print_sockaddr(&cli_addr);
        
        n = recv(afd, buf, 15, 0);
        if (n <= 0) {
            perror("recv");
            close(afd);
            continue;
        }
        
        buf[n] = 0;
        
        printf("recv: %s\n", buf);
        
        n = send(afd, "got", 3, 0);
        if (n <= 0) {
            perror("send");
            close(afd);
            continue;
        }
        
        close(afd);
        
    
    }
    
}

客户端

#include <stdio.h>          // printf perror
#include <sys/socket.h>     // socket 

#include <netinet/in.h>
#include <arpa/inet.h>

static void help()
{
    printf("./a.out ip port\n");
}

int main(int argc, char **argv)
{
    int             fd, n;
    in_port_t       port;
    char            *ip;
    char            buf[16];
    
    struct sockaddr_in      ser_addr;
    
    if (argc != 3) {
        help();
        return -1;
    }
    
    ip = argv[1];
    port = atoi(argv[2]);
    
    if (strcmp(ip, "localhost") == 0) {
        ip = "127.0.0.1";
    }
    
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd == -1) {
        perror("socket");
        return -1;
    }
    
    ser_addr.sin_family = AF_INET;
    ser_addr.sin_port = htons(port);
    ser_addr.sin_addr.s_addr = inet_addr(ip);
    
    if (connect(fd, (struct sockaddr *)&ser_addr, sizeof(ser_addr)) == -1) {
        perror("connect");
        return -1;
    }
    
    n = send(fd, "hello", 5, 0);
    if (n <= 0) {
        perror("send");
        return -1;
    }
    
    n = recv(fd, buf, 15, 0);
    if (n <= 0) {
        perror("recv");
        return -1;
    }
    
    buf[n] = 0;
    
    printf("recv: %s\n", buf);
    
    close(fd);
    
    return 0;
}


上一篇: 无
下一篇: linux c例子 - 网络编程(2) - udp
作者邮箱: 203328517@qq.com