0

I want to create a server - client socket in the simplest way possible with socket programming and test keep-alive. I want to use keep-alive on the client side, but keep-alive does not work even though it takes the parameters I gave.But it works fine on the server side. According to the parameters I gave, if the internet connection is broken, it closes the socket connection. I have to design the server side not to send any reply so the server side does not send a reply.

this is server code which is not contain keepalive settings;

#include<unistd.h>
#include<stdio.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<string.h>
#include<arpa/inet.h>   
#include<iostream>

int main(int argc, char const *argv[])
{
    int server_fd, new_socket, valread;
    struct sockaddr_in address;
    int opt = 1;
    int addrlen = sizeof(address);
    char buffer[1024] = {0};

    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
    {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }
    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
    {
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("192.168.4.28");
    address.sin_port = htons( 6000 );

    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0)
    {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }
    if (listen(server_fd, 3) < 0)
    {
        perror("listen");
        exit(EXIT_FAILURE);
    }
    if ((new_socket = accept(server_fd, (struct sockaddr *)&address,(socklen_t*)&addrlen))<0)
    {
        perror("accept");
        exit(EXIT_FAILURE);
    }

    while(valread = read(new_socket , buffer, 1024) > 0 && new_socket > 0)
    {
        std::cout << "valread : " << valread << std::endl;
        std::cout << "new_socket : " << new_socket << std::endl;
        printf("%s\n",buffer );
        buffer[1024] = {0};
    }

    close(server_fd);
    close(new_socket);

    return 0;
}

this is client code which is contain keepalive settings;

#include<stdio.h>
#include<string.h>  
#include<sys/socket.h>
#include<arpa/inet.h>   
#include<unistd.h>
#include<iostream>
#include<netinet/in.h>
#include<netinet/tcp.h>

int main(int argc , char *argv[])
{
    int socket_desc;
    struct sockaddr_in server;
    char *message;
    int flags = 1;

    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }

    // Checked all the setsockopt with getsockopt.I didn't write it here to avoid code clutter.

    flags =1;
    if (setsockopt(socket_desc, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags))) { perror("ERROR: setsocketopt(), SO_KEEPALIVE"); exit(0); };

    flags = 10;
    if (setsockopt(socket_desc, SOL_TCP, TCP_KEEPIDLE, (void *)&flags, sizeof(flags))) { perror("ERROR: setsocketopt(), SO_KEEPIDLE"); exit(0); };

    flags = 2;
    if (setsockopt(socket_desc, SOL_TCP, TCP_KEEPCNT, (void *)&flags, sizeof(flags))) { perror("ERROR: setsocketopt(), SO_KEEPCNT"); exit(0); };

    flags = 2;
    if (setsockopt(socket_desc, SOL_TCP, TCP_KEEPINTVL, (void *)&flags, sizeof(flags))) { perror("ERROR: setsocketopt(), SO_KEEPINTVL"); exit(0); };

    server.sin_addr.s_addr = inet_addr("192.168.4.28");
    server.sin_family = AF_INET;
    server.sin_port = htons( 6000 );

    if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("connect error");
        return 1;
    }

    puts("Connected\n");
    message = "Test";

    for(int i = 1; i < 10000; i++)
    {
        sleep(1);
        //Send some data
        if( send(socket_desc , message , strlen(message) , 0) < 0)
        {
            puts("Send failed");
        }
        std::cout << ("%d",i) << ". test data send" << std::endl;
    }
    close(socket_desc);
    return 0;
}

As I said above, if I use the keepalive set function on the server side, it works without any problems.

  • You should trade one of your tags for the `C++` tag. (Needs a language tag) – ryyker Sep 27 '21 at 12:33
  • TCP keepalive is an option that is not necessarily included in a TCP implementation on a host OS. – Ron Maupin Sep 27 '21 at 13:27
  • Please [edit] your question and add more details. How exactly do you test that keepalive works or doesn't work? Describe what you do, what output/behavior you get and what you expect. See also https://stackoverflow.com/q/5435098/10622916 The line `buffer[1024] = {0};` in the `while(...read...)` loop accesses one byte past the end of the array. It is not the same as in the initialization `char buffer[1024] = {0};`. If you want to use the buffer as a C string, you could append a single termination character with `buffer[valread] = '\0';` after `read` or use `memset` to fill the whole buffer. – Bodo Sep 27 '21 at 13:54

0 Answers0