嵌入式Linux網(wǎng)絡(luò)編程之:網(wǎng)絡(luò)高級(jí)編程
掃描二維碼
隨時(shí)隨地手機(jī)看文章
在實(shí)際情況中,人們往往遇到多個(gè)客戶(hù)端連接服務(wù)器端的情況。由于之前介紹的如connet()、recv()和send()等都是阻塞性函數(shù),如果資源沒(méi)有準(zhǔn)備好,則調(diào)用該函數(shù)的進(jìn)程將進(jìn)入睡眠狀態(tài),這樣就無(wú)法處理I/O多路復(fù)用的情況了。本節(jié)給出了兩種解決I/O多路復(fù)用的解決方法,這兩個(gè)函數(shù)都是之前學(xué)過(guò)的fcntl()和select()(請(qǐng)讀者先復(fù)習(xí)第6章中的相關(guān)內(nèi)容)。可以看到,由于在Linux中把socket也作為一種特殊文件描述符,這給用戶(hù)的處理帶來(lái)了很大的方便。
1.fcntl()函數(shù)fcntl()針對(duì)socket編程提供了如下的編程特性。
n 非阻塞I/O:可將cmd設(shè)置為F_SETFL,將lock設(shè)置為O_NONBLOCK。
n 異步I/O:可將cmd設(shè)置為F_SETFL,將lock設(shè)置為O_ASYNC。
下面是用fcntl()將套接字設(shè)置為非阻塞I/O的實(shí)例代碼:
/*net_fcntl.c*/
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/un.h>
#include<sys/time.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<netinet/in.h>
#include<fcntl.h>
#definePORT1234
#defineMAX_QUE_CONN_NM5
#defineBUFFER_SIZE1024
intmain()
{
structsockaddr_inserver_sockaddr,client_sockaddr;
intsin_size,recvbytes,flags;
intsockfd,client_fd;
charbuf[BUFFER_SIZE];
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
server_sockaddr.sin_family=AF_INET;
server_sockaddr.sin_port=htons(PORT);
server_sockaddr.sin_addr.s_addr=INADDR_ANY;
bzero(&(server_sockaddr.sin_zero),8);
inti=1;/*允許重復(fù)使用本地地址與套接字進(jìn)行綁定*/
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i));
if(bind(sockfd,(structsockaddr*)&server_sockaddr,
sizeof(structsockaddr))==-1)
{
perror("bind");
exit(1);
}
if(listen(sockfd,MAX_QUE_CONN_NM)==-1)
{
perror("listen");
exit(1);
}
printf("Listening....\n");
/*調(diào)用fcntl()函數(shù)給套接字設(shè)置非阻塞屬性*/
flags=fcntl(sockfd,F_GETFL);
if(flags<0||fcntl(sockfd,F_SETFL,flags|O_NONBLOCK)<0)
{
perror("fcntl");
exit(1);
}
while(1)
{
sin_size=sizeof(structsockaddr_in);
if((client_fd=accept(sockfd,
(structsockaddr*)&client_sockaddr,&sin_size))<0)
{
perror("accept");
exit(1);
}
if((recvbytes=recv(client_fd,buf,BUFFER_SIZE,0))<0)
{
perror("recv");
exit(1);
}
printf("Receivedamessage:%s\n",buf);
}/*while*/
close(client_fd);
exit(1);
}
運(yùn)行該程序,結(jié)果如下所示:
$./net_fcntl
Listening....
accept:Resourcetemporarilyunavailable
可以看到,當(dāng)accept()的資源不可用(沒(méi)有任何未處理的等待連接的請(qǐng)求)時(shí),程序就會(huì)自動(dòng)返回。
2.select()使用fcntl()函數(shù)雖然可以實(shí)現(xiàn)非阻塞I/O或信號(hào)驅(qū)動(dòng)I/O,但在實(shí)際使用時(shí)往往會(huì)對(duì)資源是否準(zhǔn)備完畢進(jìn)行循環(huán)測(cè)試,這樣就大大增加了不必要的CPU資源的占用。在這里可以使用select()函數(shù)來(lái)解決這個(gè)問(wèn)題,同時(shí),使用select()函數(shù)還可以設(shè)置等待的時(shí)間,可以說(shuō)功能更加強(qiáng)大。下面是使用select()函數(shù)的服務(wù)器端源代碼??蛻?hù)端程序基本上與10.2.3小節(jié)中的例子相同,僅加入一行sleep()函數(shù),使得客戶(hù)端進(jìn)程等待幾秒鐘才結(jié)束。
/*net_select.c*/
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/time.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<netinet/in.h>
#definePORT4321
#defineMAX_QUE_CONN_NM5
#defineMAX_SOCK_FDFD_SETSIZE
#defineBUFFER_SIZE1024
intmain()
{
structsockaddr_inserver_sockaddr,client_sockaddr;
intsin_size,count;
fd_setinset,tmp_inset;
intsockfd,client_fd,fd;
charbuf[BUFFER_SIZE];
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
server_sockaddr.sin_family=AF_INET;
server_sockaddr.sin_port=htons(PORT);
server_sockaddr.sin_addr.s_addr=INADDR_ANY;
bzero(&(server_sockaddr.sin_zero),8);
inti=1;/*允許重復(fù)使用本地地址與套接字進(jìn)行綁定*/
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i));
if(bind(sockfd,(structsockaddr*)&server_sockaddr,
sizeof(structsockaddr))==-1)
{
perror("bind");
exit(1);
}
if(listen(sockfd,MAX_QUE_CONN_NM)==-1)
{
perror("listen");
exit(1);
}
printf("listening....\n");
/*將調(diào)用socket()函數(shù)的描述符作為文件描述符*/
FD_ZERO(&inset);
FD_SET(sockfd,&inset);
while(1)
{
tmp_inset=inset;
sin_size=sizeof(structsockaddr_in);
memset(buf,0,sizeof(buf));
/*調(diào)用select()函數(shù)*/
if(!(select(MAX_SOCK_FD,&tmp_inset,NULL,NULL,NULL)>0))
{
perror("select");
}
for(fd=0;fd<MAX_SOCK_FD;fd++)
{
if(FD_ISSET(fd,&tmp_inset)>0)
{
if(fd==sockfd)
{/*服務(wù)端接收客戶(hù)端的連接請(qǐng)求*/
if((client_fd=accept(sockfd,
(structsockaddr*)&client_sockaddr,&sin_size))==-1)
{
perror("accept");
exit(1);
}
FD_SET(client_fd,&inset);
printf("Newconnectionfrom%d(socket)\n",client_fd);
}
else/*處理從客戶(hù)端發(fā)來(lái)的消息*/
{
if((count=recv(client_fd,buf,BUFFER_SIZE,0))>0)
{
printf("Receivedamessagefrom%d:%s\n",
client_fd,buf);
}
else
{
close(fd);
FD_CLR(fd,&inset);
printf("Client%d(socket)hasleft\n",fd);
}
}
}/*endofifFD_ISSET*/
}/*endofforfd*/
}/*endifwhilewhile*/
close(sockfd);
exit(0);
}
運(yùn)行該程序時(shí),可以先啟動(dòng)服務(wù)器端,再反復(fù)運(yùn)行客戶(hù)端程序(這里啟動(dòng)兩個(gè)客戶(hù)端進(jìn)程)即可,服務(wù)器端運(yùn)行結(jié)果如下所示:
$./server
listening....
Newconnectionfrom4(socket)/*接受第一個(gè)客戶(hù)端的連接請(qǐng)求*/
Receivedamessagefrom4:Hello,First!/*接收第一個(gè)客戶(hù)端發(fā)送的數(shù)據(jù)*/
Newconnectionfrom5(socket)/*接受第二個(gè)客戶(hù)端的連接請(qǐng)求*/
Receivedamessagefrom5:Hello,Second!/*接收第二個(gè)客戶(hù)端發(fā)送的數(shù)據(jù)*/
Client4(socket)hasleft/*檢測(cè)到第一個(gè)客戶(hù)端離線了*/
Client5(socket)hasleft/*檢測(cè)到第二個(gè)客戶(hù)端離線了*/
$./clientlocalhostHello,First!&./clientlocalhostHello,Second