struct sockaddr_in servaddr, cliaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
In my previous work on structs i didn't fill the structures with value 0, but here in socket we always reset, fill 0, the struct before using it. Why is it important to fill 0 the structure?
Unless the variables are declared as static storage duration, they will have an arbitrary value upon creation. Hence, it's always a good idea to set them to a known value before using them.
For example, if your next step after socket()
is a connect()
or bind()
, they will very much want a specific structure value for client or server respectively.
It's no different really to the function:
void xyzzy(void) {
int plugh;
printf("%d\n", plugh);
}
inasmuch as it may print any value.