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?
//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];
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
//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;
}