2013-06-05 59 views
24

Trước hết, tôi biết câu hỏi này là tất cả trên trang web này nhưng tôi đã xem xét hầu như tất cả chúng và dường như không thể tìm ra điều gì sai. Đây là trong VS 2012. Cảm ơn.Biểu tượng bên ngoài chưa được giải quyết LNK2019

//Socket.h 
#pragma once 

#include <iostream> 
#include <WinSock2.h> 

using namespace std; 

const int STRLEN = 256; 

class Socket 
{ 
    protected: 
     WSADATA wsaData; 
     SOCKET mySocket; 
     SOCKET myBackup; 
     SOCKET acceptSocket; 
     sockaddr_in myAddress; 
    public: 
     Socket(); 
     ~Socket(); 
     bool SendData(char*); 
     bool RecvData(char*, int); 
     void CloseConnection(); 
     void GetAndSendMessage(); 
}; 

class ServerSocket : public Socket 
{ 
    public: 
     void Listen(); 
     void Bind(int port); 
     void StartHosting(int port); 
}; 

class ClientSocket : public Socket 
{ 
    public: 
     void ConnectToServer(const char *ipAddress, int port); 
}; 

Dưới đây là Socket.cpp

//Socket.cpp 


#include "stdafx.h" 
#include "Socket.h" 


Socket::Socket() 
{ 
    if(WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) 
    { 
     cerr<<"Socket Initialization: Error with WSAStartup\n"; 
     system("pause"); 
     WSACleanup(); 
     exit(10); 
    } 

    //Create a socket 
    mySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 

    if (mySocket == INVALID_SOCKET) 
    { 
     cerr<<"Socket Initialization: Error creating socket"<<endl; 
     system("pause"); 
     WSACleanup(); 
     exit(11); 
    } 

    myBackup = mySocket; 
} 

Socket::~Socket() 
{ 
    WSACleanup(); 
} 

bool Socket::SendData(char *buffer) 
{ 
    send(mySocket, buffer, strlen(buffer), 0); 
    return true; 
} 

bool Socket::RecvData(char *buffer, int size) 
{ 
    int i = recv(mySocket, buffer, size, 0); 
    buffer[i] = '\0'; 
    return true; 
} 

void Socket::CloseConnection() 
{ 
    //cout<<"CLOSE CONNECTION"<<endl; 
    closesocket(mySocket); 
    mySocket = myBackup; 
} 

void Socket::GetAndSendMessage() 
{ 
    char message[STRLEN]; 
    cin.ignore();//without this, it gets the return char from the last cin and ignores the following one! 
    cout<<"Send > "; 
    cin.get(message, STRLEN); 
    SendData(message); 
} 

void ServerSocket::StartHosting(int port) 
{ 
    Bind(port); 
    Listen(); 
} 

void ServerSocket::Listen() 
{ 
    //cout<<"LISTEN FOR CLIENT..."<<endl; 

    if (listen (mySocket, 1) == SOCKET_ERROR) 
    { 
     cerr<<"ServerSocket: Error listening on socket\n"; 
     system("pause"); 
     WSACleanup(); 
     exit(15); 
    } 

    //cout<<"ACCEPT CONNECTION..."<<endl; 

    acceptSocket = accept(myBackup, NULL, NULL); 
    while (acceptSocket == SOCKET_ERROR) 
    { 
     acceptSocket = accept(myBackup, NULL, NULL); 
    } 
    mySocket = acceptSocket; 
} 

void ServerSocket::Bind(int port) 
{ 
    myAddress.sin_family = AF_INET; 
    myAddress.sin_addr.s_addr = inet_addr("0.0.0.0"); 
    myAddress.sin_port = htons(port); 

    //cout<<"BIND TO PORT "<<port<<endl; 

    if (bind (mySocket, (SOCKADDR*) &myAddress, sizeof(myAddress)) == SOCKET_ERROR) 
    { 
     cerr<<"ServerSocket: Failed to connect\n"; 
     system("pause"); 
     WSACleanup(); 
     exit(14); 
    } 
} 

