2013-06-05 5 views
24

우선,이 질문은이 사이트의 모든 곳에서 다 알고 있지만 나는 거의 모든 것을 보았고 무엇이 잘못되었는지를 알 수없는 것처럼 보입니다. 이것은 VS 2012에 있습니다. 감사. 해결되지 않은 외부 기호 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); 
}; 

여기
//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); 
    } 

} 

Socket.cpp

입니다 그리고 여기

#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" 

그리고 여기 내 오류 메시지입니다 STDAFX.H입니다 :

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

링커/입력 설정의 추가 종속성 부분에 Ws2_32.lib를 추가 했습니까? – drescherjm

+0

아니요 ... 시도해보고이 게시물로 돌아갑니다 – Iowa15

+3

이 방법으로 추가 할 수 있습니다. #pragma comment (lib, "Ws2_32.lib") – SChepurin

답변

79

문제는 당신이하지 않은 것입니다 Ws2_32.lib 라이브러리에 대한 링크. 이 문제를 해결하려면 프로젝트의 링커/입력 설정에 대한 추가 종속성 탭에 추가하면됩니다. (코멘트에 SChepurin에 의해 지적) 또는 당신은 당신의 프로젝트의 소스 파일에

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

를 추가 할 수 있습니다.

+0

Eclipse에서 같은 문제가 발생했습니다. 그것을 해결하는 방법? –

+1

Eclipse는 IDE입니다. 당신의 컴파일러는 무엇입니까? 컴파일러가 Visual Studio가 아닌데 어쨌든 도움을 줄 수는 없습니다. – drescherjm

관련 문제