程序是從網上的一個epoll例子修改而來的sql
epoll tcp服務端 epoll_server.c:api
/*
gcc -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -I${ORACLE_HOME}/rdbms/public -I${ORACLE_HOME}/rdbms/demo -L${ORACLE_HOME}/lib -lclntsh -pthread -o epoll_server epoll_server.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
//#include <openssl/engines/ssl.h>
//#include <openssl/err.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/resource.h>服務器
//#include "oci_api.h"多線程
#define MAXBUF 1024
#define MAXEPOLLSIZE 10000socket
/*
setnonblocking - 設置句柄爲非阻塞方式
*/
int setnonblocking(int sockfd)
{
if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK) == -1) {
return -1;
}
return 0;
}tcp
/*
handle_message - 處理每一個 socket 上的消息收發
*/
int handle_message(int new_fd)
{
char buf[MAXBUF + 1];
char sendbuf[MAXBUF+1];
int len;
/* 開始處理每一個新鏈接上的數據收發 */
bzero(buf, MAXBUF + 1);
/* 接收客戶端的消息 */
len = recv(new_fd, buf, MAXBUF, 0);
if (len > 0){線程
printf
("%d接收消息成功:'%s',共%d個字節的數據/n",
new_fd, buf, len);
/*server
char *s1="insert";
char *s2="select";
char *s3="delete";進程
if(!strncmp(s1,buf,6))
oci_insert(buf,sendbuf);
else if(!strncmp(s2,buf,6))
oci_select(buf,sendbuf);
else if(!strncmp(s3,buf,6))
oci_delete(buf,sendbuf);
else
sprintf(sendbuf,"input sql is error!/n");事件
len=send(new_fd,sendbuf,strlen(sendbuf),0);
if(len<0)
printf("消息發送失敗!錯誤代碼是%d,錯誤信息是'%s'/n",
errno, strerror(errno));
*/
}
else {
if (len < 0)
printf
("消息接收失敗!錯誤代碼是%d,錯誤信息是'%s'/n",
errno, strerror(errno));
else
printf
("客戶端%d退出!/n",new_fd);
//close(new_fd);
return -1;
}
/* 處理每一個新鏈接上的數據收發結束 */
return len;
}
/************關於本文檔********************************************
*filename: epoll-server.c
*purpose: 演示epoll處理海量socket鏈接的方法
*wrote by: zhoulifa(zhoulifa@163.com) 周立發(http://zhoulifa.bokee.com)
Linux愛好者 Linux知識傳播者 SOHO族 開發者 最擅長C語言
*date time:2007-01-31 21:00
*Note: 任何人能夠任意複製代碼並運用這些文檔,固然包括你的商業用途
* 但請遵循GPL
*Thanks to:Google
*Hope:但願愈來愈多的人貢獻本身的力量,爲科學技術發展出力
* 科技站在巨人的肩膀上進步更快!感謝有開源前輩的貢獻!
*********************************************************************/
int main(int argc, char **argv)
{
int listener, new_fd, nfds, n, ret;
struct epoll_event ev;
int kdpfd, curfds;
socklen_t len;
struct sockaddr_in my_addr, their_addr;
unsigned int myport, lisnum;
struct epoll_event events[MAXEPOLLSIZE];
struct rlimit rt;
if (argc>1)
myport = atoi(argv[1]);
else
myport = 1234;
if (argc>2)
lisnum = atoi(argv[2]);
else
lisnum = 10;
/* 設置每一個進程容許打開的最大文件數 */
rt.rlim_max = rt.rlim_cur = MAXEPOLLSIZE;
if (setrlimit(RLIMIT_NOFILE, &rt) == -1) {
perror("setrlimit");
exit(1);
}
else printf("設置系統資源參數成功!/n");
/* 開啓 socket 監聽 */
if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
} else
printf("socket 建立成功!/n");
/*設置socket屬性,端口能夠重用*/
int opt=SO_REUSEADDR;
setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
/*設置socket爲非阻塞模式*/
setnonblocking(listener);
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = PF_INET;
my_addr.sin_port = htons(myport);
if (argc>3)
my_addr.sin_addr.s_addr = inet_addr(argv[3]);
else
my_addr.sin_addr.s_addr = INADDR_ANY;
if (bind
(listener, (struct sockaddr *) &my_addr, sizeof(struct sockaddr))
== -1) {
perror("bind");
exit(1);
} else
printf("IP 地址和端口綁定成功/n");
if (listen(listener, lisnum) == -1) {
perror("listen");
exit(1);
} else
printf("開啓服務成功!/n");
/* 建立 epoll 句柄,把監聽 socket 加入到 epoll 集合裏 */
kdpfd = epoll_create(MAXEPOLLSIZE);
len = sizeof(struct sockaddr_in);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = listener;
if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, listener, &ev) < 0) {
fprintf(stderr, "epoll set insertion error: fd=%d/n", listener);
return -1;
} else
printf("監聽 socket 加入 epoll 成功!/n");
curfds = 1;
while (1) {
/* 等待有事件發生 */
nfds = epoll_wait(kdpfd, events, curfds, -1);
if (nfds == -1) {
perror("epoll_wait");
break;
}
/* 處理全部事件 */
for (n = 0; n < nfds; ++n) {
if (events[n].data.fd == listener) {
new_fd = accept(listener, (struct sockaddr *) &their_addr,
&len);
if (new_fd < 0) {
perror("accept");
continue;
} else
printf("有鏈接來自於: %s:%d, 分配的 socket 爲:%d/n", inet_ntoa(their_addr.sin_addr), ntohs(their_addr.sin_port), new_fd);
setnonblocking(new_fd);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = new_fd;
if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, new_fd, &ev) < 0) {
fprintf(stderr, "把 socket '%d' 加入 epoll 失敗!%s/n",
new_fd, strerror(errno));
return -1;
}
curfds++;
} else {
ret = handle_message(events[n].data.fd);
if (ret < 1 && errno != 11) {
if(epoll_ctl(kdpfd, EPOLL_CTL_DEL, events[n].data.fd,
&ev) < 0)
{
fprintf(stderr, "把 socket '%d' 從 epoll 刪除失敗!%s/n",
events[n].data.fd, strerror(errno));
}
curfds--;
close(events[n].data.fd);
}
}
}
}
close(listener);
return 0;
}
tcp客戶端client.c:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/select.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#define MAXLINE 1024
char sendhead[MAXLINE];
int main(int argc , char* argv[])
{
int sockfd;
struct sockaddr_in servaddr;
char *info="cxt";
int maxfdp1, stdineof;
fd_set rset;
char recvbuf[MAXLINE],tmp[128],sendbuf[MAXLINE];
int n,len,fd;
if(argc!=3){
printf("useage:client address port ");
exit(0);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1 )
{
perror("socket");
exit(1);
}
printf("%s connect server/n",info);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(atoi(argv[2]));
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
if( ( connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) )<0)
{
perror("connect");
exit(1);
}
send(sockfd,info,strlen(info),0);
//FD_ZERO(&rset);
//FD_SET(sockfd, &rset);
//FD_SET(0, &rset);
//maxfdp1=sockfd+1;
for ( ; ; ) {
FD_ZERO(&rset);
FD_SET(sockfd, &rset);
FD_SET(0, &rset);
maxfdp1=sockfd+1;
if( ( select(maxfdp1, &rset, NULL, NULL, NULL) )<=0){
perror("select");
}else{
if (FD_ISSET(0,&rset)){
fgets(sendbuf, MAXLINE, stdin);
n=send(sockfd,sendbuf,strlen(sendbuf)-1,0);
if(n>0)
printf("send: %s",sendbuf);
else
printf("send: %s error,the erro cause is %s:%s/n",sendbuf,errno,strerror(errno));
bzero(sendbuf,strlen(sendbuf));
}
if (FD_ISSET(sockfd, &rset)) { /* socket is readable */
n=recv(sockfd, recvbuf, MAXLINE,0) ;
if(n<0) {
perror("str_cli: server terminated prematurely");
}else if(n==0)
{
printf("sever shutdown!");
exit(-1);
}
//recvbuf
recvbuf[n]='/0';
printf("receive :%s/n",recvbuf);
fflush(stdout);
bzero(recvbuf,strlen(recvbuf));
}
}
}
exit(0);
}
udp +epoll+多線程服務器 udp_epoll_server.c:
/*
gcc -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -I${ORACLE_HOME}/rdbms/public -I${ORACLE_HOME}/rdbms/demo -L${ORACLE_HOME}/lib -lclntsh -pthread -o udp_epoll_server udp_epoll_server.c -lpthread
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
//#include <openssl/ssl.h>
//#include <openssl/err.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <pthread.h>
#include <assert.h>
//#include "oci_api.h"
#define MAXBUF 1024
#define MAXEPOLLSIZE 100
/*
setnonblocking – 設置句柄爲非阻塞方式
*/
int setnonblocking(int sockfd)
{
if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK) == -1)
{
return -1;
}
return 0;
}
/*
pthread_handle_message – 線程處理 socket 上的消息收發
*/
void* pthread_handle_message(int* sock_fd)
{
char recvbuf[MAXBUF + 1];
char sendbuf[MAXBUF+1];
int ret;
int new_fd;
struct sockaddr_in client_addr;
socklen_t cli_len=sizeof(client_addr);
new_fd=*sock_fd;
/* 開始處理每一個新鏈接上的數據收發 */
bzero(recvbuf, MAXBUF + 1);
bzero(sendbuf, MAXBUF + 1);
/* 接收客戶端的消息 */
ret = recvfrom(new_fd, recvbuf, MAXBUF, 0, (struct sockaddr *)&client_addr, &cli_len);
if (ret > 0)
{
printf("socket %d 接收到來自:%s:%d的消息成功:’%s’,共%d個字節的數據/n",
new_fd, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), recvbuf, ret);
/* char *s1="insert";
char *s2="select";
char *s3="delete";
if(!strncmp(s1,recvbuf,6))
oci_insert(recvbuf,sendbuf);
else if(!strncmp(s2,recvbuf,6))
oci_select(recvbuf,sendbuf);
else if(!strncmp(s3,recvbuf,6))
oci_delete(recvbuf,sendbuf);
else
sprintf(sendbuf,"input sql is error!/n");
ret = sendto(new_fd, sendbuf, strlen(sendbuf), 0, (struct sockaddr *)&client_addr, cli_len);
if(ret<0)
printf("消息發送失敗!錯誤代碼是%d,錯誤信息是'%s'/n",
errno, strerror(errno));
*/
}
else
{
printf("消息接收失敗!錯誤代碼是%d,錯誤信息是’%s’/n",
errno, strerror(errno));
}
/* 處理每一個新鏈接上的數據收發結束 */
//printf("pthread exit!");
fflush(stdout);
pthread_exit(NULL);
}
int main(int argc, char **argv)
{
int listener, kdpfd, nfds, n, curfds;
socklen_t len;
struct sockaddr_in my_addr, their_addr;
unsigned int myport;
struct epoll_event ev;
struct epoll_event events[MAXEPOLLSIZE];
struct rlimit rt;
myport = 1234;
pthread_t thread;
pthread_attr_t attr;
/* 設置每一個進程容許打開的最大文件數 */
rt.rlim_max = rt.rlim_cur = MAXEPOLLSIZE;
if (setrlimit(RLIMIT_NOFILE, &rt) == -1)
{
perror("setrlimit");
exit(1);
}
else
{
printf("設置系統資源參數成功!/n");
}
/* 開啓 socket 監聽 */
if ((listener = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket 建立失敗!");
exit(1);
}
else
{
printf("socket 建立成功!/n");
}
/*設置socket屬性,端口能夠重用*/
int opt=SO_REUSEADDR;
setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
setnonblocking(listener);
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = PF_INET;
my_addr.sin_port = htons(myport);
my_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(listener, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1)
{
perror("bind");
exit(1);
}
else
{
printf("IP 地址和端口綁定成功/n");
}
/* 建立 epoll 句柄,把監聽 socket 加入到 epoll 集合裏 */
kdpfd = epoll_create(MAXEPOLLSIZE);
len = sizeof(struct sockaddr_in);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = listener;
if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, listener, &ev) < 0)
{
fprintf(stderr, "epoll set insertion error: fd=%d/n", listener);
return -1;
}
else
{
printf("監聽 socket 加入 epoll 成功!/n");
}
while (1)
{
/* 等待有事件發生 */
nfds = epoll_wait(kdpfd, events, 10000, -1);
if (nfds == -1)
{
perror("epoll_wait");
break;
}
/* 處理全部事件 */
for (n = 0; n < nfds; ++n)
{
if (events[n].data.fd == listener)
{
/*初始化屬性值,均設爲默認值*/
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
/* 設置線程爲分離屬性*/
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if(pthread_create(&thread,&attr,(void*)pthread_handle_message,(void*)&(events[n].data.fd)))
{
perror("pthread_creat error!");
exit(-1);
}
}
}
}
close(listener);
return 0;
}
udp客戶端 udp_client.c:
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXLINE 1024
#define SERV_PORT 1234
void do_cli(FILE *fp, int sockfd, struct sockaddr *pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
/* connect to server */
if(connect(sockfd, (struct sockaddr *)pservaddr, servlen) == -1)
{
perror("connect error");
exit(1);
}
while(fgets(sendline, MAXLINE, fp) != NULL)
{
/* read a line and send to server */
write(sockfd, sendline, strlen(sendline)-1);
/* receive data from server */
n = read(sockfd, recvline, MAXLINE);
if(n == -1)
{
perror("read error");
exit(1);
}
recvline[n] = '/0'; /* terminate string */
//fputs(recvline, stdout);
printf("receive:%s/n",recvline);
fflush(stdout);
}
}
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
/* check args */
if(argc != 2)
{
printf("usage: udpclient <IPaddress>/n");
exit(1);
}
/* init servaddr */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
{
printf("[%s] is not a valid IPaddress/n", argv[1]);
exit(1);
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
do_cli(stdin, sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
return 0; }