简体   繁体   中英

Unresolved sockets references under Windows 8

I've installed Windows 8 64bits and Visual Sudio 2012.

Later, in class, the teacher gave us a .cpp file to try a udp server on our computer. It should be just run and use but, in my case, VS is giving me some unresolved externals

#include <stdio.h>
#include <winsock.h>


#define GET_TIME "TIME\r\n"
#define MAX_MSG_SIZE 100

void processClient(LPVOID param);

void main(int argc, char **argv)
{
    SOCKET s, cliSocket;
    WSADATA wsaData;
    int iResult, len;
    struct sockaddr_in serv_addr, cli_addr;
    SECURITY_ATTRIBUTES sa;
    DWORD thread_id;

    if(argc != 2){
        printf("Usage: %s  <time_server_port>\n", argv[0]);
        getchar();
        exit(1);
    }

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        getchar();
        exit(1);
    }

    if((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR){
        printf("Unable to create socket (error: %d)!\n", WSAGetLastError());
        getchar();
        exit(1);
    }

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(atoi(argv[1]));

    if(bind(s, (struct sockaddr *)&serv_addr, sizeof(serv_addr))==SOCKET_ERROR){
        printf("Unable to bind socket to port %s (error: %d)\n", argv[1], WSAGetLastError());
        closesocket(s);
        getchar();
        exit(1);
    }

    listen(s, 5);

    while(1){

        printf("Waiting for a time request...\n");

        len = sizeof(cli_addr);

        if((cliSocket = accept(s, (struct sockaddr *)&cli_addr, &len))==SOCKET_ERROR){
            printf("Unable to accept new connection (error: %d)\n", WSAGetLastError());
            closesocket(s);
            getchar();
            exit(1);
        }

        printf("Connection from %s:%d accepted\n", inet_ntoa(cli_addr.sin_addr),
                    ntohs(cli_addr.sin_port));


        sa.nLength=sizeof(sa);
        sa.lpSecurityDescriptor=NULL;

        if(CreateThread(&sa,0 ,(LPTHREAD_START_ROUTINE)processClient, (LPVOID)cliSocket, (DWORD)0, &thread_id)==NULL){
            printf("Cannot start new slave thread (error: %d)\n", GetLastError());          
            closesocket(cliSocket);
        }

        printf("New slave thread started (id: %d)\n", thread_id);           
    }
}

void processClient(LPVOID param)
{
    SOCKET s;
    int nbytes, len;
    struct sockaddr_in cli_addr;
    char request[MAX_MSG_SIZE], response[MAX_MSG_SIZE];
    SYSTEMTIME systemTime;

    s = (SOCKET)param;

    if((nbytes = recv(s, request, MAX_MSG_SIZE, 0)) == SOCKET_ERROR){

        printf("Unable to receive request (error: %d)\n", WSAGetLastError());
        closesocket(s);
        return;
    }

    request[nbytes] = 0;
    if(strcmp(GET_TIME, request)!=0){
        printf("Unexpected request \"%s\" will be ignored\n", request);
        closesocket(s);
        return;
    }

    GetSystemTime(&systemTime);
    sprintf(response, "%d:%d:%d", systemTime.wHour,
                        systemTime.wMinute, systemTime.wSecond);

    if(send(s, response, strlen(response), 0) == SOCKET_ERROR){
        printf("Unable to send reponse (error: %d)\n", WSAGetLastError());
        closesocket(s);
        return;
    }

    len = sizeof(cli_addr);
    getpeername(s, (struct sockaddr *)&cli_addr, &len);
    printf("\"%s\" sent to %s:%d\n", response, inet_ntoa(cli_addr.sin_addr),
                    ntohs(cli_addr.sin_port));

    closesocket(s);
}

These are the errors:

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _accept@12 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _bind@12 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _closesocket@4 referenced in function "void __cdecl processClient(void *)" (?processClient@@YAXPAX@Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _getpeername@12 referenced in function "void __cdecl processClient(void *)" (?processClient@@YAXPAX@Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _htonl@4 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _htons@4 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _inet_ntoa@4 referenced in function "void __cdecl processClient(void *)" (?processClient@@YAXPAX@Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _listen@8 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _ntohs@4 referenced in function "void __cdecl processClient(void *)" (?processClient@@YAXPAX@Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _recv@16 referenced in function "void __cdecl processClient(void *)" (?processClient@@YAXPAX@Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _send@16 referenced in function "void __cdecl processClient(void *)" (?processClient@@YAXPAX@Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _socket@12 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _WSAStartup@8 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _WSAGetLastError@0 referenced in function "void __cdecl processClient(void *)" (?processClient@@YAXPAX@Z)
1>c:\users\brunoc\documents\visual studio 2012\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe : fatal error LNK1120: 14 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Does someone knows how to solve this? Thanks

打开Project属性,转到ws2_32.lib Linker -> Input ,然后将ws2_32.lib添加到Additional Dependencies编辑行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM