




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
數(shù)據(jù)流通信數(shù)據(jù)流套接口是可靠的面向連接的通信數(shù)據(jù)流。如果在套接口中以“1,2”的順序放入兩個數(shù)據(jù),它們在另一端也會以“1,2”的順序到達(dá),它們也可以被認(rèn)為是無錯誤的傳輸。軟帝信息科技http
阻塞,等待客戶連接請求bind()listen()accept()recv()socket()send()處理服務(wù)請求服務(wù)器connect()send()recv()socket()close()close()客戶機(jī)接連建立服務(wù)請求服務(wù)響應(yīng)軟帝信息科技http
socket通信常用API函數(shù)socket(
)使用系統(tǒng)調(diào)用socket()來獲得文件描述符,該調(diào)用的格式如下:#include
<sys/types.h>#include
<sys/socket.h>intsocket(int ,
int
type,
int
protocol);軟帝信息科技http
socket函數(shù)說明軟帝信息科技http
bind(
)一旦有了一個套接口以后,下一步工作就是把套接口綁定到本地計算機(jī)的某一個端口上,但如果只想使用connect()則無此必要。下面是系統(tǒng)調(diào)用bind()的定義:#include
<sys/types.h>#include
<sys/socket.h>int
bind(int
sockfd,
struct
sockaddr
*my_addr,
intaddrlen);軟帝信息科技http
bind函數(shù)說明軟帝信息科技http
connect(
)connect()系統(tǒng)調(diào)用由客戶端調(diào)用,它的用法如下:#include
<sys/types.h>#include
<sys/socket.h>int
connect(int
sockfd,
struct
sockaddr
*serv_addr,intaddrlen);軟帝信息科技http
connect函數(shù)說明軟帝信息科技http
listen(
)在服務(wù)器端,如果希望等待一個進(jìn)入的連接請求,然后再處理這個連接請求,可以通過首先調(diào)用listen(),然后再調(diào)用accept()來實現(xiàn)。系統(tǒng)調(diào)用liste
n()的定義如下:#include
<sys/socket.h>int
listen(int
sockfd,
int
backlog);軟帝信息科技http
listen函數(shù)說明軟帝信息科技http
accept()當(dāng)在遠(yuǎn)端的客戶機(jī)試圖通過connect()連接服務(wù)器使用
listen()正在偵聽的端口時,此連接將會在隊列中等待,直到服務(wù)器使用accept()處理它。調(diào)用accept()之后,將會返回一個全新的套接口文件描述符來處理這個連接。accept()的用法如下:#include
<sys/socket.h>intaccept(int
sockfd,
void
*addr,
int
*addrlen);軟帝信息科技http
accept函數(shù)說明軟帝信息科技http
send(
)
和recv(
)這兩個函數(shù)是在建立連接后用于完成發(fā)送與接收數(shù)據(jù)的系統(tǒng)調(diào)用,它們的用法為:#include
<sys/socket.h>in
t(int
sockfd, const
void
*msg,
int
len,
int
flags);軟帝信息科技http
send(
)
和recv(
)這兩個函數(shù)是在建立連接后用于完成發(fā)送與接收數(shù)據(jù)的系統(tǒng)調(diào)用,它們的用法為:int
recv(int
sockfd, void
*buf,
int
len,
unsigned
int
flags);軟帝信息科技http
sendto()和recvfrom()
因為數(shù)據(jù)報套接口是無連接的,它并不連接到
的主機(jī)上,所以在發(fā)送數(shù)據(jù)包之前,必須首先給出目的地址:in
dto(int
sockfd, const
void
*msg,
int
len,unsignedint
flags, const
stuct
sockaddr
*to,
inttolen);intrecvfrom(intsockfd,void
*buf,
intlen,unsignedintflags,
stuctsockaddr
*from,
int
*fromlen);軟帝信息科技http
sendto函數(shù)說明軟帝信息科技http
close(
)和shutdown()可以使用close()調(diào)用關(guān)閉連接的套接口文件描述符,它的調(diào)用方式為:close(sockfd);這樣以后就不能再對此套接口進(jìn)行任何的讀/寫操作了。使用系統(tǒng)調(diào)用shutdown(),可有
的控制權(quán)。int
shutdown(int
sockfd,
int
how);第1個參數(shù)是希望切斷通信的套接口文件描述符。第2個參數(shù)how值如下:0:不允許再接收數(shù)據(jù)。1:不允許再發(fā)送數(shù)據(jù)。2:發(fā)送和接收數(shù)據(jù)都不允許。shutdown()調(diào)用如果成功返回0,如果失敗則返回-l。軟帝信息科技http
網(wǎng)絡(luò)數(shù)據(jù)流編程/*******服務(wù)器程序
TCPServer.c
************/#include
<stdlib.h>#include
<stdio.h>#include
<errno.h>#include
<string.h>#include
<netdb.h>#include
<sys/types.h>#include
<netinet/in.h>#include
<sys/socket.h>#define
WAITBUF
10軟帝信息科技http
網(wǎng)絡(luò)數(shù)據(jù)流編程int
main(int
argc,
char
*argv[
]){intsockfd,new_fd;struct
sockaddr_in
server_addr;struct
sockaddr_in
client_addr;int
sin_size,portnumber;char o[
]=“ o!
Socket
communication
world!\n”;if(argc!=2){fprintf(stderr,"Usage:%s
portnumber\a\n",argv[0]);exit(1);}軟帝信息科技http
網(wǎng)絡(luò)數(shù)據(jù)流編程/*端
不對,退出*/if((portnumber=atoi(argv[1]))<0){fprintf(stderr,"Usage:%s
portnumber\a\n",argv[0]);exit(1);}/*服務(wù)器端開始建立socket描述符*/if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){fprintf(stderr,"Socket
error:%s\n\a",strerror(errno));exit(1);}軟帝信息科技http
網(wǎng)絡(luò)數(shù)據(jù)流編程/*服務(wù)器端填充sockaddr結(jié)構(gòu)*/bzero(&server_addr,sizeof(struct
sockaddr_in));server_addr.sin_family=AF_INET;/*自動填充主機(jī)IP*/server_addr.sin_addr.s_addr=htonl(INADDR_ANY);server_addr.sin_port=htons(portnumber);/*
sockfd描述符*/if(bind(sockfd,(struct
sockaddr
*)(&server_addr),sizeof(struct
sockaddr))==-1)
{fprintf(stderr,"Bind
error:%s\n\a",strerror(errno));exit(1);軟帝信息科技http
}網(wǎng)絡(luò)數(shù)據(jù)流編程/*
sockfd描述符*/if(listen(sockfd,
WAITBUF)==-1)
{fprintf(stderr,"Listen
error:%s\n\a",strerror(errno));exit(1);}while(1){/*服務(wù)器阻塞,直到客戶程序建立連接*/sin_size=sizeof(struct
sockaddr_in);if((new_fd=accept(sockfd,(struct
sockaddr
*)(&client_addr),&sin_size))==-1){fprintf(stderr,"Accept
error:%s\n\a",strerror(errno));exit(1);}/*可以在這里加上自己的處理函數(shù)*/軟帝信息科技http
網(wǎng)絡(luò)數(shù)據(jù)流編程fprintf(stderr,"Serverget
connection
from
%s\n",inet_ntoa(client_addr.sin_addr));if(send(new_fd,
o,strlen(
o),0)==-1)
{fprintf(stderr,"Write
Error:%s\n",strerror(errno));exit(1);}/*這個通信已經(jīng)結(jié)束*/close(new_fd);/*循環(huán)下一個*/}//while(1)close(sockfd);exit(0);軟帝信息科技http
}網(wǎng)絡(luò)數(shù)據(jù)流編程—客戶端/*******
客戶端程序
TCPClient.c
************/#include
<stdlib.h>#include
<stdio.h>#include
<errno.h>#include
<string.h>#include
<netdb.h>#include
<sys/types.h>#include
<netinet/in.h>#include
<sys/socket.h>#define
RECVBUFSIZE
1024軟帝信息科技http
網(wǎng)絡(luò)數(shù)據(jù)流編程—客戶端int
main(int
argc,
char
*argv[]){int
sockfd;char
buffer[RECVBUFSIZE];struct
sockaddr_in
server_addr;struct
hostent
*host;intportnumber,nbytes;if(argc!=3)
{fprintf(stderr,"Usage:%s
hostname
portnumber\a\n",argv[0]);exit(1);}軟帝信息科技http
網(wǎng)絡(luò)數(shù)據(jù)流編程—客戶端if((portnumber=atoi(argv[2]))<0)
{fprintf(stderr,"Usage:%s
hostname
portnumber\a\n",argv[0]);exit(1);}/*客戶程序開始建立sockfd描述符*/if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){fprintf(stderr,"Socket
Error:%s\a\n",strerror(errno));exit(1);}軟帝信息科技http
網(wǎng)絡(luò)數(shù)據(jù)流編程—客戶端/*客戶程序填充服務(wù)端的資料*/bzero(&server_addr,sizeof(server_addr));server_addr.sin_family=AF_INET;server_addr.sin_port=htons(portnumber);server_addr.sin
_
addr.s_addr=inet_addr(argv[1]);/*客戶程序發(fā)起連接請求*/if(connect(sockfd,(struct
sockaddr
*)(&server_addr),sizeof(struct
sockaddr))==-1){fprintf(stderr,"Connect
Error:%s\a\n",strerror(errno));exit(1);}軟帝信息科技http
網(wǎng)絡(luò)數(shù)據(jù)流編程—客戶端/*連接成功,接收數(shù)據(jù)*/if((nbytes=recv(sockfd,buffer,
RECVBUFSIZE,0))==-1){fprintf(stderr,"Read
Error:%s\n",strerror(errno));exit(1);}buffer[nbytes]='\0';printf("I
have
received:%s\n",buffer);/*結(jié)束通訊*/close(sockfd);exit(0);}軟帝信息科技http
socket編程高級特性IP地址和
的轉(zhuǎn)換在網(wǎng)絡(luò)上標(biāo)識一臺機(jī)器時既可采用IP,也可采用兩者的轉(zhuǎn)換呢?可以使用如下函數(shù):,那么怎么實現(xiàn)struct hostent
*gethosbyname(constchar
*hostname)struct hostent
*gethostbyaddr(const
char
*addr,
int
len,int
type)gethostbyname()可以將機(jī)器名(如linux.yessun.tom)轉(zhuǎn)換為一個hostent結(jié)構(gòu)指針,在其中
了
的地址信息。gethostbyaddr()可以將一個32位的IP地址(C0A80001)轉(zhuǎn)換為結(jié)構(gòu)指針,在其中
了
的地址信息。軟帝信息科技http
socket編程高級特性IP地址和
的轉(zhuǎn)換軟帝信息科技http
字符串的IP地址和32位的IP地址轉(zhuǎn)換“192.168.0.1”應(yīng)該表示為C0A80001。int
inet_aton(con
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 醫(yī)療企業(yè)布袋管理辦法
- 數(shù)智時代涉外商事糾紛解決之道
- 廚房配料使用管理辦法
- 河南循環(huán)包裝管理辦法
- 1850年以來吳語文獻(xiàn)的詞匯演變與語法特征研究
- 民航包機(jī)乘客管理辦法
- 高壓變電站主接線系統(tǒng)優(yōu)化設(shè)計研究
- 對話自我與外界:探索個體與社會的互動
- 培訓(xùn)機(jī)構(gòu)抽成管理辦法
- 辦公利益制度管理辦法
- 會議活動復(fù)盤報告
- 隧道安全運營管理制度
- 2025年銀行從業(yè)資格考試公共基礎(chǔ)知識必考題庫及答案(共五套)
- 山東省2024年藝術(shù)類本科批美術(shù)與設(shè)計類第1次志愿投檔情況表(公布)
- 2025-2030膠原酶產(chǎn)業(yè)發(fā)展分析及發(fā)展趨勢與投資前景預(yù)測報告
- 血液凈化中心護(hù)理工作總結(jié)
- 2025年當(dāng)兵的心理測試題及答案
- 2025年中級管道工(四級)技能認(rèn)定理論考試指導(dǎo)題庫(含答案)
- 頭端可彎曲負(fù)壓吸引鞘在輸尿管軟鏡碎石術(shù)處理長徑≤2cm上尿路結(jié)石中的應(yīng)用研究
- 重大活動交通保障應(yīng)急預(yù)案
- 凈水設(shè)備維保合同
評論
0/150
提交評論