0

While the Socket itself is initialized, Binding Socket refused to bind returning -1 (socket operation on non-socket). Socket code:

#include "Socket.hpp"

Network::Socket::Socket(int domain, int service, int protocol, int port, unsigned long interface) {
    // Defining address structure
    address.sin_family = domain;
    address.sin_port = htons(port);
    address.sin_addr.s_addr = htonl(interface);

    // Establishing connection
    // Establishing socket
    int sock = socket(domain, service, protocol);
    int reuse = 1;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse));
    setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse));
    std::cout << "Testing Socket" << std::endl;
    test_connection(sock);
}

// Test the connection TODO: maybe change this
void Network::Socket::test_connection(int to_test) {
    std::cout << "in test connection " << to_test << std::endl;
    if (to_test < 0) {
        perror("Connection failed");
        exit(EXIT_FAILURE);
    }
}

// Getters
struct sockaddr_in Network::Socket::get_address() {
    return address;
}

int Network::Socket::get_sock() {
    return sock;
}

Binding Socket code:

#include "BindingSocket.hpp"

// Constructor
Network::BindingSocket::BindingSocket(int domain, int service, int protocol, int port, unsigned long interface) : Socket(domain, service, protocol, port, interface) {
    connect_to_network(get_sock(), get_address());
    std::cout << "Testing BindingSocket " << std::endl; 
    test_connection(binding);
};

// Definition of connect_to network
void Network::BindingSocket::connect_to_network(int sock, struct sockaddr_in address) {
    binding = bind(sock, (struct sockaddr *)&address, sizeof(address));
}

Here's the Server initializer (to see what I actually put into socket when initialized):

#include "TestServer.hpp"
Network::TestServer::TestServer() : SimpleServer(AF_INET, SOCK_STREAM, 0, 80, INADDR_ANY, 10) {
    launch();
}

// Accepts the connection
void Network::TestServer::accepter() {
    struct sockaddr_in address = get_socket()->get_address();
    int addrlen = sizeof(address);
    new_socket = accept(get_socket()->get_sock(), (struct sockaddr *)&address, (socklen_t *)&addrlen);
    read(new_socket, buffer, 3000); // should store the request in the buffer
}

// Handles the connection
void Network::TestServer::handler() {
    std::cout << buffer << std::endl;
}

// Responds to the connection
void Network::TestServer::responder() {
    char *hello_msg = "Hello from server";
    write(new_socket, hello_msg, sizeof(hello_msg) - 1); // Could not figure out a workaround, predefined function requires const void *__buf as well as size_t __n, although they are deprecated (gcc will give a warning, ignore it)
    close(new_socket);
}

void Network::TestServer::launch() {
    while(true) {
        std::cout << "IDLE" << std::endl;
        accepter();
        handler();
        responder();
        std::cout << "COMPLETED" << std::endl;
    }
}

I am very confused as to why this doesn't work.

After googling the issue I tried to reset the port and address (as can be seen in the Socket.cpp), but it didn't work. Also two other socket types do not work, not only Binding: Listening and Connecting sockets. I am guessing that the problem has the same root for all three but can't find it myself. All show the same issue: socket operation on non-socket. Please, help!

2
  • 2
    You have int sock = socket(domain, service, protocol); in your Network::Socket constructor so you're initializing a local variable -- not the class member named sock as returned by Network::Socket::get_sock. Commented Jul 3, 2024 at 11:48
  • this hurts. Thank you @G.M. you are a lifesaver Commented Jul 3, 2024 at 12:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.