Why are these headers included?

I am programming my first server and socket programs using a couple different tutorials. I started going through comment the code so that I am able to study it more. I removed three of the header files and the program still compiles and runs. My question is why does this program work with out sys/types.h, sys/socket.h, and netinet/in.h?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
//#include <sys/types.h>
//#include <sys/socket.h>
//#include <netinet/in.h>
#include <arpa/inet.h>


//This function will be called when something fails. It will display an error.
void pERR(const char *error){
perror(error);
exit(1); //usually indicates unsucessful termination
}

int main(int argc, char **argv){

int sockfd, newsockfd;//sockfd and newsockfd store values returned by the socket system call and accept calls//fd = file descriptor
int port; //port stores the portnumber the server accepts connections on
int noc; //noc(Number Of Characters) return value for the read() and write() calls
socklen_t clilen; //clilen stores the size of the clients address
char buffer[256]; //Server reads characters from the socket connection into buffer
char client_addr_ipv6[100];

struct sockaddr_in6 ser_addr, cli_addr;//ser_adder = servers address///cli_addr = clients address

// struc sockaddr_in6{ //<netinet/in.h>
// sin6_family; /* addr family:AF_INET6 */ //MAN7:
// sin6_port; /* port in byte order */ //sockaddr_in6
// sin6_flowinfo; /* IPv6 flow info */ //DESCRIPTION
// sin6_addr; /* IPv6 address */
// uint32_t; /* Scope ID */
// }

//This will tell the user how to use the program if they just run the program with out arguements
if(argc < 2){

fprintf(stderr,"\nUSAGE: %s (port number)\n\n", argv[0]);
exit(0);
}

printf("\nIPv6 TCP Server Started...\n\n");

sockfd = socket(AF_INET6, SOCK_STREAM, 0);//The user needs to pass the port number the server will communicate on as an arguement

//sock() system call creates a new socket taking three arugements
// socket(
// sin_family, //<netinet/in.h>
// socket_type, //MAN7
// protocol //socket(); DESCRIPTION
// );

if(sockfd < 0){

pERR("ERROR opening the socket");
}

bzero((char *) &ser_addr, sizeof(ser_addr));//Sets all values in a buffer to zero

// bzero(
// void *s, /* pointer to the buffer */ //<strings.h>
// size_t n /* size of the buffer */ //
// )

port = atoi(argv[1]); //Sets the int value of the first arguement entered after the name
ser_addr.sin6_family = AF_INET6; //Assigns the addr family in our server struct to AF_INET
ser_addr.sin6_port = htons(port); //Assigns the port in our server struct using network byte order
ser_addr.sin6_flowinfo = 0; //Assigns flowinfoin our server strut to 0
ser_addr.sin6_addr = in6addr_any; //Assigns the IP addr in our sturct to the IP of the machine running this

if(bind(sockfd, (struct sockaddr *) &ser_addr, sizeof(ser_addr)) < 0){

pERR("ERROR on binding");
}

//Allows the process to listen on the designated socket for connections
listen(sockfd, 5);

// listen(
// int sockfd, /* file descriptor that refers to a socket type */
// int backlog /* defines max length that pending sockfd connections may grow */
// );

clilen = sizeof(cli_addr);

//Causes the process to block until a client connects to the server. It wakes the system when a connection has been established
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
// accept(
// int sockfd, //Socket that was created
// struct sock *addr, //Reference pointer to the address of the client
// socklen_t *addrlen //The size of the structure
// );

if(newsockfd < 0){

pERR("Error on accept");
}


inet_ntop(AF_INET6, &(cli_addr.sin6_addr), client_addr_ipv6, 100);// converts IPv4 and IPv6 address from binary to text
printf("Incoming connection from client having IPv6 address: %s\n", client_addr_ipv6);

bzero(buffer,256);//reinitializes the buffer to zeros

noc = read(newsockfd, buffer, 255);//attempts to read up to read bytes from file descriptor into the buffer starting at buffer
// read(
// int fd,
// void *buf,
// size_t count
// );

if(noc < 0){

pERR("ERROR reading from socket");
}

printf("Message from client: %s\n", buffer);

noc = write(newsockfd, "I got your message", 18);//attempts to read up to read bytes from file descriptor into the buffer starting at buffer
// write(
// int fd,
// const void *buf,
// size_t count
// );

if(noc < 0){

pERR("ERROR writing to socket");
}

//Closes the file descriptors sockfd and newsockfd so that they may no longer be used
close(sockfd);
close(newsockfd);
return 0;
}
I think that I found the answer. Is this why?

"Inclusion of the <arpa/inet.h> header may also make visible all symbols from <netinet/in.h> and <inttypes.h>."

Also in the code i have replaced the bzero() function with memset(). I found out that that bzero() should no longer be used.
Topic archived. No new replies allowed.