• C++ programming: Linux server socket example


    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #define TRUE   1
    #define PORT 51500
    
    int main(int argc, char *argv[]) {
        int master_sock,
                addrlen,
                new_sock,
                maximum_clients = 30,
                client_sock[maximum_clients],
                act,
                i,
                value_read,
                sock_descriptor,
                maximum_socket_descriptor;
        struct sockaddr_in adr{};
    
        char buff[1024];  //data buffer of 1K
        fd_set read_fds; //set of socket file descriptors
        char *message = "ECHO Daemon v1.0 \r\n"; //connect notice message
    
        //initialise all client_sock to 0
        for (i = 0; i < maximum_clients; i++) {
            client_sock[i] = 0;
        }
    
        //creating a master socket
        if ((master_sock = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
            perror("Failed_Socket");
            exit(EXIT_FAILURE);
        }
    
        //These are the types of sockets that we have created
        adr.sin_family = AF_INET;//PROTOCOL = IP
        adr.sin_addr.s_addr = INADDR_ANY;//SOURCE ADDRESS = 0.0.0.0/0
        adr.sin_port = htons(PORT);//LISTENING PORT = PORT
    
        //bind the socket to localhost port
        if (bind(master_sock, (struct sockaddr *) &adr, sizeof(adr)) < 0) {
            perror("Failed_Bind");
            exit(EXIT_FAILURE);
        }
    
        printf("Port having listener:  %d \n", PORT);
    
        //Specify 3 as maximum pending connections for master socket
        if (listen(master_sock, 3) < 0) {
            perror("listen");
            exit(EXIT_FAILURE);
        }
    
        //Accepting the Incoming Connection
        addrlen = sizeof(adr);
        puts("Looking For Connections");
    
        //*******************************//
        // Here we start using select functions and the macros for multiple client handling
    
        while (TRUE) {
            //Clearing the socket set
            FD_ZERO(&read_fds);
    
            //Adding the master socket to the set
            FD_SET(master_sock, &read_fds);
            maximum_socket_descriptor = master_sock;
    
            //Adding child sockets to set
            for (i = 0; i < maximum_clients; i++) {
                //Descriptor for Socket
                sock_descriptor = client_sock[i];
    
                //if the socket descriptor is valid then adding it to the read list
                if (sock_descriptor > 0) {
                    FD_SET(sock_descriptor, &read_fds);
                }
    
                //Highest File Descriptor Number which is needed for the select function
                if (sock_descriptor > maximum_socket_descriptor) {
                    maximum_socket_descriptor = sock_descriptor;
                }
            }
    
            //Waiting for something to happen on the master socket. As the wait time is NULL the wait is indefinite
            act = select(maximum_socket_descriptor + 1, &read_fds, nullptr, nullptr, nullptr);
            if ((act < 0) && (errno != EINTR)) {
                printf("Failed_Select");
            }
    
            //Any activity on the master socket is treated as an incoming connection
            if (FD_ISSET(master_sock, &read_fds)) {
                if ((new_sock = accept(master_sock, (struct sockaddr *) &adr, (socklen_t *) &addrlen)) < 0) {
                    perror("Accept!");
                    exit(EXIT_FAILURE);
                }
    
                //Informing the user of the socket number which will be sued to send and receive messages
                printf("This is a New Connection,The socket file descriptor is %d and the IP is : %s on Port : %d\n",
                       new_sock,
                       inet_ntoa(adr.sin_addr),
                       ntohs(adr.sin_port));
    
                // Sending Greeting Message on New Connection
                if (send(new_sock, message, strlen(message), 0) != strlen(message)) {
                    perror("Send!!");
                }
                puts("Welcome Text Sent Affirmative.");
    
                // Adding new socket to the array of sockets
                for (i = 0; i < maximum_clients; i++) {
                    // Checking if the position is empty
                    if (client_sock[i] == 0) {
                        client_sock[i] = new_sock;
                        printf("Adding new socket to the list of sockets as %d\n", i);
                        break;
                    }
                }
            }
    
            //If not the master socket then it is some i/o activity on some other socket
            for (i = 0; i < maximum_clients; i++) {
    
                sock_descriptor = client_sock[i];
                if (FD_ISSET(sock_descriptor, &read_fds)) {
    
                    //Checking if the activity was for closing and reading the incoming message
                    if ((value_read = read(sock_descriptor, buff, 1024)) == 0) {
    
                        //If someone disconnected, getting their details and printing a message
                        getpeername(sock_descriptor, (struct sockaddr *) &adr, (socklen_t *) &addrlen);
    
                        printf("Disconnected Host. Their , IP %s and PORT %d \n",
                               inet_ntoa(adr.sin_addr), ntohs(adr.sin_port));
    
                        //Closing the socket and marking it as 0 in the list to be reused
                        close(sock_descriptor);
                        client_sock[i] = 0;
                    } else {
                        //Setting the string terminating NULL byte on the end of the data that is read
                        buff[value_read] = '\0';
                        //Echoing back the message that came in the socket
                        send(sock_descriptor, buff, strlen(buff), 0);
                    }
                }
            }
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
  • 相关阅读:
    PHP 个人愿望众筹网站系统mysql数据库web结构apache计算机软件工程网页wamp
    以树莓集团的视角:探索AI技术如何重塑数字媒体产业发展
    详细介绍R语言在数据分析中的应用
    SOLIDWORKS功能布局实用技巧之保存实体技术
    python矩阵最近邻填充替换
    AI人工智能进阶-BERT/Transformer/LSTM/RNN原理与代码
    磷脂-聚乙二醇-叠氮,DSPE-PEG-Azide,DSPE-PEG-N3,MW:5000
    C# 程序运行无法加载 C++ DLL“xxx.dll”: 找不到指定的模块 Exception from HRESULT: 0x8007007E
    进程控制,父子进程
    Spring注解驱动之后再说事务
  • 原文地址:https://blog.csdn.net/crazy_rays/article/details/132619829