TCP connection with C on Windows [server/client]

2k views Asked by At

I found that I should use winsock... I tried with this example:

http://www.binarytides.com/winsock-socket-programming-tutorial/

but error starts with this function WSAStartup.

I need client/server connection...

Edit:

I use Windows XP, GCC compiler and Codeblock IDE... I get this error:

C:\Documents and Settings\Administrator\Desktop\FunTool\main.c|15|undefined reference to `WSAStartup@8'|

I am trying to run this code:

/*
    Initialise Winsock
*/

#include<stdio.h>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
    WSADATA wsa;

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    printf("Initialised.");

    return 0;
}
1

There are 1 answers

3
Carsten Hansen On BEST ANSWER

The code compiles and runs fine in Visual Studio 2013.

I am not familiar with the Codeblock IDE, but I suspect it ignores the pragma to link with ws2_32.lib, causing a linker error.

Based on other SO answers, try opening project > build options > linker settings and adding ws2_32.lib.