




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
數(shù)據(jù)流通信數(shù)據(jù)流套接口是可靠的面向連接的通信數(shù)據(jù)流。如果在套接口中以“1,2”的順序放入兩個數(shù)據(jù),它們在另一端也會以“1,2”的順序到達,它們也可以被認為是無錯誤的傳輸。軟帝信息科技http
阻塞,等待客戶連接請求bind()listen()accept()recv()socket()send()處理服務請求服務器connect()send()recv()socket()close()close()客戶機接連建立服務請求服務響應軟帝信息科技http
socket通信常用API函數(shù)socket(
)使用系統(tǒng)調用socket()來獲得文件描述符,該調用的格式如下:#include
<sys/types.h>#include
<sys/socket.h>intsocket(int ,
int
type,
int
protocol);軟帝信息科技http
socket函數(shù)說明軟帝信息科技http
bind(
)一旦有了一個套接口以后,下一步工作就是把套接口綁定到本地計算機的某一個端口上,但如果只想使用connect()則無此必要。下面是系統(tǒng)調用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)調用由客戶端調用,它的用法如下:#include
<sys/types.h>#include
<sys/socket.h>int
connect(int
sockfd,
struct
sockaddr
*serv_addr,intaddrlen);軟帝信息科技http
connect函數(shù)說明軟帝信息科技http
listen(
)在服務器端,如果希望等待一個進入的連接請求,然后再處理這個連接請求,可以通過首先調用listen(),然后再調用accept()來實現(xiàn)。系統(tǒng)調用liste
n()的定義如下:#include
<sys/socket.h>int
listen(int
sockfd,
int
backlog);軟帝信息科技http
listen函數(shù)說明軟帝信息科技http
accept()當在遠端的客戶機試圖通過connect()連接服務器使用
listen()正在偵聽的端口時,此連接將會在隊列中等待,直到服務器使用accept()處理它。調用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)調用,它們的用法為:#include
<sys/socket.h>in
t(int
sockfd, const
void
*msg,
int
len,
int
flags);軟帝信息科技http
send(
)
和recv(
)這兩個函數(shù)是在建立連接后用于完成發(fā)送與接收數(shù)據(jù)的系統(tǒng)調用,它們的用法為:int
recv(int
sockfd, void
*buf,
int
len,
unsigned
int
flags);軟帝信息科技http
sendto()和recvfrom()
因為數(shù)據(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()調用關閉連接的套接口文件描述符,它的調用方式為:close(sockfd);這樣以后就不能再對此套接口進行任何的讀/寫操作了。使用系統(tǒng)調用shutdown(),可有
的控制權。int
shutdown(int
sockfd,
int
how);第1個參數(shù)是希望切斷通信的套接口文件描述符。第2個參數(shù)how值如下:0:不允許再接收數(shù)據(jù)。1:不允許再發(fā)送數(shù)據(jù)。2:發(fā)送和接收數(shù)據(jù)都不允許。shutdown()調用如果成功返回0,如果失敗則返回-l。軟帝信息科技http
網(wǎng)絡數(shù)據(jù)流編程/*******服務器程序
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)絡數(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)絡數(shù)據(jù)流編程/*端
不對,退出*/if((portnumber=atoi(argv[1]))<0){fprintf(stderr,"Usage:%s
portnumber\a\n",argv[0]);exit(1);}/*服務器端開始建立socket描述符*/if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){fprintf(stderr,"Socket
error:%s\n\a",strerror(errno));exit(1);}軟帝信息科技http
網(wǎng)絡數(shù)據(jù)流編程/*服務器端填充sockaddr結構*/bzero(&server_addr,sizeof(struct
sockaddr_in));server_addr.sin_family=AF_INET;/*自動填充主機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)絡數(shù)據(jù)流編程/*
sockfd描述符*/if(listen(sockfd,
WAITBUF)==-1)
{fprintf(stderr,"Listen
error:%s\n\a",strerror(errno));exit(1);}while(1){/*服務器阻塞,直到客戶程序建立連接*/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)絡數(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)結束*/close(new_fd);/*循環(huán)下一個*/}//while(1)close(sockfd);exit(0);軟帝信息科技http
}網(wǎng)絡數(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)絡數(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)絡數(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)絡數(shù)據(jù)流編程—客戶端/*客戶程序填充服務端的資料*/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)絡數(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);/*結束通訊*/close(sockfd);exit(0);}軟帝信息科技http
socket編程高級特性IP地址和
的轉換在網(wǎng)絡上標識一臺機器時既可采用IP,也可采用兩者的轉換呢?可以使用如下函數(shù):,那么怎么實現(xiàn)struct hostent
*gethosbyname(constchar
*hostname)struct hostent
*gethostbyaddr(const
char
*addr,
int
len,int
type)gethostbyname()可以將機器名(如linux.yessun.tom)轉換為一個hostent結構指針,在其中
了
的地址信息。gethostbyaddr()可以將一個32位的IP地址(C0A80001)轉換為結構指針,在其中
了
的地址信息。軟帝信息科技http
socket編程高級特性IP地址和
的轉換軟帝信息科技http
字符串的IP地址和32位的IP地址轉換“192.168.0.1”應該表示為C0A80001。int
inet_aton(con
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 博士生招生監(jiān)督管理辦法
- 江蘇搬運裝卸業(yè)管理辦法
- 大型企業(yè)差旅費管理辦法
- usb拷貝材料管理辦法
- 畢節(jié)市交通安全管理辦法
- 重慶律師會費管理辦法
- 醫(yī)院內審科績效管理辦法
- 公路機械廠宿舍管理辦法
- 中外合資子公司管理辦法
- 淄博市土地儲備管理辦法
- 民法學全套精美課件
- 叉車安全駕駛技術(叉車基礎知識、安全駕駛、動力裝置)課件
- 國內高品質膠原蛋白行業(yè)發(fā)展白皮書
- 《莊子》寓言對后世的影響
- 質量過程報告記錄匯總表-scr與ncr表格報檢單
- 湖南省長沙市2022-2023學年新高一英語入學分班考試試卷【含答案】
- k-bus產品手冊中文版ip interface使用手冊
- 第九講有機化學結構理論
- 工程化學復習要點及習題解答童志平版本PPT課件
- 論中心蝶閥、單、雙、三、四偏心蝶閥
- 《中國語言文化》課程教學大綱
評論
0/150
提交評論