void ClientSocket::ConnectToServer(const char *ipAddress, int port) 
{ 
    myAddress.sin_family = AF_INET; 
    myAddress.sin_addr.s_addr = inet_addr(ipAddress); 
    myAddress.sin_port = htons(port); 

    //cout<<"CONNECTED"<<endl; 

    if (connect(mySocket, (SOCKADDR*) &myAddress, sizeof(myAddress)) == SOCKET_ERROR) 
    { 
     cerr<<"ClientSocket: Failed to connect\n"; 
     system("pause"); 
     WSACleanup(); 
     exit(13); 
    } 

} 

Và đây là stdafx.h

#pragma once 

#include "targetver.h" 

#define WIN32_LEAN_AND_MEAN    // Exclude rarely-used stuff from Windows headers 
// Windows Header Files: 
#include <windows.h> 

// C RunTime Header Files 
#include <stdlib.h> 
#include <malloc.h> 
#include <memory.h> 
#include <tchar.h> 


// TODO: reference additional headers your program requires here 
#include "Socket.h" 

Và đây là thông báo lỗi của tôi:

1>------ Build started: Project: Client, Configuration: Debug Win32 ------ 
1> stdafx.cpp 
1> Socket.cpp 
1> Client.cpp 
1> Generating Code... 
1>Socket.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: void __thiscall ServerSocket::Listen(void)" ([email protected]@@QAEXXZ) 
1>Socket.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: void __thiscall ServerSocket::Bind(int)" ([email protected]@@[email protected]) 
1>Socket.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: void __thiscall Socket::CloseConnection(void)" ([email protected]@@QAEXXZ) 
1>Socket.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: void __thiscall ClientSocket::ConnectToServer(char const *,int)" ([email protected]@@[email protected]) 
1>Socket.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: void __thiscall ServerSocket::Bind(int)" ([email protected]@@[email protected]) 
1>Socket.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: void __thiscall ServerSocket::Bind(int)" ([email protected]@@[email protected]) 
1>Socket.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: void __thiscall ServerSocket::Listen(void)" ([email protected]@@QAEXXZ) 
1>Socket.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: bool __thiscall Socket::RecvData(char *,int)" ([email protected]@@[email protected]) 
1>Socket.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: bool __thiscall Socket::SendData(char *)" ([email protected]@@[email protected]) 
1>Socket.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: __thiscall Socket::Socket(void)" ([email protected]@[email protected]) 
1>Socket.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: __thiscall Socket::Socket(void)" ([email protected]@[email protected]) 
1>Socket.obj : error LNK2019: unresolved external symbol [email protected] referenced in function "public: __thiscall Socket::Socket(void)" ([email protected]@[email protected]) 
1>C:\Users\ajayp_000\documents\visual studio 2012\Projects\Client\Debug\Client.exe : fatal error LNK1120: 12 unresolved externals 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+3

Bạn đã thêm Ws2_32.lib vào phần phụ thuộc bổ sung của cài đặt Trình liên kết/nhập liệu của mình chưa? – drescherjm

+0

Không ... Tôi sẽ thử điều đó và quay lại bài đăng này – Iowa15

+3

Bạn có thể thêm bài đăng này theo cách này - #pragma nhận xét (lib, "Ws2_32.lib") – SChepurin

Trả lời

79

Vấn đề là bạn không liên kết với thư viện Ws2_32.lib. Để khắc phục điều này, bạn có thể thêm điều đó vào tab phụ thuộc bổ sung của cài đặt trình liên kết/đầu vào cho dự án của bạn. Ngoài ra (như được chỉ ra bởi SChepurin trong các ý kiến), bạn có thể thêm

#pragma comment(lib, "Ws2_32.lib") 

vào tệp nguồn của dự án của bạn.

+0

Tôi phải đối mặt với cùng một vấn đề trong Eclipse. Làm thế nào để giải quyết nó? –

+1

Eclipse là một IDE. Trình biên dịch của bạn là gì? Mặc dù nếu trình biên dịch không phải là Visual Studio tôi có lẽ không thể giúp anyways. – drescherjm

Các vấn đề liên quan