0

I am very new to c++ and am struggling to understand why I am getting the error when I build my solution, but not when I compile a file within my solution. My code is as so:

#include <winsock2.h>
#include <WS2tcpip.h>

using namespace std;


int ResolveHostName(const char* pszHostName, sockaddr_in* pAddr)
{

    int ret;
    HRESULT hr = S_OK;
    addrinfo* pResultList = NULL;
    addrinfo hints = {};
    int result = -1;

    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;

    ret = getaddrinfo(pszHostName, NULL, &hints, &pResultList);

    result = (ret == 0) ? 1 : -1;
    if (result != -1)
    {
        // just pick the first one found
        *pAddr = *(sockaddr_in*)(pResultList->ai_addr);
        result = 0;
    }

    if (pResultList != NULL)
    {
        ::freeaddrinfo(pResultList);
    }

    return result;
}


int main()
{

    SOCKET sock = -1;
    WSADATA data = {};
    sockaddr_in addrRemote = {};
    int result;

    WSAStartup(MAKEWORD(2, 2), &data);


    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock != -1)
    {
        if (ResolveHostName("192.168.1.35", &addrRemote) != -1)
        {
            addrRemote.sin_port = htons(80);
            result = connect(sock, (sockaddr*)&addrRemote, sizeof(addrRemote));

            if (result != -1)
            {
                const char* msg = "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n";
                int msg_len = strlen(msg);
                result = send(sock, msg, msg_len, 0);
                if (result != -1)
                {
                    char szBuffer[10000 + 1];
                    result = recv(sock, szBuffer, 10000, 0);
                    if (result > 0)
                    {
                        // safely null terminate
                        szBuffer[result] = '\0';
                        printf("%s\n", szBuffer);
                    }
                }
            }
        }
    }

    if (sock != -1)
    {
        closesocket(sock);
        sock = -1;
    }

    return 0;
}

...this is a basic socket connection example I have copied from off SO and fixed a couple of bugs in. The linker is giving me unresolved external symbol errors though (for example for closesocket, connect, htons, recv etc etc). The error codes are LNK2019 and s final, fatal error of LNK1120.

I get that they linker cannot resolve them, but I am not sure why when the function and main are both in the same Main.cpp file.

Any ideas?

Thanks

gdogg371
  • 3,879
  • 14
  • 63
  • 107
  • ok, that built, but i dont really understand from the comments/answers in the two posts referenced what that did. as i say i am very new to c++...i get the #pragma bit, but not the rest of it – gdogg371 Jun 22 '18 at 23:15
  • 1
    You need to link to a library that provides the socket functionality. I expect MSDN should have mentioned the library needed in the documentation for any of the functions needed. The #pragma comment (lib, ...) is just a way to do that in the code. You could have edited your project settings and added it in the Linker section instead. – drescherjm Jun 22 '18 at 23:17
  • without wishing to sound too much of a moron, why is that not included in the project as #include <"ws2_32.lib">? – gdogg371 Jun 22 '18 at 23:18
  • `#include <"ws2_32.lib">` is wrong. You don't include libraries. – drescherjm Jun 22 '18 at 23:19
  • 1
    Here is an example in the MSDN documentation (scroll to the bottom) that shows the #pragma() https://msdn.microsoft.com/en-us/library/windows/desktop/ms742213(v=vs.85).aspx – drescherjm Jun 22 '18 at 23:21

0 Answers0