版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Linux22007-09-19 21:15:32C/C+LinuxBS2. TCPLinux(socket).socket,linux.2.1 socketint socket(int domain, int type,int protocol)domain:(AF_UNIXAF_INET).AF_UNIXUnix,AF_INETInternet, ( man socket domain PF_*AF_*,glibcposixPFAF,).type:(SOCK_STREAM,SOCK_DGRAM) SOCK_STREAMTCP,. SOCK_DGRAM
2、UDP,.protocol:type,0 socket.,-1,error.2.2 bind, (listen(),) connect(), , bind() :int bind(int sockfd, struct sockaddr *my_addr, int addrlen)sockfd:socket.addrlen:sockaddr.my_addr:sockaddr. sockaddrstruct sockaddrunisgned short as_family;char sa_data14;,(struct sockaddr_in) .sockaddr_instruct sockadd
3、r_inunsigned short sin_family;unsigned short int sin_port;struct in_addr sin_addr;unsigned char sin_zero8;Internetsin_familyAF_INET,sin_addrINADDR_ANY,sin_port.sin_zero8. bindsocket.0,socket2.3 listen, , :,-listen(),-accept() ( ), listen int listen(int sockfd, int backlog);sockfd socket() backlog ?
4、(accept() ) 20,510,-1, errno, listen() bind() , :socket();bind();listen();/* accept() */2.4 accept, accept() ! : (listen() (connect() (accept() accept() !, (send() ( recv() !int accept(int sockfd, struct sockaddr *addr,int *addrlen)sockfd:listen.addr, addrlen,.bind,listenaccept,accept, .accept,. -12
5、.5 connectint connect(int sockfd, struct sockaddr * serv_addr,int addrlen)sockfd:socket.serv_addr:.sin_addaddrlen:serv_addrconnect.0,sockfd -1. telnet socket(), 23( telnet )0? ,connect()- connect() :#include #include int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);sockfd
6、socket() serv_addr IP structsockaddraddrlen sizeof(struct sockaddr)?:#include #include #include #define DEST_IP 0#define DEST_PORT 23main()int sockfd;struct sockaddr_in dest_addr; /* */sockfd = socket(AF_INET, SOCK_STREAM, 0); /* */dest_addr.sin_family = AF_INET; /* host byte order */dest
7、_addr.sin_port = htons(DEST_PORT); /* short, network byte order */dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);bzero(&(dest_addr.sin_zero),; /* zero the rest of the struct */* dont forget to error check the connect()! */connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr);.2.6:so
8、cket:int int, : ( octet, ) , (Network Byte Order), NBO ,( htons() ) (Host Byte Order) NBO, (TM)-struct sockaddr. :struct sockaddr unsigned short sa_family; /* , AF_xxx */char sa_data14; /*14*/;sa_family , AF_INET sa_data struct sockaddr,: struct sockaddr_in (in Internet)struct sockaddr_in short int
9、sin_family; /* */unsigned short int sin_port; /* */struct in_addr sin_addr; /* Internet */unsigned char sin_zero8; /* sockaddr*/; sin_zero (, structsockaddr ) bzero() memset() , sockaddr_insockaddr socket() struct sockaddr *, struct sockaddr_in, sin_family struct sockaddr sa_family AF_INET,sin_port
10、sin_addr (Network Byte Order)!:, struct in_addr sin_addr ? , : struct in_addr, (unions):/* Internet () */struct in_addr unsigned long s_addr;, ina struct sockaddr_in ,ina.sin_addr.s_addr 4 IP () struct in_addr ,4 IP (#define)struct hostentstruct hostent char *h_name;char *h_aliases;int h_addrtype;in
11、t h_length;char *h_addr_list;#define h_addr h_addr_list0struct hostent:h_name ? h_aliases ? -h_addrtype ?; AF_INETh_length ? h_addr_list ? -h_addr - h_addr_list2.7 ,!: short () long () unsigned short h (host), to, n (network), s short: h-to-n-s, htons() (Host to Network Short).,n,h,s, l ,stolh() (Sh
12、ort toLong Host) , ,:htons()-Host to Network Shorthtonl()-Host to Network Longntohs()-Network to Host Shortntohl()-Network to Host Long,: char ? , : 68000 , htonl() IP , ,! Unix !: ,: struct sockaddr_in , sin_addr sin_port ,sin_family ? : sin_addr sin_port IP UDP , sin_family (kernel) , sin_family ,
13、2.8 /* (server.c) */* Name:server.c* Used to study the network programming in Linux OS.* Showing how to use the functions,* like socket,bind,listen,accept and write.* This is the server program.* Author:zeickey* Date:2006/9/16* Copyright (c) 2006,All Rights Reserved!*/#include #include #include #inc
14、lude #include #include #include #include int main(int argc, char *argv)int sockfd,new_fd;struct sockaddr_in server_addr;struct sockaddr_in client_addr;int sin_size,portnumber;char hello=Hello! Are You Fine?n;if(argc!=2)fprintf(stderr,Usage:%s portnumberan,argv0);return 1;if( (portnumber = atoi(argv1
15、) 0 )fprintf(stderr,Usage:%s portnumberan,argv0);return 1;/* socket */if( (sockfd = socket(AF_INET,SOCK_STREAM, 0) = -1 )fprintf(stderr,Socket error:%sna,strerror(errno);return 1;/* sockaddr */bzero(&server_addr, sizeof(struct sockaddr_in);memset(&server_addr, 0, sizeof(struct sockaddr_in);server_ad
16、dr.sin_family = AF_INET;server_addr.sin_addr.s_addr = htonl(INADDR_ANY);server_addr.sin_port = htons(portnumber);/* sockfdlisten */if( bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)=-1 )fprintf(stderr,Bind error:%sna,strerror(errno);return 1;/* sockfd */if( -1 = listen(sockfd,
17、5) )fprintf(stderr,Listen error:%sna,strerror(errno);return 1;while(1)/* , */sin_size=sizeof(struct sockaddr_in);/if( (new_fd = accept(sockfd, (struct sockaddr *)(&client_addr), &sin_size) = -1)new_fd = accept(sockfd, (struct sockaddr *)(&client_addr), &sin_size);if( -1 = new_fd )fprintf(stderr,Acce
18、pt error:%sna,strerror(errno);return 1;fprintf(stderr,Server get connection from %sn, inet_ntoa(client_addr.sin_addr);if(write(new_fd,hello,strlen(hello)=-1)fprintf(stderr,Write Error:%sn,strerror(errno);return 1;/* */close(new_fd);/* */close(sockfd);return 0;/* client.c */* Name:client.c* Used to s
19、tudy the network programming in Linux OS.* Showing how to use the functions,* like socket,bind,listen,accept and write.* This is the client program.* Author:zeickey* Date:2006/9/16* Copyright (c) 2006,All Rights Reserved!*/#include #include #include #include #include #include #include int main(int a
20、rgc, char *argv)int sockfd;char buffer1024;struct sockaddr_in server_addr;/struct hostent *host;char *ip;int portnumber,nbytes;if(argc!=3)fprintf(stderr,Usage:%s ip portnumberan,argv0);return 1;/if(host=gethostbyname(argv1)=NULL)printf(agrv1 = %sn,argv1);if( strlen(ip=argv1) 7 )fprintf(stderr,Get Ip address errorn);return 1;if(portnumber=atoi(argv2)h_addr );/server_addr.sin_addr.s_addr = inet_addr(host-h_addr);server_addr.sin_addr.s_addr
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 宅基地轉(zhuǎn)讓合同8篇2
- 2024購(gòu)房補(bǔ)貼借款合同范本2
- 2024個(gè)人借款合同范本「無(wú)息」
- 《環(huán)境保護(hù)法講座》課件
- 蘇州科技大學(xué)天平學(xué)院《裝飾基礎(chǔ)》2022-2023學(xué)年第一學(xué)期期末試卷
- 蘇州科技大學(xué)天平學(xué)院《智能制造技術(shù)與系統(tǒng)》2022-2023學(xué)年第一學(xué)期期末試卷
- 2024上海市房屋轉(zhuǎn)租合同的范本
- 蘇州科技大學(xué)天平學(xué)院《舞蹈編導(dǎo)二》2022-2023學(xué)年第一學(xué)期期末試卷
- 廣告投放與市場(chǎng)競(jìng)爭(zhēng)考核試卷
- 智慧工廠的解決方案
- Honda-Special-Requirement本田的特殊要求-課件
- 北理c語(yǔ)言上機(jī)答案(全)
- 大學(xué)生創(chuàng)新思維與創(chuàng)新能力培養(yǎng)的實(shí)踐探索模板課件
- DB63-T 1674-2018+多年凍土區(qū)+公路隧道技術(shù)規(guī)范
- 雙向情感障礙
- 統(tǒng)編語(yǔ)文教材的主要特點(diǎn)和教學(xué)建議(課堂PPT)課件
- 胃潰瘍 演示文稿課件
- 腹腔鏡膽囊切除術(shù)后膽瘺
- ESD的防護(hù)常識(shí)幻燈片
- 傳感器課程設(shè)計(jì)基于的溫度測(cè)量系統(tǒng)
- 蘇武傳 省賽獲獎(jiǎng)-完整版課件
評(píng)論
0/150
提交評(píng)論