00001 /* 00002 inet_addr example 00003 AUP2, Sec. 8.02.3 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 #include <sys/socket.h> 00023 #include <netinet/in.h> 00024 #include <arpa/inet.h> 00025 /*[pgm]*/ 00026 #define REQUEST "GET / HTTP/1.0\r\n\r\n" 00027 00028 int main(void) 00029 { 00030 struct sockaddr_in sa; 00031 int fd_skt; 00032 char buf[1000]; 00033 ssize_t nread; 00034 00035 sa.sin_family = AF_INET; 00036 sa.sin_port = htons(80); 00037 sa.sin_addr.s_addr = inet_addr("216.109.125.70"); 00038 ec_neg1( fd_skt = socket(AF_INET, SOCK_STREAM, 0) ) 00039 ec_neg1( connect(fd_skt, (struct sockaddr *)&sa, sizeof(sa)) ) 00040 ec_neg1( write(fd_skt, REQUEST, strlen(REQUEST) ) ) 00041 ec_neg1( nread = read(fd_skt, buf, sizeof(buf)) ) 00042 (void)write(STDOUT_FILENO, buf, nread); 00043 ec_neg1( close(fd_skt) ) 00044 exit(EXIT_SUCCESS); 00045 00046 EC_CLEANUP_BGN 00047 exit(EXIT_FAILURE); 00048 EC_CLEANUP_END 00049 } 00050 /*[]*/ 00051 00052 int main1(void) 00053 { 00054 struct sockaddr_in sa; 00055 int fd_skt; 00056 char buf[1000]; 00057 ssize_t nread; 00058 00059 sa.sin_family = AF_INET; 00060 sa.sin_port = htons(80); 00061 sa.sin_addr.s_addr = htonl((216UL << 24) + (109UL << 16) + 00062 (125UL << 8) + 70UL); /* 216.109.125.70 */ 00063 printf("%ld %lx\n", (long)sa.sin_addr.s_addr, (long)sa.sin_addr.s_addr); 00064 ec_neg1( fd_skt = socket(AF_INET, SOCK_STREAM, 0) ) 00065 ec_neg1( connect(fd_skt, (struct sockaddr *)&sa, sizeof(sa)) ) 00066 ec_neg1( write(fd_skt, REQUEST, strlen(REQUEST) ) ) 00067 ec_neg1( nread = read(fd_skt, buf, sizeof(buf)) ) 00068 (void)write(STDOUT_FILENO, buf, nread); 00069 ec_neg1( close(fd_skt) ) 00070 exit(EXIT_SUCCESS); 00071 00072 EC_CLEANUP_BGN 00073 exit(EXIT_FAILURE); 00074 EC_CLEANUP_END 00075 }