21 Linux L i n u x 211 U N I X U N I X I / O F I F O U N I X I n t e r n e t s o c k e t () s o c k e t () send() r e c v ( read() w r i t e () send() r e c v () I n t e r n e t 212 Internet Internet S O C K _ S T R E A M S O C K _ D G R A M 1, 2 1, 2 t e l n e t H T T P W W W telnet W W W 80 GET H T M L T C P U D P 213 U N I X ( t e l n e t f t p ) ( T C P U D P ) I n t e r n e t ( I P ) ( ) 214
226 Linux i n t struct sockaddr struct sockaddr ; unsigned short sa_family; /* address family, AF_xxx */ char sa_data[14]; /* 14 bytes of protocol address */ sa_family A F _ I N E T s a _ d a t a s o c k a d d r _ i n struct sockaddr_in ; short int sin_family; /* Address family */ unsigned short int sin_port; /* Port number */ struct in_addr sin_addr; /* Internet address */ unsigned char sin_zero[8]; /* Same size as struct sockaddr */ s i n _ z e r o bzero() m e m s e t () 0 s o c k a d d r _ i n s o c k a d d r 215 IP I P i n e t _ a d d r () 132 241 5 10 I P i n a s i n _ a d d rs_addr = inet_addr("132241510"); i n e t _ a d d r () i n e t _ n t o a () p r i n t f ("% s ", i n e t _ n t o a ( i n a s i n _ a d d r )); I P 2151 socket() s o c k e t () #include <sys/typesh> #include <sys/socketh> int socket(int domain, int type, int protocol); d o m a i n A F _ I N E T SOCK_STREAM S O C K _ D G R A M 0 s o c k e t () 2152 bind() c o n n e c t () b i n d ( ) #include <sys/typesh>
21 Linux 227 #include <sys/socketh> int bind(int sockfd, struct sockaddr *my_addr, int addrlen); sockfd s o c k e t () my_addr s o c k a d d r s o c k a d d r I P a d d r l e n sizeof(struct sockaddr) #include <stringh> #include <sys/typesh> #include <sys/socketh> #define MYPORT 3490 m a i n () int sockfd; struct sockaddr_in my_addr; sockfd = socket(af_inet, SOCK_STREAM, 0); /* do some error checking! */ m y _ a d d rsin_family = AF_INET; /* host byte order */ m y _ a d d rsin_port = htons(myport); /* short, network byte order */ m y _ a d d r s i n _ a d d rs_addr = inet_addr("132241510"); b z e r o (&( m y _ a d d rsin_zero), 8); /* zero the rest of the struct */ /* don't forget your error checking for bind(): */ bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)); bind() c o n n e c t () c o n n e c t ( 2153 connect() c o n n e c t () #include <sys/typesh> #include <sys/socketh> int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); s o c k e t () s e r v _ a d d r s o c k a d d r I P sizeof(struct sockaddr) #include <stringh> #include <sys/typesh> #include <sys/socketh> #define DEST_IP "132241510"
228 Linux #define DEST_PORT 23 m a i n () int sockfd; struct sockaddr_in dest_addr; /* will hold the destination addr */ sockfd = socket(af_inet, SOCK_STREAM, 0); /* do some error checking! */ d e s t _ a d d rsin_family = AF_INET; /* host byte order */ d e s t _ a d d rsin_port = htons(dest_port); /* short, network byte order */ d e s t _ a d d r s i n _ a d d rs_addr = inet_addr(dest_ip); b z e r o (&( d e s t _ a d d rsin_zero), 8); /* zero the rest of the struct */ /* don't forget to error check the connect()! */ connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)); c o n n e c t () 2154 listen() l i s t e n () a c c e p t () l i s t e n () int listen(int sockfd, int backlog); s o c k e t () a c c e p t () 20 5 1 l i s t e n () l i s t e n () b i n d () s o c k e t ( ) ; b i n d ( ) ; l i s t e n ( ) ; /* accept() goes here */ 2155 accept() a c c e p t () c o n n e c t ( l i s t e n () a c c e p t ( a c c e p t s e n d () r e c v ()
21 Linux 229 #include <sys/socketh> int accept(int sockfd, void *addr, int *addrlen); a d d r s o c k a d d r _ i n c o n n e c t () sizeof(struct sockaddr_in) a c c e p t () #include <stringh> #include <sys/typesh> #include <sys/socketh> #define MYPORT 3490 /* the port users will be connecting to */ #define BACKLOG 10 /* how many pending connections queue will hold */ m a i n () int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */ struct sockaddr_in my_addr; /* my address information */ struct sockaddr_in their_addr; /* connector's address information */ int sin_size; sockfd = socket(af_inet, SOCK_STREAM, 0); /* do some error checking! */ m y _ a d d rsin_family = AF_INET; /* host byte order */ m y _ a d d rsin_port = htons(myport); /* short, network byte order */ m y _ a d d r s i n _ a d d rs_addr = INADDR_ANY; /* auto-fill with my IP */ b z e r o (&( m y _ a d d rsin_zero), 8); /* zero the rest of the struct */ /* don't forget your error checking for these calls: */ bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)); listen(sockfd, BACKLOG); sin_size = sizeof(struct sockaddr_in); new_fd = accept(sockfd, &their_addr, &sin_size); n e w _ f d s e n d () r e c v () 2156 send() recv() s e n d () int send(int sockfd, const void *msg, int len, int flags); s o c k e t () a c c e p t () 0
230 Linux char *msg = "Beej was here!"; int len, bytes_sent; len = strlen(msg); bytes_sent = send(sockfd, msg, len, 0); s e n d () s e n d () r e c v () s e n d int recv(int sockfd, void *buf, int len, unsigned int flags); 0 r e c v () 2157 sendto() recvfrom() int sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen); s e n d () t o I P s o c k a d d r t o l e n sizeof(struct sockaddr) s e n d t o () r e c v f r o m () r e c v () int recvfrom(int sockfd, void *buf, int len, unsigned int flags struct sockaddr *from, int *fromlen); f r o m I P s o c k a d d r f r o m l e n sizeof(struct sockaddr) r e c v f r o m () 2158 close() shutdown() c l o s e () c l o s e ( s o c k f d ); s h u t d o w n () int shutdown(int sockfd, int how); h o w 0 Further receives are disallowed 1 Further sends are disallowed
21 Linux 231 2 Further sends and receives are disallowed (like close()) shutdown() 0 2159 getpeername() #include <sys/socketh> int getpeername(int sockfd, struct sockaddr *addr, int *addrlen); s o c k a d d r sizeof(struct sockaddr) inet_ntoa() g e t h o s t b y a d d r () 21510 gethostname() g e t h o s t n a m e () g e t p e e r n a m e () g e t h o s t b y n a m e () I P #include <unistdh> int gethostname(char *hostname, size_t size); g e t h o s t n a m e 0 216 DNS DNS Domain Name Service I P I P b i n d () c o n n e c t () s e n d t o () g e t h o s t b y n a m e () #include <netdbh> struct hostent *gethostbyname(const char *name); h o s t e n t h o s t e n struct hostent ; char char int int char *h_name; **h_aliases; h_addrtype; h_length; **h_addr_list; #define h_addr h_addr_list[0] h_name h_aliases h _ a d d r t y p e A F _ I N E T h _ l e n g t h h _ a d d r _ l i s t
232 Linux h_addr h _ a d d r _ l i s t g e t h o s t b y n a m e () h o s t e n t N U L L #include <stdioh> #include <stdlibh> #include <errnoh> #include <netdbh> #include <sys/typesh> #include <netinet/inh> int main(int argc, char *argv[]) struct hostent *h; if (argc!= 2) /* error check the command line */ f p r i n t f ( s t d e r r,"usage: getip address\n"); if ((h=gethostbyname(argv[1])) == NULL) /* get the host info */ h e r r o r (" g e t h o s t b y n a m e "); printf("host name : %s\n", h->h_name); printf("ip Address : %s\n",inet_ntoa(*((struct in_addr *)h->h_addr))); return 0; g e t h o s t b y n a m e () p e r r o r () h e r r o r ( ) 217 / / t e l n e t t e l n e 23 t e l n e t d t e l n e t / S O C K _ S T R E A M S O C K _ D G R A M t e l n e t / t e l n e t d f t p / f t p d b o o t p / b o o t p d f t p f t p d f o r k () a c c e p t () f o r k () 218 Hello, Wo r l d!\ n t e l n e t
21 Linux 233 $ telnet remotehostname 3490 r e m o t e h o s t n a m e #include <stdioh> #include <stdlibh> #include <errnoh> #include <stringh> #include <sys/typesh> #include <netinet/inh> #include <sys/socketh> #include <sys/waith> #define MYPORT 3490 /* the port users will be connecting to */ #define BACKLOG 10 /* how many pending connections queue will hold */ m a i n () int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */ struct sockaddr_in my_addr; /* my address information */ struct sockaddr_in their_addr; /* connector's address information */ int sin_size; if ((sockfd = socket(af_inet, SOCK_STREAM, 0)) == -1) p e r r o r (" s o c k e t "); m y _ a d d rsin_family = AF_INET; /* host byte order */ m y _ a d d rsin_port = htons(myport); /* short, network byte order */ m y _ a d d r s i n _ a d d rs_addr = INADDR_ANY; /* auto-fill with my IP */ b z e r o (&( m y _ a d d rsin_zero), 8); /* zero the rest of the struct */ if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \ == -1) p e r r o r (" b i n d "); if (listen(sockfd, BACKLOG) == -1) p e r r o r (" l i s t e n "); while(1) /* main accept() loop */ sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, \ &sin_size)) == -1) p e r r o r (" a c c e p t "); c o n t i n u e ;
234 Linux printf("server: got connection from %s\n", \ i n e t _ n t o a ( t h e i r _ a d d r s i n _ a d d r )); if (!fork()) /* this is the child process */ if (send(new_fd, "Hello, world!\n", 14, 0) == -1) p e r r o r (" s e n d "); c l o s e ( n e w _ f d ); e x i t ( 0 ); close(new_fd); /* parent doesn't need this */ while(waitpid(-1,null,wnohang) > 0); /* clean up child processes */ 219 3490 #include <stdioh> #include <stdlibh> #include <errnoh> #include <stringh> #include <netdbh> #include <sys/typesh> #include <netinet/inh> #include <sys/socketh> #define PORT 3490 /* the port client will be connecting to */ #define MAXDATASIZE 100 /* max number of bytes we can get at once */ int main(int argc, char *argv[]) int sockfd, numbytes; char buf[maxdata S I Z E ]; struct hostent *he; struct sockaddr_in their_addr; /* connector's address information */ if (argc!= 2) f p r i n t f ( s t d e r r,"usage: client hostname\n"); if ((he=gethostbyname(argv[1])) == NULL) /* get the host info */ h e r r o r (" g e t h o s t b y n a m e ");
21 Linux 235 if ((sockfd = socket(af_inet, SOCK_STREAM, 0)) == -1) p e r r o r (" s o c k e t "); t h e i r _ a d d rsin_family = AF_INET; /* host byte order */ t h e i r _ a d d rsin_port = htons(port); /* short, network byte order */ t h e i r _ a d d rsin_addr = *((struct in_addr *)he->h_addr); b z e r o (&( t h e i r _ a d d rsin_zero), 8); /* zero the rest of the struct */ if (connect(sockfd, (struct sockaddr *)&their_addr, \ sizeof(struct sockaddr)) == -1) p e r r o r (" c o n n e c t "); if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) p e r r o r (" r e c v "); buf[numbytes] = '\0'; printf("received: %s",buf); c l o s e ( s o c k f d ) ; return 0; Connection refused 2110 l i s t e n e r 4950 t a l k e r 4950 l i s t e n e r c #include <stdioh> #include <stdlibh> #include <errnoh> #include <stringh> #include <sys/typesh> #include <netinet/inh> #include <sys/socketh> #include <sys/waith> #define MYPORT 4950 /* the port users will be sending to */ #define MAXBUFLEN 100
236 Linux m a i n () int sockfd; struct sockaddr_in my_addr; /* my address information */ struct sockaddr_in their_addr; /* connector's address information */ int addr_len, numbytes; char buf[maxbuflen]; if ((sockfd = socket(af_inet, SOCK_DGRAM, 0)) == -1) p e r r o r (" s o c k e t "); m y _ a d d rsin_family = AF_INET; /* host byte order */ m y _ a d d rsin_port = htons(myport); /* short, network byte order */ m y _ a d d r s i n _ a d d rs_addr = INADDR_ANY; /* auto-fill with my IP */ b z e r o (&( m y _ a d d rsin_zero), 8); /* zero the rest of the struct */ if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \ == -1) p e r r o r (" b i n d "); addr_len = sizeof(struct sockaddr); if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, \ (struct sockaddr *)&their_addr, &addr_len)) == -1) p e r r o r (" r e c v f r o m "); printf("got packet from %s\n",inet_ntoa(their_addr s i n _ a d d r )); printf("packet is %d bytes long\n",numbytes); buf[numbytes] = '\0'; printf("packet contains \"%s\"\n",buf); c l o s e ( s o c k f d ) ; t a l k e r c #include <stdioh> #include <stdlibh> #include <errnoh> #include <stringh> #include <sys/typesh> #include <netinet/inh> #include <netdbh> #include <sys/socketh> #include <sys/waith> #define MYPORT 4950 /* the port users will be sending to */
21 Linux 237 int main(int argc, char *argv[]) int sockfd; struct sockaddr_in their_addr; /* connector's address information */ struct hostent *he; int numbytes; if (argc!= 3) f p r i n t f ( s t d e r r,"usage: talker hostname message\n"); if ((he=gethostbyname(argv[1])) == NULL) /* get the host info */ h e r r o r (" g e t h o s t b y n a m e "); if ((sockfd = socket(af_inet, SOCK_DGRAM, 0)) == -1) p e r r o r (" s o c k e t "); t h e i r _ a d d rsin_family = AF_INET; /* host byte order */ t h e i r _ a d d rsin_port = htons(myport); /* short, network byte order */ t h e i r _ a d d rsin_addr = *((struct in_addr *)he->h_addr); b z e r o (&( t h e i r _ a d d rsin_zero), 8); /* zero the rest of the struct */ if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0, \ (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) p e r r o r (" s e n d t o "); printf("sent %d bytes to %s\n",numbytes,inet_ntoa(their_addr s i n _ a d d r ) ) ; c l o s e ( s o c k f d ) ; return 0; l i s t e n e r t a l k e r 2111 l i s t e n e r r e c v f o r m () r e c v f o r m () a c c e p t () r e c v *()
238 Linux f c n t l () #include <unistdh> #include <fcntlh> sockfd = socket(af_inet, SOCK_STREAM, 0); fcntl(sockfd, F_SETFL, O_NONBLOCK); C P U s e l e c t ( ) s e l e c t () I / O a c c e p t () r e c v () a c c e p t () r e c v () s e l e c t () s e l e c t () #include <sys/timeh> #include <sys/typesh> #include <unistdh> int select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); r e a d f d s w r i t e f d s e x c e p t f d s s o c k f d 0 s o c k f d r e a d f d s n u m f d s 1 s e l e c t () r e a d f d s F D _ I S S E T () FD_ZERO(fd_set *set) FD_SET(int fd, fd_set *set) fd FD_CLR(int fd, fd_set *set) fd FD_ISSET(int fd, fd_set *set) fd t i m e v a l struct timeval ; int tv_sec; /* seconds */ int tv_usec; /* microseconds */ t v _ s e c t v _ u s e c 1 000 000 µ s 2 5 s #include <sys/timeh> #include <sys/typesh> #include <unistdh>
21 Linux 239 #define STDIN 0 /* file descriptor for standard input */ m a i n () struct timeval tv; fd_set readfds; t vtv_sec = 2; t vtv_usec = 500000; F D _ Z E R O ( & r e a d f d s ) ; FD_SET(STDIN, &readfds); /* don't care about writefds and exceptfds: */ select(stdin+1, &readfds, NULL, NULL, &tv); if (FD_ISSET(STDIN, &readfds)) printf("a key was pressed!\n"); e l s e p r i n t f (" Timed out\n");