Home > Software design >  c port listener client disconnects after 1 message
c port listener client disconnects after 1 message

Time:05-13

I did create a program that listens on a port and prints the message out on the terminal.

My problem is, that I want to connect with telnet (telnet localhost 1337) to my server which works fine. Then I listen on the port, which also works fine, but the loop doesn't execute and I do get this message from my telnet:

┌──(sourcecodeengineer㉿kali)-[~]
└─$ telnet localhost 1337                                                                                     1 ⨯
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
a
Connection closed by foreign host.

I want to listen on the port except I do get a "/shutdown" message which I also implemented.

This would be my code for this attempt:

#include <stdio.h>     // perror, printf
#include <stdlib.h>    // exit, atoi
#include <unistd.h>    // read, write, close
#include <arpa/inet.h> // sockaddr_in, AF_INET, SOCK_STREAM, INADDR_ANY, socket etc...
#include <string.h>    // memset, strcmp


// server program, client program can be found at my Github Repo
int main(int argc, char const *argv[])
{
    int serverFd, clientFd;
    struct sockaddr_in server, client;
    socklen_t len;

    // default port if no port is given to listen on!
    int port = 1337;

    // buffer 
    char buffer[BUFSIZ];

    if (argc == 2)
    {
        port = atoi(argv[1]);
    }

    serverFd = socket(AF_INET, SOCK_STREAM, 0);

    if (serverFd < 0)
    {
        perror("Cannot create socket");
        exit(1);
    }

    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(port);

    len = sizeof(server);

    if (bind(serverFd, (struct sockaddr *)&server, len) < 0)
    {
        perror("Cannot bind socket");
        exit(2);
    }

    if (listen(serverFd, 10) < 0)
    {
        perror("Listen error");
        exit(3);
    } else {
        printf("Listening on port %d.\n", port);
    }
    
    while (1)
    {
        len = sizeof(client);

        if ((clientFd = accept(serverFd, (struct sockaddr *)&client, &len)) < 0)
        {
            perror("accept error");
            exit(4);
        }

        memset(buffer, 0, sizeof(buffer));

        int size = read(clientFd, buffer, sizeof(buffer));

        if (size < 0)
        {
            perror("read error");
            exit(5);
        }

        if (strcmp(buffer, "/shutdown")){
            printf("Shutting down\n");
            close(clientFd);
            break;
        } else {
            printf("Echo: %s\n", buffer);
        }
        
    }

    close(serverFd);

    return EXIT_SUCCESS;
}

Is there also a good way to implement a behavior, so that when more clients connect to my port, I will only accept 1 of them at a time and wait for them to disconnect (telnet -> (CTRL 5))?

CodePudding user response:

The problem is

if (strcmp(buffer, "/shutdown"))

is true if buffer is not equal to "/shutdown", so your listener terminates after first read. As strcmp() doesn't return a boolean value in my opinion one should never write

if (strcmp(...))

or

if (!strcmp( ... ))

but - in your case -

if (strcmp( ... ) == 0) 
  • Related