• Uncategorized

About linux : I-have-a-server-socket-program-runnig-in-Debian-10-but-netstat-does-not-show-it

Question Detail

I am doing a server and client programs in C++ on Debian Linux
Below is the server:

void MonitoringServer::serve() {

int serverSocketFd, childSocketFd;
int clientAddrLen;
struct sockaddr_in serverAddr, clientAddr;
if ((serverSocketFd = socket(AF_INET, SOCK_STREAM, 6)) < 0) {
    std::cout << "could not create socket. exiting..." << std::endl;
} else {
    std::cout << "socket created with file descriptor : " << serverSocketFd << std::endl;
    bzero((char*)&serverAddr, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = inet_addr("192.168.1.92");
    serverAddr.sin_port = htonl(15500);
    if (bind(serverSocketFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
        std::cout << "error binding server socket. exiting..." << std::endl;
        stop = true;
    } else {
        std::cout << "socket file descriptor bound to the specified address" << std::endl;
    }

    int listenResult = listen(serverSocketFd, 10);
    if (listenResult < 0) {
        std::cout << "listen returned a negative value" << std::endl;
        stop = true;
    } else {
        std::cout << "server socket is listening" << std::endl;
    }
    while (!stop) {
        std::cout << "going to accept the next client connection" << std::endl;
        childSocketFd = accept(serverSocketFd, (struct sockaddr*)&clientAddr, (socklen_t*)&clientAddrLen);
        if (childSocketFd < 0) {
            std::cout << "error accepting connections. exiting..." << std::endl;
            switch (errno) {
                case EBADF : std::cout << "socket not valid file descriptor" << std::endl; break;
                case ECONNABORTED : std::cout << "connection aborted" << std::endl; break;
                case EFAULT : std::cout << "address param not in writable part of address space" << std::endl; break;
                case EINTR : std::cout << "accept() terminated by a signal" << std::endl; break;
                case EINVAL : std::cout << "socket is not wiling to accept connections" << std::endl; break;
                case EMFILE : std::cout << "The per-process descriptor table is full" << std::endl; break;
                case ENOMEM : std::cout << "No memory available to complete the operation" << std::endl; break;
                case ENOTSOCK : std::cout << "Descriptor is not a socket" << std::endl; break;
                case EOPNOTSUPP : std::cout << "socket is not of type SOCK_STREAM and thus does not accept connections" << std::endl; break;
                case EWOULDBLOCK : std::cout << "socket is non-blocking and no connections are present to be accepted" << std::endl; break;
                default : std::cout << "unknown error in accept call : " << errno << std::endl;
            }
            stop = true;
        }
        pthread_attr_t attr;
        pthread_t threadID;
        int threadOpRetValue;
        if (!stop) {
            threadOpRetValue = pthread_attr_init(&attr);
            if (threadOpRetValue < 0) {
                std::cout << "error creating client handling thread. exiting..." << std::endl;
                stop = true;
            }
        }
        if (!stop) {
            threadOpRetValue = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
            if (threadOpRetValue < 0) {
                std::cout << "error configuring client handling thread. exiting..." << std::endl;
                stop = true;
            }
        }
        if (!stop) {
            threadOpRetValue = pthread_create(reinterpret_cast<pthread_t *>(threadID), &attr,
                                              &MonitoringServer::handle, &childSocketFd);
            if (threadOpRetValue < 0) {
                std::cout << "error creating the client socket worker thread. exiting..." << std::endl;
                stop = true;
            }
        }
    }
}

}

When I run the program, it blocks on the accept call, but I neither see the process on the netstat -a output, and the below client returns a connection refused error code.

int main(int argc, char **argv) {
std::cout << "starting client..." << std::endl;

int socketFD;
struct sockaddr_in serverAddress;
bool stop = false;

// open the socket
bzero((char*)&serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr(argv[1]);
serverAddress.sin_port = htons(atoi(argv[2]));

if ((socketFD = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    std::cout << "error creating the socket. exiting..." << std::endl;
    stop = true;
}
int connRetValue;
if (!stop && (connRetValue = connect(socketFD, (struct sockaddr*)&serverAddress, sizeof serverAddress)) < 0) {
    std::cout << "error code " << connRetValue << std::endl;
    std::cout << "error connecting to the server. exiting..." << std::endl;
    connRetValue = errno;
    switch (connRetValue) {
        case EACCES : std::cout << "broadcast address specified" << std::endl; break;
        case EADDRINUSE : std::cout << "address in use" << std::endl; break;
        case EADDRNOTAVAIL : std::cout << "address not available" << std::endl; break;
        case EAFNOSUPPORT : std::cout << "wrong address family" << std::endl; break;
        case EBADF : std::cout << "bad socket descriptor" << std::endl; break;
        case ECONNREFUSED : std::cout << "connection refused" << std::endl; break;
        case EFAULT : std::cout << "address out of process address space" << std::endl; break;
        case EHOSTUNREACH : std::cout << "target host is unreachable" << std::endl; break;
        case EINPROGRESS : std::cout << "connection in progress in nonblocking socket" << std::endl; break;
        case EINTR : std::cout << "execution interrupted by a signal" << std::endl; break;
        case EISCONN : std::cout << "socket already connected" << std::endl; break;
        case ENETDOWN : std::cout << "network interface mal-function" << std::endl; break;
        case ENETUNREACH : std::cout << "network unreachable" << std::endl; break;
        case ENOBUFS : std::cout << "unable to allocate buffer in memory" << std::endl; break;
        case ENOTSOCK : std::cout << "descriptor is not a socket" << std::endl; break;
        case EOPNOTSUPP : std::cout << "socket is listening. operation not allowed" << std::endl; break;
        case EPROTOTYPE : std::cout << "address of different type than the socket" << std::endl; break;
        case ETIMEDOUT : std::cout << "timeout" << std::endl; break;
        case ECONNRESET : std::cout << "remote host reset the connection" << std::endl; break;
        default : std::cout << "unknown error code : " << connRetValue << std::endl;
    }
    stop = true;
}
if (!stop) {
    char* message = "ola bichinhos";
    write(socketFD, message, strlen(message)*sizeof(char));
}
close(socketFD);
return 0;

}

I am starting to suspect of selinux imposing security restrictions on what my processes can do, but I am not sure.
Can someone tell me if there is anything wrong with this code before I start to investigate SE linux ?

Question Answer

I was building the address port with the wrong conversion function.
I was using htonl and it should be htons.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.