==============================MODULE40===================================== | | | 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... /*****getipaddr.c ******/ /****a hostname lookup program example******/ #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { struct hostent *h; /*error check the command line*/ if(argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(1); } /*get the host info*/ if((h=gethostbyname(argv[1])) == NULL) { herror("gethostbyname(): "); exit(1); } else printf("gethostbyname() is OK.\n"); printf("The host name is: %s\n", h->h_name); printf("The IP Address is: %s\n", inet_ntoa(*((struct in_addr *)h->h_addr))); printf("The address length is: %d\n", h->h_length); printf("Sniffing other names...sniff...sniff...sniff...\n"); int j = 0; do { printf("An alias #%d is: %s\n", j, h->h_aliases[j]); j++; } while(h->h_aliases[j] != NULL); printf("Sniffing other IPs...sniff....sniff...sniff...\n"); int i = 0; do { printf("Address #%i is: %s\n", i, inet_ntoa(*((struct in_addr *)(h->h_addr_list[i])))); i++; } while(h->h_addr_list[i] != NULL); return 0; } ----------------------------------------------------------------------------------------------- /*serverprog.c - a stream socket server demo*/ #include #include #include #include #include #include #include #include #include #include #include /* the port users will be connecting to */ #define MYPORT 3490 /* how many pending connections queue will hold */ #define BACKLOG 10 void sigchld_handler(int s) { while(wait(NULL) > 0); } int main(int argc, char *argv[]) { /*listen on sock_fd, new connection on new_fd*/ int sockfd, new_fd; /*my address information*/ struct sockaddr_in my_addr; /*connector’s address information*/ struct sockaddr_in their_addr; int sin_size; struct sigaction sa; int yes = 1; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Server-socket() error lol!"); exit(1); } else printf("Server-socket() sockfd is OK...\n"); if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("Server-setsockopt() error lol!"); exit(1); } else printf("Server-setsockopt is OK...\n"); /* host byte order*/ my_addr.sin_family = AF_INET; /* short, network byte order*/ my_addr.sin_port = htons(MYPORT); /* automatically fill with my IP*/ my_addr.sin_addr.s_addr = INADDR_ANY; printf("Server-Using %s and port %d...\n", inet_ntoa(my_addr.sin_addr), MYPORT); /* 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"); exit(1); } else printf("Server-bind() is OK...\n"); if(listen(sockfd, BACKLOG) == -1) { perror("Server-listen() error"); exit(1); } printf("Server-listen() is OK...Listening...\n"); /*clean all the dead processes*/ sa.sa_handler = sigchld_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if(sigaction(SIGCHLD, &sa, NULL) == -1) { perror("Server-sigaction() error"); exit(1); } else printf("Server-sigaction() is OK...\n"); /*accept() loop*/ while(1) { sin_size = sizeof(struct sockaddr_in); if((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { perror("Server-accept() error"); continue; } else printf("Server-accept() is OK...\n"); printf("Server-new socket, new_fd is OK...\n"); printf("Server: Got connection from %s\n", inet_ntoa(their_addr.sin_addr)); /* this is the child process */ if(!fork()) { /* child doesn’t need the listener */ close(sockfd); if(send(new_fd, "This is a test string from server!\n", 37, 0) == -1) perror("Server-send() error lol!"); close(new_fd); exit(0); } else printf("Server-send is OK...!\n"); /* parent doesn’t need this*/ close(new_fd); printf("Server-new socket, new_fd closed successfully...\n"); } return 0; } ----------------------------------------------------------------------------------------------- /*** clientprog.c ****/ /*** a stream socket client demo ***/ #include #include #include #include #include #include #include #include #include //the port client will be connecting to #define PORT 3490 //max number of bytes we can get at once #define MAXDATASIZE 300 int main(int argc, char *argv[]) { int sockfd, numbytes; char buf[MAXDATASIZE]; struct hostent *he; //connector’s address information struct sockaddr_in their_addr; //if no command line argument supplied if(argc != 2) { fprintf(stderr, "Client-Usage: %s the_client_hostname\n", argv[0]); //just exit exit(1); } //get the host info if((he=gethostbyname(argv[1])) == NULL) { perror("gethostbyname()"); exit(1); } else printf("Client-The remote host is: %s\n", argv[1]); if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket()"); exit(1); } else printf("Client-The socket() sockfd is OK...\n"); //host byte order their_addr.sin_family = AF_INET; //short, network byte order printf("Server-Using %s and port %d...\n", argv[1], PORT); their_addr.sin_port = htons(PORT); their_addr.sin_addr = *((struct in_addr *)he->h_addr); //zero the rest of the struct memset(&(their_addr.sin_zero), '\0', 8); if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { perror("connect()"); exit(1); } else printf("Client-The connect() is OK...\n"); if((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv()"); exit(1); } else printf("Client-The recv() is OK...\n"); buf[numbytes] = '\0'; printf("Client-Received: %s", buf); printf("Client-Closing sockfd\n"); close(sockfd); return 0; } ----------------------------------------------------------------------------------------------- /*receiverprog.c - a server, datagram sockets*/ #include #include #include #include #include #include #include #include #include /* the port users will be connecting to */ #define MYPORT 4950 #define MAXBUFLEN 500 int main(int argc, char *argv[]) { int sockfd; /* my address information */ struct sockaddr_in my_addr; /* connector’s address information */ struct sockaddr_in their_addr; int addr_len, numbytes; char buf[MAXBUFLEN]; if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("Server-socket() sockfd 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); /* automatically 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("Server-bind() error lol!"); exit(1); } else printf("Server-bind() is OK...\n"); addr_len = sizeof(struct sockaddr); if((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("Server-recvfrom() error lol!"); /*If something wrong, just exit lol...*/ exit(1); } else { printf("Server-Waiting and listening...\n"); printf("Server-recvfrom() is OK...\n"); } printf("Server-Got packet from %s\n", inet_ntoa(their_addr.sin_addr)); printf("Server-Packet is %d bytes long\n", numbytes); buf[numbytes] = '\0'; printf("Server-Packet contains \"%s\"\n", buf); if(close(sockfd) != 0) printf("Server-sockfd closing failed!\n"); else printf("Server-sockfd successfully closed!\n"); return 0; } ----------------------------------------------------------------------------------------------- /*senderprog.c - a client, datagram*/ #include #include #include #include #include #include #include #include #include #include /* the port users will be connecting to */ #define MYPORT 4950 int main(int argc, char *argv[]) { int sockfd; /* connector’s address information */ struct sockaddr_in their_addr; struct hostent *he; int numbytes; if (argc != 3) { fprintf(stderr, "Client-Usage: %s \n", argv[0]); exit(1); } /* get the host info */ if ((he = gethostbyname(argv[1])) == NULL) { perror("Client-gethostbyname() error lol!"); exit(1); } else printf("Client-gethostname() is OK...\n"); if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("Client-socket() error lol!"); exit(1); } else printf("Client-socket() sockfd is OK...\n"); /* host byte order */ their_addr.sin_family = AF_INET; /* short, network byte order */ printf("Using port: 4950\n"); their_addr.sin_port = htons(MYPORT); their_addr.sin_addr = *((struct in_addr *)he->h_addr); /* zero the rest of the struct */ memset(&(their_addr.sin_zero), '\0', 8); if((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) { perror("Client-sendto() error lol!"); exit(1); } else printf("Client-sendto() is OK...\n"); printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr)); if (close(sockfd) != 0) printf("Client-sockfd closing is failed!\n"); else printf("Client-sockfd successfully closed!\n"); return 0; } ----------------------------------------------------------------------------------------------- /*selectcp.c - a select() demo*/ #include #include #include #include /* file descriptor for standard input */ #define STDIN 0 int main(int argc, char *argv[]) { struct timeval tval; fd_set readfds; tval.tv_sec = 5; tval.tv_usec = 800000; FD_ZERO(&readfds); FD_SET(STDIN, &readfds); /* don’t care about writefds and exceptfds: */ select(STDIN+1, &readfds, NULL, NULL, &tval); if (FD_ISSET(STDIN, &readfds)) printf("A key was pressed lor!\n"); else printf("Timed out lor!...\n"); return 0; } =====================================================================================================