Where to find the WinSock libraries?

Now that I have a bit more knowledge about the WINPI I thought it might be a good time to try creating programs that can access the internet and all that fun stuff. I found a few online tutorials but I am having a bit of trouble...

One thing all the tutorials(Or nearly all of them, sometimes the tutorials use code so different I thought I had looked up the wrong language) have in common is that they require you to manually link the WinSock library. After searching through the files in the program directory of my IDE (Codeblocks) only to not find it, I tried to Google it, but I have no idea which one to download, do I link the 2.2, 2.0, 1.1, Xceed, Coalesys? Or does it even matter all that much?

Another thing I noticed is that some tutorials used _beginthreadex() to create a thread which then handled all of commands dealing with sockets, while others just had the socket work done inside of either a WINAPI WinMain or a normal main. What is the difference between the two? I had thought that windows automatically handled everything concerning process threads.

Last question: Anyone know of a more up to date WinSock tutorial? The ones I found recommend I use WinSock 1.1 which from that I gather is quite old. Or maybe just show me which tutorial teaches the normal way of doing WinSock(Compared to say, the tutorials which overly complicate the program)?
Last edited on
You should download the Microsoft SDK.

http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=c17ba869-9671-4330-a63e-1fd44e0e2505

The above is updated to cover Windows 7.
Tried using the Microsoft SDK files, didn't work. Turns out MinGW and the Microsoft SDK don't like being mixed, downloaded the new MinGW release and manually included the libws2_32.a file(The MinGW equivalent) tried compiling a program and I THINK it worked... Ran just fine but whether any of the commands and such run correctly I don't know.

Is there a difference between using #include <winsock2.h> and <winsock.h>? Or does winsock2.h include everything in winsock.h?

In terms of a winsock tutorial I sugest two things. First if you are new to socket programing a read of Beej's network programming guide is a must:
http://beej.us/guide/bgnet/

This is not a winsock tutorial but a Unix sockets tutorial, he does make some mentions about specific function calls you need to make on windows to get it to work but the general principle is the same.

If you are a seasoned unix socket programmer then I just sugest looking at the MSDN Library:
http://msdn.microsoft.com/en-us/library/ms740673(VS.85).aspx

Under winsock references you should find a list of all the functions you can use.

A note about thread handeling:
the idea is that another thread is spawned to allow concurrency of accepting connections and recving data (as I understand it). You can use Sets of FD's which allow the same idea but efectively instead of haveing a listen thread runnign you just loop through polling to see if there is information on a socket to be read.
If you read beej's guide he walks you through the different methods including using select (although instead of _beginthreadex() he uses fork() in his example as the guide is written for unix systems)
I would try to avoid creating new threads if you can to handel connections as it can get very messy very fast, lookup the select() and poll() functions which should be able to do the job you want.

Of course if you read beej's guide its all in there.

If you need any help then you can PM me/skype me/post here in the forums and I'm sure everyone will be willing to help you out.
Socket programing is good fun ^_^
closed account (S6k9GNh0)
WinSock2 is just a version chsnge. Use winsock2.h.
I actually could use some help with things, I had stopped messing around with Winsock for a bit because it just got too confusing for me to progress.

One of the biggest problems I am having is the usage of pointers and type casting. I understand the basic idea of a pointer, it points to data in memory, but what I don't understand is how they typecast struct pointers to different kinds of structs, or when they just typecast an entire structure to a completely different type.

An example would be how sockaddr_storage can be typecasting to either a sockaddr_in or a sockaddr_in6.

1
2
3
4
5
6
7
8
struct sockaddr_storage {
    sa_family_t  ss_family;     // address family

    // all this is padding, implementation specific, ignore it:
    char      __ss_pad1[_SS_PAD1SIZE];
    int64_t   __ss_align;
    char      __ss_pad2[_SS_PAD2SIZE];
};


Typecasted to:

1
2
3
4
5
6
struct sockaddr_in {
    short int          sin_family;  // Address family, AF_INET
    unsigned short int sin_port;    // Port number
    struct in_addr     sin_addr;    // Internet address
    unsigned char      sin_zero[8]; // Same size as struct sockaddr
};


I don't understand how it works, does it make sockaddr_storage inherit the values of sockaddr_in? What I really don't understand is how all these structures are filled out, I know you call getaddrinfo() to fill out the addrinfo struct, does that somehow fill out all other structs related to it?

Another example from Beej's guide:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
** showip.c -- show IP addresses for a host given on the command line
*/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
    struct addrinfo hints, *res, *p;
    int status;
    char ipstr[INET6_ADDRSTRLEN];

    if (argc != 2) {
        fprintf(stderr,"usage: showip hostname\n");
        return 1;
    }

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
    hints.ai_socktype = SOCK_STREAM;

    if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
        return 2;
    }

    printf("IP addresses for %s:\n\n", argv[1]);

    for(p = res;p != NULL; p = p->ai_next) {
        void *addr;
        char *ipver;

        // get the pointer to the address itself,
        // different fields in IPv4 and IPv6:
        if (p->ai_family == AF_INET) { // IPv4
            struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
            addr = &(ipv4->sin_addr);
            ipver = "IPv4";
        } else { // IPv6
            struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
            addr = &(ipv6->sin6_addr);
            ipver = "IPv6";
        }

        // convert the IP to a string and print it:
        inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
        printf("  %s: %s\n", ipver, ipstr);
    }

    freeaddrinfo(res); // free the linked list

    return 0;
}


I don't understand how
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; works, it typecasts a addrinfo pointer as a sockaddr_in pointer which points to the ai_addr info, how is all of this filled out? More importantly, how does it work? Why does p have to be typecasted? Why can't we just use a sockaddr_in struct? Is the pointer required for something? Or is it just to keep memory usage down? How is it that the pointer points to memory that relates to the addrinfo struct rather than just an empty sockaddr_in struct?

Topic archived. No new replies allowed.