trivial socket program failing at accept() with errno 22

40 Views Asked by At
`#if defined (_WIN32)
#ifndef _WIN32_WINNIT
#define _WIN32_WINNT 0x0600
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment (lib, "ws2_32.lib")
#define ISVALIDSOCKET(s) ((s) != INVALID_SOCKET)
#define CLOSESOCKET(s) closesocket(s)
#define GETSOCKETERRONO() (WSAGetLastError())
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define ISVALIDSOCKET(s) ((s) >= 0)
#define CLOSESOCKET(s) close(s)
#define SOCKET int
#define GETSOCKETERRNO() (errno)
#endif


int main(void)
{
        printf("Configuring local address ...\n");
        struct addrinfo hints;
        memset(&hints, 0, sizeof(hints));
        hints.ai_family = AF_INET;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_PASSIVE;

        struct addrinfo *bind_address;
        getaddrinfo(0, "8080", &hints, &bind_address);

        printf("Creating socket...\n");
        SOCKET socket_listen;
        socket_listen = socket(bind_address->ai_family, bind_address->ai_socktype, bind_address->ai_protocol);

        if (!ISVALIDSOCKET(socket_listen))
        {
                fprintf(stderr, "socket() failed. (%d)", GETSOCKETERRNO());
                return 1;
        }

        printf("Binding socket to local address...\n");
        if (bind(socket_listen, bind_address->ai_addr, bind_address->ai_addrlen))
        {
                fprintf(stderr, "bind() failed. (%d)\n", GETSOCKETERRNO());
                return 1;
        }
        freeaddrinfo(bind_address);

        printf("Waiting for connection...\n");
        struct sockaddr_storage client_address;
        socklen_t client_len = sizeof(client_address);
        printf("running the accept function\n");
        SOCKET socket_client = accept(socket_listen, (struct sockaddr*) &client_address, &client_len);
        if (!ISVALIDSOCKET(socket_client))

        {
                fprintf(stderr, "accept() failed. (%d)\n",GETSOCKETERRNO());
                return 1;
        }

        printf("Client is connected...");
        char address_buffer[100];
        getnameinfo((struct sockaddr*)&client_address, client_len, address_buffer, sizeof(address_buffer), 0, 0, NI_NUMERICHOST);
        printf("%s\n", address_buffer);


        printf("Reading request...\n");
        char request [1024];
        int bytes_recieved = recv(socket_client, request, 1024,0 );
        printf("Recieved %d bytes .\n", bytes_recieved);
        printf("%.*s", bytes_recieved,request);


        printf("Sending response...\n");
        const char *response =
                "HTTP/1.1 200 OK\r\n"
                "Connection: close:\r\n"
                "Content-Type: text/plain\r\n\r\n"
                "Local time is: ";
        int bytes_sent = send(socket_client, response, strlen(response), 0);
        printf("Send %d of %d bytes.\n", bytes_sent, (int)strlen(response));


        time_t timer;
        time (&timer);
        char *time_msg = ctime(&timer);
        bytes_sent = (socket_client, time_msg, strlen(time_msg), 0);
        printf("Send %d of %d bytes.\n", bytes_sent, (int)strlen(time_msg));



        printf("Closing connection...\n");
        CLOSESOCKET(socket_client);


        printf("Closing listening socket...\n");
        CLOSESOCKET(socket_listen);


#if defined(_WIN32)
        WSACleanup();
#endif
        printf("Finished.\n");

        return 0;

}`

It terminates with this when executed on ubuntu WSL Configuring local address ... Creating socket... Binding socket to local address... Waiting for connection... accept() failed. (22)

in attempts to mitigate this I've: confirmed that the port is not in use by another process tried running with sudo switched of the firewall and other network protection utilities

0

There are 0 best solutions below