ebook35-21

Similar documents
网络程序设计(socketAPI)

下表所示, 此时 protocol 参数可使用缺省值 0 ; 但如果还有多个协议供选择, 则必须使用 protocol 参数来标识 协议族 ( 仅考虑 IP 协议 传送类型 protocol 参数常量 协议类型 族 ) (/usr/include/linux/in.h) SOCK_STREAM IP

Linux網路傳輸設定

untitled

工程硕士信息通信网实验讲义.doc

它是使用标准 Unix 文件描述符 (file descriptor) 和其它程序通讯的方式 什么? 你也许听到一些 Unix 高手 (hacker) 这样说过 : 呀,Unix 中的一切就是文件!. 那个家伙也许正在说到一个事实 :Unix 程序在执行任何形式的 I/O 的时候, 程序是在读或者写

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

24 数据包 Sockets 阻塞 select()-- 多路同步 I/O 重新回顾 TCP,UDP 47 4 什么是 socket 你经常听到人们谈论着 socket, 或许你还不知道它的确切含义 现在让我告诉你 : 它是使用标准 Unix 文件描述符 (fi


VoIP Make a Rtp Call VoIP Abstract... 2 VoIP RTP...3 Socket IP...9 Config Two Voice-hub

前言 UDP 基础通信实例广播和多播思考练习本章重点. 本章重点...1 UDP 基础通信模型 UDP 常用函数...2 通信实例通信模式 1 通信模式 2 UDP 与多进程...3 广播和多播广播多播...4 思考练习. 2 / 56

Slide 1

Microsoft PowerPoint - Socket programming.ppt [相容模式]

新・明解C言語入門編『索引』

ACE

《 计 算 机 网 络 》

Chapter 5- 运输层 (5)-2017

epub 33-8

C6_ppt.PDF

static struct file_operations gpio_ctl_fops={ ioctl: gpio_ctl_ioctl, open : gpio_open, release: gpio_release, ; #defineled1_on() (GPBDAT &= ~0x1) #def

华清远见就业优势倍增项目手册

PowerPoint Presentation

CC213

引言 ftp 工作原理 FTP 客户端思考练习 要点回顾 1 ip 地址结构 2 字节顺序转换函数 3 IP 格式转换函数 2 / 29

ebook140-8

提纲 1 2 OS Examples for 3

C++ 程式設計

华恒家庭网关方案

Bus Hound 5

untitled

ebook70-13

图 4.2 udpclient 项目解决方案 3. 客户机程序编码如下 : 程序 : udp 客户机程序 udpclient.cpp

ebook15-12

untitled

(Microsoft Word - socket\312\265\321\351\262\316\277\274.doc)

穨control.PDF

C/C++语言 - C/C++数据

FY.DOC

C/C++ 语言 - 循环

第6章 嵌入式Linux网络编程

AL-M200 Series

嵌入式Linux块设备驱动开发解析

<4D F736F F D20BCC6CBE3BBFACDF8C2E7CAB5D1E9D6B8B5BCCAD6B2E12E646F63>

ebook15-10

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

ebook71-13

ebook

ebook12-1

C语言的应用.PDF

Slide 1

Chap6.ppt

Chap04

附录一 简明Socket编程指南

Ps22Pdf

C 1

untitled

/3/15 1, linux. linux,,. : 1.NAT ; 2. (load balance, virtual server);; 3. ; 4. ; 5. 6.VPN; 7. ; 8. ; 9.. (,

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

Simulator By SunLingxi 2003

IP505SM_manual_cn.doc

<4D F736F F F696E74202D20312EB9FEB6FBB1F5B9A4D2B5B4F3D1A7D5E7C1BCA3BAC3E6CFF2D1D0BEBFC9FAB8B4CAD4B5C4BDE1B9B9BBAFC3E6CAD4BFBCBACBCCBDCBF7D3EBCAB5BCF92E BBCE6C8DDC4A3CABD5D>

Microsoft Word - MSP430 Launchpad 指导书.docx

TX-NR3030_BAS_Cs_ indd

Microsoft Word - 第八章透過Ehernet傳輸檔案.doc

新版 明解C++入門編

2 本文尚未完成, 僅供中央機械網管使用!! 要公開也等我寫完吧 G Exploring Socket Programming Prefect 聽說, 鳥人超強. Who Should Read This Book? 本書適合想要實作 Winsock 程式的 Programmer 閱讀 這本書的原則

Microsoft Word - Socket±à³Ì.doc

PowerPoint Presentation

Ps22Pdf

Microsoft Word - Winsock網路程式設計.doc

, 7, Windows,,,, : ,,,, ;,, ( CIP) /,,. : ;, ( 21 ) ISBN : -. TP CIP ( 2005) 1

untitled

(Methods) Client Server Microsoft Winsock Control VB 1 VB Microsoft Winsock Control 6.0 Microsoft Winsock Control 6.0 1(a). 2

CC213


¶C¶L§§¬_™¨ A.PDF

c_cpp

ebook15-C

Title Unassigned.

RunPC2_.doc

[改訂新版]C言語による標準アルゴリズム事典

UDP 8.2 TCP/IP OSI OSI 3 OSI TCP/IP IP TCP/IP TCP/IP Transport Control Protocol TCP User Datagram Protocol UDP TCP TCP/IP IP TCP TCP/IP TC

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

untitled

ebook70-11

第 11 章 互聯網技術 11.1 互聯 網 和 萬 維 網 的 發 展 歷 史 A. 互聯網的發展 互聯網是由 ARPANET 開 始發展的 1969 年 美國國防部高級研究計劃署 (ARPA) 把部分軍事研究所和大 的電腦連接起來 建造了㆒個實驗性的電腦網絡 稱為 ARPANET 並 列 的功能

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

/ / (FC 3)...

ebook140-9

Linux网络编程socket错误分析

概述

Untitiled

第3章.doc

自由軟體教學平台

epub 61-2

untitled

Microsoft Word - 实用案例.doc

Microsoft Word - beej-zhtw.docx

Transcription:

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");