==============================MODULE39===================================== | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Compiled using gcc on Linux/Fedora Core 3 machine... /*testpid.c*/ #include #include #include #include #include int main (int argc, char **argv) { int i, pid; pid = fork(); printf("Forking...the pid: %d\n", pid); for (i = 0; i < 5; i++) printf(" %d %d\n", i, getpid()); if (pid) wait(NULL); return 0; } ---------------------------------------------------------------------------- #include #include #include #include #define MYPORT 3334 int main() { int sockfd; /*socket file descriptor*/ struct sockaddr_in my_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Server-socket() error lol!"); exit(1); } else printf("Server-socket() sockfd is OK...\n"); /* host byte order */ my_addr.sin_family = AF_INET; /* short, network byte order */ my_addr.sin_port = htons(MYPORT); my_addr.sin_addr.s_addr = INADDR_ANY; /* zero the rest of the struct */ memset(&(my_addr.sin_zero), 0, 8); if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("Server-bind() error lol!"); exit(1); } else printf("Server-bind() is OK...\n"); /*....other codes....*/ return 0; } --------------------------------------------------------------------------------------------- #include #include #include #include #define DEST_IP "127.0.0.1" #define DEST_PORT 80 int main(int argc, char *argv[]) { int sockfd; /* will hold the destination addr */ struct sockaddr_in dest_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd == -1) { perror("Client-socket() error lol!"); exit(1); } else printf("Client-socket() sockfd is OK...\n"); /* host byte order */ dest_addr.sin_family = AF_INET; /* short, network byte order */ dest_addr.sin_port = htons(DEST_PORT); dest_addr.sin_addr.s_addr = inet_addr(DEST_IP); /* zero the rest of the struct */ memset(&(dest_addr.sin_zero), 0, 8); if(connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) == -1) { perror("Client-connect() error lol"); exit(1); } else printf("Client-connect() is OK...\n"); /*...other codes...*/ return 0; } -------------------------------------------------------------------------------------------- #include #include #include #include /*the port users will be connecting to*/ #define MYPORT 3440 /* how many pending connections queue will hold */ #define BACKLOG 10 int main() { /* listen on sock_fd, new connection on new_fd */ int sockfd, new_fd; /* my address information, address where I run this program */ struct sockaddr_in my_addr; /* remote address information */ struct sockaddr_in their_addr; int sin_size; sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd == -1) { perror("socket() error lol!"); exit(1); } else printf("socket() is OK...\n"); /* host byte order */ my_addr.sin_family = AF_INET; /* short, network byte order */ my_addr.sin_port = htons(MYPORT); /* auto-fill with my IP */ my_addr.sin_addr.s_addr = INADDR_ANY; /* zero the rest of the struct */ memset(&(my_addr.sin_zero), 0, 8); if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind() error lol!"); exit(1); } else printf("bind() is OK...\n"); if(listen(sockfd, BACKLOG) == -1) { perror("listen() error lol!"); exit(1); } else printf("listen() is OK...\n"); /* ...other codes to read the received data... */ sin_size = sizeof(struct sockaddr_in); new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if(new_fd == -1) perror("accept() error lol!"); else printf("accept() is OK...\n"); /*.....other codes.......*/ close(new_fd); close(sockfd); return 0; } =========================================================================================