Establishing a tcp connection from within a DLL

Posted by Nicholas Hollander on Stack Overflow See other posts from Stack Overflow or by Nicholas Hollander
Published on 2014-06-07T20:55:29Z Indexed on 2014/06/07 21:24 UTC
Read the original article Hit count: 298

Filed under:
|
|
|

I'm trying to write a piece of code that will allow me to establish a TCP connection from within a DLL file. Here's my situation: I have a ruby application that needs to be able to send and receive data over a socket, but I can not access the native ruby socket methods because of the environment in which it will be running. I can however access a DLL file and run the functions within that, so I figured I would create a wrapper for winsock. Unfortunately, attempting to take a piece of code that should connect to a TCP socket in a normal C++ application throws a slew of LNK2019 errors that I can not for the life of me resolve.

This is the method I'm using to connect:

//Socket variable
SOCKET s;

//Establishes a connection to the server
int server_connect(char* addr, int port)
{

    //Start up Winsock
    WSADATA wsadata;

    int error = WSAStartup(0x0202, &wsadata);

    //Check if something happened
    if (error)
        return -1;



    //Verify Winock version
    if (wsadata.wVersion != 0x0202)
    {
        //Clean up and close
        WSACleanup();
        return -2;
    }

    //Get the information needed to finalize a socket
    SOCKADDR_IN target;
    target.sin_family = AF_INET; //Address family internet
    target.sin_port = _WINSOCKAPI_::htons(port); //Port #
    target.sin_addr.s_addr = inet_addr(addr);

    //Create the socket
    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (s == INVALID_SOCKET)
    {
        return -3;
    }

    //Try connecting
    if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
    {
        //Failed to connect
        return -4;
    }
    else
    {
        //Success
        return 1;
    }
}

The exact errors that I'm receiving are:

Error   1   error LNK2019: unresolved external symbol _closesocket@4 referenced in function _server_disconnect  [Project Path]
Error   2   error LNK2019: unresolved external symbol _connect@12 referenced in function _server_connect    [Project Path]
Error   3   error LNK2019: unresolved external symbol _htons@4 referenced in function _server_connect   [Project Path]
Error   4   error LNK2019: unresolved external symbol _inet_addr@4 referenced in function _server_connect   [Project Path]
Error   5   error LNK2019: unresolved external symbol _socket@12 referenced in function _server_connect [Project Path]
Error   6   error LNK2019: unresolved external symbol _WSAStartup@8 referenced in function _server_connect  [Project Path]
Error   7   error LNK2019: unresolved external symbol _WSACleanup@0 referenced in function _server_connect  [Project Path]
Error   8   error LNK1120: 7 unresolved externals   [Project Path]  1   1   

Many thanks!

© Stack Overflow or respective owner

Related posts about c++

Related posts about sockets