C ++ WinSock2错误

时间:2013-06-12 15:45:33

标签: c++ winsock2

昨天我尝试用C ++创建一个套接字服务器,但是编译时出错了。 错误:

错误6错误LNK2019:函数“public:static unsigned long __cdecl Env :: GetSocket(void)”中引用了未解析的外部符号_ imp _socket @ 12(?GetSocket @ Env @@ SAKXZ) C:\ Users \ JoshuaTha \ Documents \ Visual Studio 2010 \ Projects \ HabboV5 \ HabboV5 \ Network.obj HabboV5

错误5错误LNK2019:未解析的外部符号_ imp _listen @ 8在函数“public:void __thiscall Network :: Start(void)”中引用(?Start @ Network @@ QAEXXZ)C: \ Users \ JoshuaTha \ Documents \ Visual Studio 2010 \ Projects \ HabboV5 \ HabboV5 \ Network.obj HabboV5

错误4错误LNK2019:未解析的外部符号_ imp _htons @ 4在函数“public:void __thiscall Network :: Start(void)”中引用(?Start @ Network @@ QAEXXZ)C: \ Users \ JoshuaTha \ Documents \ Visual Studio 2010 \ Projects \ HabboV5 \ HabboV5 \ Network.obj HabboV5

错误3错误LNK2019:未解析的外部符号_ imp _bind @ 12在函数“public:void __thiscall Network :: Start(void)”中引用(?Start @ Network @@ QAEXXZ)C: \ Users \ JoshuaTha \ Documents \ Visual Studio 2010 \ Projects \ HabboV5 \ HabboV5 \ Network.obj HabboV5

错误2错误LNK2001:未解析的外部符号“public:static class Network * Env :: Network”(?Network @ Env @@ 2PAV0 @ A)C:\ Users \ JoshuaTha \ Documents \ Visual Studio 2010 \ Projects \ HabboV5 \ HabboV5 \ HabboV5.obj HabboV5

错误7错误LNK1120:5个未解析的外部C:\ Users \ JoshuaTha \ Documents \ Visual Studio 2010 \ Projects \ HabboV5 \ Debug \ HabboV5.exe HabboV5

我的主要.cpp类:

// HabboV5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "Env.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
cout.write("hi", 2);
cout << "Hello World!" << endl;

Env::Network = new Network();
Env::Network->Start();

while (1)
{
    char input[256];
    cin.getline(input, 256);
}
}

Network.h:

#pragma once
#include <WinSock2.h>

class Network
{
private:
    SOCKET socket;
public:
    Network(void);
    void Start();
};

Network.cpp:

#include "StdAfx.h"
#include "Network.h"
#include <WinSock2.h>
#include "Env.h"

Network::Network(void)
{
}

void Network::Start()
{
    this->socket = Env::GetSocket();

    SOCKADDR_IN sInformation;

    sInformation.sin_family = AF_INET;
    sInformation.sin_addr.s_addr = INADDR_ANY;
    sInformation.sin_port = htons(30000);

    bind(this->socket, (SOCKADDR*) (&sInformation), sizeof(sInformation));
    listen(this->socket, 10);
}

Env.h:

#include "stdafx.h"
#include "Network.h"
#include <WinSock2.h>

class Env
{
public:
    static Network* Network;

    static DWORD GetSocket()
    {
        return socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    }
};

1 个答案:

答案 0 :(得分:3)

在链接器选项中(在项目上单击鼠标右键,链接器,输入),您需要将wsock32.libws2_32.lib添加到输入文件列表中。