• Socket相关函数(1)- socket(), bind(), listen(), accept(), connect(), TCP模型


    tcp_server.c

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <ctype.h>
    #include <unistd.h>
    
    
    #define SOCK_PORT 49128
    #define MAXLINE 80
    
    int main()
    {
        int sockfd, connfd;    
        struct sockaddr_in servaddr, cliaddr;
        socklen_t cliaddr_len;
        char buf[MAXLINE];
        char str[INET_ADDRSTRLEN];
        int i, n;    
    
        if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
            perror("socket");
            exit(1);
        }
        
        /*Construct sockaddr_in struct*/
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(SOCK_PORT);
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
        if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
            perror("connect");
            exit(1);
        }
    
        if (listen(sockfd, 128) < 0) {
            perror("listen");
            exit(1);
        }
        
        while (1) {
            cliaddr_len = sizeof(cliaddr);
            connfd = accept(sockfd, (struct sockaddr *)&cliaddr, &cliaddr_len);
            n = read(connfd, buf, MAXLINE);
            printf("received from %s at PORT %d
    ", inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)),
                ntohs(cliaddr.sin_port));
            for (i = 0; i < n; i++)
                buf[i] = toupper(buf[i]);
            write(connfd, buf, n);
            close(connfd);
        }
        
        close(sockfd);
        return 0;
    }

    tcp_client.c

    #include <sys/socket.h>
    #include <sys/types.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    
    #define SERV_PORT 49128
    #define MAXLINE 80
    
    int main(int argc, char *argv[])
    {
        
        int sockfd, n;
        struct sockaddr_in servaddr;
        char *str;
        char buf[MAXLINE];
    
        if (argc != 2) {
            fputs("usage: ./client message
    ", stderr);
            exit(1);
        }
        str = argv[1];
    
        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
            perror("socket");
            exit(1);
        }
        
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
        servaddr.sin_port = htons(SERV_PORT);    
        
        connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
        
        write(sockfd, str, strlen(str));
        n = read(sockfd, buf, MAXLINE);
        printf("Response from server:
    ");
        write(STDOUT_FILENO, buf, n);
        close(sockfd);
        return 0;
    }

    运行:

    Client:

    ./client app
    Response from server:
    APP

    Server:

    received from 127.0.0.1 at PORT 48041

  • 相关阅读:
    Git分支合并:Merge、Rebase的选择
    linux系统下MySQL表名区分大小写问题
    Spring Mvc和Spring Boot读取Profile方式
    Git删除远程分支
    TortoiseGit push免输密码
    git [command line] fatal: Authentication failed for
    Jenkins [Error] at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:192)
    FAIL
    WIN下修改host文件并立即生效
    MYSQL 创建数据库SQL
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/8287810.html
Copyright © 2020-2023  润新知