2013-08-22 54 views
0

下面是從一個教程which is here我的代碼:什麼是導致我的套接字代碼編譯錯誤,我該如何解決它?

//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> 

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 
} 

我用於編譯的命令是: 克++ -o winsoc.exe -lwsock32 winsoc.cpp

編譯器的輸出是:

C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0x1b): undefined r 
eference to `[email protected]' 
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0x3f): undefined r 
eference to `[email protected]' 
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0x5a): undefined r 
eference to `[email protected]' 
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0x6c): undefined r 
eference to `[email protected]' 
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0x8e): undefined r 
eference to `[email protected]' 
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0xc0): undefined r 
eference to `[email protected]' 
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0xf1): undefined r 
eference to `[email protected]' 
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0xf9): undefined r 
eference to `[email protected]' 
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../libmingw32.a(main.o): In function 
`main': 
C:\MinGW\msys\1.0\src\mingwrt/../mingw/main.c:73: undefined reference to `WinMai 
[email protected]' 
collect2: ld returned 1 exit status 

即時通訊使用的是Windows 7,MinGW的作爲我的編譯器和使用的Visual Studio Express還編制都試過了。

+3

您是否閱讀過錯誤信息?您將'if'拼寫爲'If'和'wsadata'拼寫爲'wssadata'。編輯後的 – Casey

+0

與插座無關。 – xaxxon

+0

你可能想'G ++ winsoc.cpp -o winsoc.exe -lwsock32'如下解釋:http://stackoverflow.com/questions/2033608/mingw-linker-error-winsock您還沒有WinMain函數。 –

回答

2

既簡單錯別字
- 與if
取代If - 與wsadata 取代wssadata - 第三個一旦你解決以上兩個

+0

固定拼寫後錯誤,我仍然有錯誤:http://pastebin.com/KBaxFVKK – user2503170

+0

我已更新我的問題。 – user2503170

+0

@ user2503170看看退休忍者的評論 –

0

嘗試用VS編譯和加入這一行應該dissappear:

#pragma comment(lib,"ws2_32") 

應該可以解決一切^

我不知道這是你的完整代碼,但你需要添加一個入口點。

相關問題