0

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

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.

user2503170
  • 145
  • 1
  • 8
  • 3
    Did you read the error messages? You misspelled `if` as `If` and `wsadata` as `wssadata`. – Casey Aug 22 '13 at 02:50
  • edited since this has nothing to do with sockets. – xaxxon Aug 22 '13 at 03:10
  • You probably want `g++ winsoc.cpp -o winsoc.exe -lwsock32` as explained here: http://stackoverflow.com/questions/2033608/mingw-linker-error-winsock You also have no WinMain function. – Retired Ninja Aug 22 '13 at 04:55
  • @RetiredNinja Thank you, that fixed the problem. For anyone having the same problem the answer to the "No WinMain" problem I found the answer here: [link](http://stackoverflow.com/questions/5259714/undefined-reference-to-winmain16) – user2503170 Aug 22 '13 at 17:53

2 Answers2

2

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

Karthik T
  • 31,456
  • 5
  • 68
  • 87
0

Try compiling with VS and add this line :

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

NotGI
  • 458
  • 9
  • 21