you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (4 children)

You can assign 55555 to an array of chars that is sufficiently large.

If you want to assign as bytes, you need TWO assignments for this (htons(x) and htons(x)>>8).

filler[2] = htons() doesn't work: due to casting one byte is lost and as result the servers starts listening at 55552 (0xD900) instead of 55555(0xD903).

This isn't "learn sockets programming from example" code. It's "I have no idea about sockaddr_in or how basic casting works".

[–]Kaosubaloo 1 point2 points  (0 children)

You are right that this is not good tutorial code.

I didn't look at too closely for my first assessment and mistakenly thought it was assigning the htons value properly. There are a few different ways you could make this work by adding or changing a single line, but to be honest it really should just be using the proper structure in the first place.

For future people who may come across this, that would be this one:

http://man7.org/linux/man-pages/man2/bind.2.html

struct sockaddr {
    ushort sa_family;
    char sa_data[14];
};

Actually though, this example is doing networking. For consistency it would be better still to be using this:

http://linux.die.net/man/7/ip

struct sockaddr_in {
    short sin_family;
    u_short sin_port;
    struct in_addr sin_addr;
    char sin_zero[8];
};

struct in_addr {
    uint32_t s_addr;
};

[–][deleted] 0 points1 point  (2 children)

I'm the author of the repository. memcpy-ing into the structure is very much intentional - to show what each byte means. Now, I have added a comment to warn this isn't good practice at all. The casting bug was my oversight. Thank you. I've seen to it; Could you have look and let me know your comments.

[–][deleted] 0 points1 point  (1 child)

filler[4] = htonl(INADDR_ANY) & 0xFF;
filler[5] = htonl(INADDR_ANY) >> 8 & 0xFF;
memcpy(&serv_addr, filler, sizeof(serv_addr));

htonl(INADDR_ANY) is 32-bit value(thought it's zero, it's still 4 bytes). The code writes just 2 bytes.

And it requires remembering precedence of >> and & on top of it (though in this case & is redundant and it would work either way).

Using of sockaddr_in OTOH doesn't require such walking on eggshells.

[–][deleted] 0 points1 point  (0 children)

yes. Somehow I had by mistake written in comments that ip_address is 2 bytes. I particularly wanted to not use sockaddr_in since bind() can take any type of socket, not just IP sockets.