【技术分享】【原创】基于linux的网络高级编程
0赞上面介绍了Linux的基础编程,下面在介绍Linux的网络高级编程。
由于在前面介绍的函数如connet、recv、send都是阻塞性函数,若资源没有准备好,则调用该函数的进程将进入休眠状态,这样无法实现I/O多路复用了,下面介绍两种I/O多路复用的解决方案。
1、fcntl函数实现(非阻塞方式)
实例fcntl.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 3333
#define BACKLOG 10
#define MAX_CONNECTED_NO 10
#define MAXDATASIZE 100
int main()
{
struct sockaddr_in server_sockaddr,client_sockaddr;
int sin_size,recvbytes,flags;
int sockfd,client_fd;
char buf[MAXDATASIZE];
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
printf("socket success!,sockfd=%d\n",sockfd);
server_sockaddr.sin_family=AF_INET;
server_sockaddr.sin_port=htons(SERVPORT);
server_sockaddr.sin_addr.s_addr=INADDR_ANY;
bzero(&(server_sockaddr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))==-1){
perror("bind");
exit(1);
}
printf("bind success!\n");
if(listen(sockfd,BACKLOG)==-1){
perror("listen");
exit(1);
}
printf("listening....\n");
if((flags=fcntl( sockfd, F_SETFL, 0))<0)
perror("fcntl F_SETFL");
flags |= O_NONBLOCK;
if(fcntl( sockfd, F_SETFL,flags)<0)
perror("fcntl");
while(1){
sin_size=sizeof(struct sockaddr_in);
if((client_fd=accept(sockfd,(struct sockaddr*)&client_sockaddr,&sin_size))==-1){
perror("accept");
exit(1);
}
if((recvbytes=recv(client_fd,buf,MAXDATASIZE,0))==-1){
perror("recv");
exit(1);
}
if(read(client_fd,buf,MAXDATASIZE)<0){
perror("read");
exit(1);
}
printf("received a connection :%s",buf);
close(client_fd);
exit(1);
}/*while*/
}
2、select函数实现
实例select_socket.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 3333
#define BACKLOG 10
#define MAX_CONNECTED_NO 10
#define MAXDATASIZE 100
int main()
{
struct sockaddr_in server_sockaddr,client_sockaddr;
int sin_size,recvbytes;
fd_set readfd;
fd_set writefd;
int sockfd,client_fd;
char buf[MAXDATASIZE];
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
printf("socket success!,sockfd=%d\n",sockfd);
server_sockaddr.sin_family=AF_INET;
server_sockaddr.sin_port=htons(SERVPORT);
server_sockaddr.sin_addr.s_addr=INADDR_ANY;
bzero(&(server_sockaddr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))==-1){
perror("bind");
exit(1);
}
printf("bind success!\n");
if(listen(sockfd,BACKLOG)==-1){
perror("listen");
exit(1);
}
printf("listening....\n");
FD_ZERO(&readfd);
FD_SET(sockfd,&readfd);
while(1){
sin_size=sizeof(struct sockaddr_in);
if(select(MAX_CONNECTED_NO,&readfd,NULL,NULL,(struct timeval *)0)>0){
if(FD_ISSET(sockfd,&readfd)>0){
if((client_fd=accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size))==-1){
perror("accept");
exit(1);
}
if((recvbytes=recv(client_fd,buf,MAXDATASIZE,0))==-1){
perror("recv");
exit(1);
}
if(read(client_fd,buf,MAXDATASIZE)<0){
perror("read");
exit(1);
}
printf("received a connection :%s",buf);
}/*if*/
close(client_fd);
}/*select*/
}/*while*/
}
对这2个例程掌握了,对以后的编程就会如虎添翼。