zoukankan      html  css  js  c++  java
  • 客户端、服务器代码实例

    服务器:

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <netdb.h>
    #include <errno.h>
    
    #define PORT 2345
    #define MAXSIZE 1024
    
    int main(void)
    {
        int sockfd, newsockfd;
        int sin_size, portnumber;
        char buf[MAXSIZE] = {0};
        struct sockaddr_in server_addr;
        struct sockaddr_in client_addr;
        int client_addr_len = sizeof(struct sockaddr_in);
    
        //creat socket
        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        {
            fprintf(stderr, "creat socket failed
    ");
            exit(EXIT_FAILURE);
        }    
        printf("create socket success!
    ");
        printf("sockfd is %d
    ", sockfd);  
        
        bzero(&server_addr, sizeof(struct sockaddr_in));
        server_addr.sin_family = AF_INET;    //TCP/IP
        server_addr.sin_port = htons(PORT); //socket port
        server_addr.sin_addr.s_addr = htonl(INADDR_ANY);  //inet_addr("0.0.0.0")
        
        //bind sockfd and ip addr and port
        if (bind(sockfd, (struct sockaddr*)(&server_addr), sizeof(struct sockaddr)) < 0)
        {
            fprintf(stderr, "bind failed 
    ");
            exit(EXIT_FAILURE);
        }
        printf("bind success!
    ");
         
         //listen sockfd, passive wait
        if (listen(sockfd, 10) < 0)
        {
            perror("listen fail
    ");
        }
        puts("start listen!
    ");
        
        //accept
        if((newsockfd = accept(sockfd, (struct sockaddr *)(&client_addr), &client_addr_len)) < 0)
        {
            perror("accept error");
            exit(EXIT_FAILURE);
        }    
        printf("accepted a new connection!
    ");
        printf("The new socket id is %d
    ", newsockfd);
        printf("Accept client ip is %s
    ", inet_ntoa(client_addr.sin_addr));
        printf("Connet successful, please client input message
    ");
        
        //send message
        char sendbuf[MAXSIZE] = {0};
        while (1)
        {
            int len = recv(newsockfd, buf, sizeof(buf), 0);
            if (strcmp(buf, "exit
    ") == 0)
                break;
            fputs(buf, stdout);
            send(newsockfd, buf, len, 0);
            memset(sendbuf, 0, sizeof(sendbuf));
            memset(buf, 0 , sizeof(buf));
        }    
    
        close(newsockfd);
        close(sockfd);
        printf("exit success
    ");
        exit(EXIT_SUCCESS);
        return 0;
    }

    客户端:

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <netdb.h>
    #include <errno.h>
    
    #define PORT 2345
    #define MAXSIZE 1024
    
    
    int main(void)
    {
        int sockfd;
        char buffer[1024] = {0};
        struct sockaddr_in server_addr;
        struct hostent *host;
        int nbytes;
        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        {
            fprintf(stderr, "Socket Error is %s
    ", strerror(errno));
            exit(EXIT_FAILURE);
        }
        bzero(&server_addr, sizeof(server_addr));
        server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(PORT);
        server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    
        if(connect(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) < 0)
        {
            fprintf(stderr, "Connect failed
    ");
            exit(EXIT_FAILURE);
        }
        char sendbuf[MAXSIZE] = {0};
        char recvbuf[MAXSIZE] = {0};
        while (1)
        {
            fgets(sendbuf, sizeof(sendbuf), stdin);
            send(sockfd, sendbuf, strlen(sendbuf), 0);
            if (strcmp(sendbuf, "exit
    ") == 0)
                break;    
            recv(sockfd, recvbuf, sizeof(recvbuf), 0);
            fputs(recvbuf, stdout);
            memset(sendbuf, 0, sizeof(sendbuf));
            memset(recvbuf, 0, sizeof(recvbuf));
        }
        close(sockfd);
        exit(EXIT_SUCCESS);
        return 0;
    }
  • 相关阅读:
    2.4学习
    2.3学习
    2.2学习
    2.1学习
    公文流转系统 模拟
    《GCC编译器的使用以及静态库和动态库的制作与使用》
    《驱动调试
    《海思3521D
    《驱动调试
    《驱动调试
  • 原文地址:https://www.cnblogs.com/debruyne/p/9525191.html
Copyright © 2011-2022 走看看