2012-10-24 57 views
0

我使用的是Dev ++,根據我閱讀的套接字編程教程,我需要鏈接到我的項目的libws2_32.a如何在Dev ++中連接libws2_32.a進行套接字編程

我不明白如何做到這一點。有人可以解釋嗎?

這裏是我的代碼:

//CONNECT TO REMOTE HOST (CLIENT APPLICATION) 
//Include the needed header files. 
//Don't forget to link libws2_32.a to your program as well 
#include <winsock.h> 
#pragma comment(lib,"libwsock32.a") 
SOCKET s; //Socket handle 

//CONNECTTOHOST – Connects to a remote host 
bool ConnectToHost(int PortNo, char* IPAddress) 
{ 
    //Start up Winsock… 
    WSADATA wsadata; 

    int error = WSAStartup(0x0202, &wsadata); 

    //Did something happen? 
    if (error) 
     return false; 

    //Did we get the right Winsock version? 
    if (wsadata.wVersion != 0x0202) 
    { 
     WSACleanup(); //Clean up Winsock 
     return false; 
    } 

    //Fill out the information needed to initialize a socket… 
    SOCKADDR_IN target; //Socket address information 

    target.sin_family = AF_INET; // address family Internet 
    target.sin_port = htons (PortNo); //Port to connect on 
    target.sin_addr.s_addr = inet_addr (IPAddress); //Target IP 

    s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket 
    if (s == INVALID_SOCKET) 
    { 
     return false; //Couldn't create the socket 
    } 

    //Try connecting... 

    if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR) 
    { 
     return false; //Couldn't connect 
    } 
    else 
     return true; //Success 
} 

//CLOSECONNECTION – shuts down the socket and closes any connection on it 
void CloseConnection() 
{ 
    //Close the socket if it exists 
    if (s) 
     closesocket(s); 

    WSACleanup(); //Clean up Winsock 
} 

而且我得到的錯誤是:

[Linker error] undefined reference to `[email protected]' 

回答

2

轉到項目選項 - >參數 - >連接 - >添加庫和添加庫文件,你需要 - 在你的情況 - libws2_32.a

+0

我現在鏈接只有一個錯誤: [鏈接器錯誤] undefined引用'WinMain @ 16' 它想要什麼? – Dest

+0

@完全不同的問題 - 新問題。 –

+0

好的,謝謝,我現在要問 – Dest