1

I have a working code of the server application. Now I need to use cpp_int in my project. However, when I just try to include boost/multiprecision/cpp_int.hpp

accept(listeningSocket, (sockaddr*)&client, &sizeofclient);

returns INVALID_SOCKET, and programm terminates with the code 3.

This is a code of server.cpp

#include <iostream>
#include <Ws2tcpip.h>
//#include <boost/multiprecision/cpp_int.hpp> // reason of the probmlem
#pragma comment(lib, "ws2_32.lib")
constexpr auto MSIZE = 4096;
using namespace std;

int main()
{
    WSADATA wsData;
    WORD ver = MAKEWORD(2, 2);
    if (WSAStartup(ver, &wsData) != 0)
    {
        cerr << "Can't initialise winsock\nQuiting...\n";
        return 1;
    }   
    SOCKET listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (listeningSocket == INVALID_SOCKET)
    {
        cerr << "Can't create a socket!\nQuiting...\n";
        return 2;
    }
    sockaddr_in hint;
    hint.sin_family = AF_INET;
    hint.sin_port = htons(54000);
    hint.sin_addr.S_un.S_addr = INADDR_ANY;
    bind(listeningSocket, (sockaddr*)&hint, sizeof(hint));
    listen(listeningSocket, SOMAXCONN);
    cout << "Listenning\n";
    sockaddr_in client;
    int sizeofclient = sizeof(client);
    SOCKET clientSocket = accept(listeningSocket, (sockaddr*)&client, &sizeofclient);
    if (clientSocket == INVALID_SOCKET)
    {
        cerr << "Invalid socket.\nQuiting...\n";
        return 3;
    }

    char host[NI_MAXHOST];
    char service[NI_MAXHOST];
    ZeroMemory(host, NI_MAXHOST);
    ZeroMemory(service, NI_MAXHOST);
    char buf[MSIZE];
    while (1)
    {
        ZeroMemory(buf, MSIZE);
        int bytesReceived = recv(clientSocket, buf, MSIZE, 0);
        if (bytesReceived == SOCKET_ERROR)
        {
            cerr << "Error in recv()\n";
            break;
        }
        if (bytesReceived == 0)
        {
            cout << "Client disconnected" << endl;
            break;
        }
        if (strcmp("\r\n", buf) != 0)
        {
            cout << "(Request from " << host << ") >>  " << buf << endl;
            if (strcmp(buf, "hello") == 0)
            {
                char response[100] = "Greetings!\n\r";
                send(clientSocket, response, sizeof(response) + 1, 0);
            }
            else 
            {
                char response[100] = "Invalid command!\n\r";
                send(clientSocket, response, sizeof(response) + 1, 0);
            }
        }
    }
    cout << "Quiting program\n";
    return 0;
}

Any ideas?

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61

1 Answers1

4

The problem is a side effect of your use of using namespace std;.

When you include <boost/multiprecision/cpp_int.hpp>, it pulls in std::bind() from C++'s <functional> header, and that is the function your code ends up calling instead of Winsock's bind() function. Thus, listen() and accept() fail because the server socket is not bound to a network interface (and your code is not checking for errors on bind() and listen()). Had you checked the error codes for listen() and accept(), you would have seen that they were both reporting WSAEINVAL (meaning "The socket has not been bound with bind" and "The listen function was not invoked prior to accept", respectively).

You need to either

  • Get rid of using namespace std;, and then explicitly use the std:: prefix where needed (this is the preferred solution!)

  • when calling bind(), you can prefix it with :: to tell the compiler that you want to call the bind() function from the global namespace (Win32 APIs, like Winsock, do not use namespaces in C++) rather than the one from the std namespace.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770