zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Html 表单表格 form table
    JavaWeb -- 服务器传递给Servlet的对象 -- ServletConfig, ServletContext,Request, Response
    JavaWeb -- Servlet运行过程 和 细节
    调用DLL中的过程和函数
    调用DLL中的过程和函数
    动态载入 DLL
    动态载入 DLL
    静态载入 DLL
    DLL的加载和调用
    静态载入 DLL
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/8287810.html
Copyright © 2011-2022 走看看