是什么导致了套接字代码上的编译错误?我如何修复它?

时间:2022-11-22 22:45:32

Here is my code from a tutorial 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
}

The command I use to compile is: g++ -o winsoc.exe -lwsock32 winsoc.cpp

我用来编译的命令是:g++ -o winsoc。exe -lwsock32 winsoc.cpp

The compilers output is:

编译器的输出是:

C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0x1b): undefined r
eference to `WSAStartup@8'
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0x3f): undefined r
eference to `WSACleanup@0'
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0x5a): undefined r
eference to `htons@4'
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0x6c): undefined r
eference to `inet_addr@4'
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0x8e): undefined r
eference to `socket@12'
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0xc0): undefined r
eference to `connect@12'
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0xf1): undefined r
eference to `closesocket@4'
C:\Users\Leon\AppData\Local\Temp\ccPq0k1Y.o:winsoc.cpp:(.text+0xf9): undefined r
eference to `WSACleanup@0'
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
n@16'
collect2: ld returned 1 exit status

Im using windows 7, MinGW as my compiler and have tried using visual studio express to compile also.

我使用windows 7, MinGW作为我的编译器,并尝试使用visual studio express来编译。

2 个解决方案

#1


2  

Both simple typos
- replace If with if
- replace wssadata with wsadata - Third one should dissappear once you fix the above two

两个简单的拼写错误——用If替换If——用wsadata替换wssadata——一旦修复了上面两个错误,第三个错误就会消失

#2


0  

Try compiling with VS and add this line :

尝试使用VS编译,并添加如下内容:

#pragma comment(lib,"ws2_32")

Should fix everything ^

应该解决所有问题^

I dont know if this is your full code but you need to add an entry point.

我不知道这是否是您的完整代码,但您需要添加一个入口点。

#1


2  

Both simple typos
- replace If with if
- replace wssadata with wsadata - Third one should dissappear once you fix the above two

两个简单的拼写错误——用If替换If——用wsadata替换wssadata——一旦修复了上面两个错误,第三个错误就会消失

#2


0  

Try compiling with VS and add this line :

尝试使用VS编译,并添加如下内容:

#pragma comment(lib,"ws2_32")

Should fix everything ^

应该解决所有问题^

I dont know if this is your full code but you need to add an entry point.

我不知道这是否是您的完整代码,但您需要添加一个入口点。