none
C++ error lnk2019 Socket program

    Question

  • Hello,

    I am new to this whole C/C++ thing and I found a tutorial on a socket program.

    I get the following errors:

    1>WinSockServer.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function _main
    1>WinSockServer.obj : error LNK2019: unresolved external symbol __imp__closesocket@4 referenced in function _main
    1>WinSockServer.obj : error LNK2019: unresolved external symbol __imp__shutdown@8 referenced in function _main
    1>WinSockServer.obj : error LNK2019: unresolved external symbol __imp__recv@16 referenced in function _main
    1>WinSockServer.obj : error LNK2019: unresolved external symbol __imp__connect@12 referenced in function _main
    1>WinSockServer.obj : error LNK2019: unresolved external symbol __imp__gethostbyname@4 referenced in function _main
    1>WinSockServer.obj : error LNK2019: unresolved external symbol __imp__htons@4 referenced in function _main
    1>WinSockServer.obj : error LNK2019: unresolved external symbol __imp__socket@12 referenced in function _main
    1>WinSockServer.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function "void __cdecl wsock_init(void)" (?wsock_init@@YAXXZ)
    

    If you need the code:

    // WinSockServer.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "WinSockHeader.h"
    
    int main(int argc, char* argv[]) {
    	wsock_init();
    	SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	struct sockaddr_in si;
    	si.sin_family = AF_INET;
    	si.sin_port = htons(1234);
    	si.sin_addr = *(struct in_addr*)gethostbyname("towel.blinkenlights.nl")->h_addr;
    	memset(&(si.sin_zero), 0, 8);
    
    	if (connect(sock, (struct sockaddr*)&si, sizeof(struct sockaddr))) {
    		cout << "Could Not connect to host.\n";
    		return 1;
    	}
    	char buf[4096];
    	while (recv(sock, buf, 4096, 0))
    		cout << buf;
    
    	shutdown(sock, SD_BOTH);
    	closesocket(sock);
    	WSACleanup();
    	getchar();
    	return 0;
    }
    
    void wsock_init() {
    	WSADATA wd;
    	if (WSAStartup(MAKEWORD(1,1), &wd)) {
    		cout << "Error initilizing dlls";
    		exit(1);
    	}
    }
    

    and the stdafx.h:

    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    //
    
    #pragma once
    
    #include "targetver.h"
    
    #include <stdio.h>
    #include <tchar.h>
    
    
    
    // TODO: reference additional headers your program requires here
    
    

    and finally my custom header file:

    //WinSock Header file
    //Contains the extra includes and junk
    #include "Winsock2.h"
    #include <iostream>
    
    //namespace
    using namespace std;
    
    //function defines:
    void wsock_init();
    

    If you would like to view the tutorial: http://www.youtube.com/watch?v=0-Uem2FzXXA

    He explains the code in a well manor, in my view.

     

    All help appreciated,

    Visual2010User

     

     

    Monday, February 21, 2011 11:15 PM

Answers

  • You need to link with ws2_32.lib
    Microsoft Test - http://tester.poleyland.com/
    • Marked as answer by Visual2010User Tuesday, February 22, 2011 1:02 AM
    Monday, February 21, 2011 11:22 PM
  • Thanks, that fixed it.

    For anyone reading this that wants to know how, go to "Project-Properties-Configuration Properties-Linker-input-Additional dependencies" then go to the end and type ";ws2_32.lib".

    • Marked as answer by Visual2010User Tuesday, February 22, 2011 2:27 AM
    Tuesday, February 22, 2011 2:27 AM

All replies

  • You need to link with ws2_32.lib
    Microsoft Test - http://tester.poleyland.com/
    • Marked as answer by Visual2010User Tuesday, February 22, 2011 1:02 AM
    Monday, February 21, 2011 11:22 PM
  • Thanks, that fixed it.

    For anyone reading this that wants to know how, go to "Project-Properties-Configuration Properties-Linker-input-Additional dependencies" then go to the end and type ";ws2_32.lib".

    • Marked as answer by Visual2010User Tuesday, February 22, 2011 2:27 AM
    Tuesday, February 22, 2011 2:27 AM