Hello,
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
Code:
bind(123, (struct sockaddr_in*) &sin, 10);
The & would mean the 'address of'. So get the address of sin and cast it to a pointer.
I know how it works, but don't understand why? Any easy explaining would be most helpful.
Code:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
struct in_addr
{
unsigned long s_addr;
};
struct sockaddr_in
{
short int sin_family;
unsigned short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
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);
return EXIT_SUCCESS;
}
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);
return 1;
}
}