Thanks for the reply.
The reason i haven't included the header files, as I am practicing using the structure and passing them using pointers and references. So this is just some programming practice for me. And as I am learning socket programming, thought I would kill 2 birds with one stone.
I have corrected what you said in your last post.
Just one question about references
The & would mean the 'address of'. So get the address of sin and cast it to a pointer.
bind(123, (struct sockaddr_in*) &sin, 10);
I know how it works, but don't understand why? Any easy explaining would be most helpful.
unsigned long s_addr;
short int sin_family;
unsigned short sin_port;
struct in_addr sin_addr;
int bind(int sockfd, struct sockaddr_in *my_addr, int length);
int main(int argc, char *argv)
printf("Socket Programming example!\n");
struct sockaddr_in sin;
sin.sin_family = 123;
sin.sin_port = 7000;
sin.sin_addr.s_addr = 1921;
printf("sin_family: %d\n", sin.sin_family);
printf("sin_port: %d\n", sin.sin_port);
printf("sin_addr: %lu\n", sin.sin_addr.s_addr);
int result = bind(123, (struct sockaddr_in*) &sin, 10);
int bind(int sockfd, struct sockaddr_in *my_addr, int length)
printf("\nsockfd: %d\n", sockfd);
printf("sin_family: %d\n", my_addr->sin_family);
printf("sin_port: %d\n", my_addr->sin_port);
printf("nsin_address: %lu\n", my_addr->sin_addr.s_addr);