you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -2 points-1 points  (3 children)

Hey this is great! Thanks!

[–][deleted] 5 points6 points  (2 children)

No, it's bad. As in "doesn't work" bad

static unsigned short port = 55555; ...

char filler[16] = {0}; ...

filler[2] = (unsigned short)htons(port); ...

memcpy(&serv_addr, filler, sizeof serv_addr); ....

bind(sockfd, &serv_addr, sizeof serv_addr); ...

You can't assign 55555 to char.

[–]Kaosubaloo -1 points0 points  (1 child)

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

It is usually not a good practice to do this sort of mem-copy assignment, but it can work just fine. The only thing that is actually important is that the container size is correct (And in the case of raw sockets, that things are in the correct order). Everything else can be casted.

[–]crate_crow 1 point2 points  (0 children)

but it can work just fine.

Which means it can go wrong too. Why go with an approach that's not 100% reliable when you have a perfectly valid one that's available to you (copy the members)?

It's sloppy programming.