客戶端從標準輸入讀入一行,傳送到服務端
服務端從網路讀取一行,然後輸出到客戶端
客戶端收到服務端的響應,輸出這一行到標準輸出
**如下:
#include #include /***如下:basic system data types
*/#include /*
basic socket definitions
*/#include in.h> /*
sockaddr_in{} and other internet defns
*/#include /*
inet(3) functions
*/#include /*
epoll function
*/#include /*
nonblocking
*/#include /*
setrlimit
*/#include #include #include #include
#define maxepollsize 10000
#define maxline 10240
int handle(int connfd);
int setnonblocking(int sockfd)
return0;}
int main(int argc, char **argv)
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = af_inet;
servaddr.sin_addr.s_addr = htonl (inaddr_any);
servaddr.sin_port = htons (servport);
listenfd = socket(af_inet, sock_stream, 0);
if (listenfd == -1)
int opt = 1;
setsockopt(listenfd, sol_socket, so_reuseaddr, &opt, sizeof(opt));
if (setnonblocking(listenfd) < 0)
if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) == -1)
if (listen(listenfd, listenq) == -1)
/*建立 epoll 控制代碼,把監聽 socket 加入到 epoll 集合裡
*/ kdpfd = epoll_create(maxepollsize);
ev.events = epollin | epollet;
ev.data.fd = listenfd;
if (epoll_ctl(kdpfd, epoll_ctl_add, listenfd, &ev) < 0)
curfds = 1;
printf("
epollserver startup,port %d, max connection is %d, backlog is %d\n
", servport, maxepollsize, listenq);
for (;;)
/*處理所有事件
*/for (n = 0; n < nfds; ++n)
sprintf(buf, "
accept form %s:%d\n
", inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);
printf("
%d:%s
", ++acceptcount, buf);
if (curfds >= maxepollsize)
if (setnonblocking(connfd) < 0)
ev.events = epollin | epollet;
ev.data.fd = connfd;
if (epoll_ctl(kdpfd, epoll_ctl_add, connfd, &ev) < 0)
curfds++;
continue;
} //
處理客戶端請求
if (handle(events[n].data.fd) < 0) }}
close(listenfd);
return0;}
int handle(int connfd)
if (nread < 0)
write(connfd, buf, nread);//
響應客戶端
return
0;}
#include #include /*編譯和啟動伺服器:basic system data types
*/#include /*
basic socket definitions
*/#include in.h> /*
sockaddr_in{} and other internet defns
*/#include /*
inet(3) functions
*/#include /*
gethostbyname function
*/#include #include #include #include
#define maxline 1024
void handle(int connfd);
int main(int argc, char **argv)
if (argc == 3)
if (argc > 3)
connfd = socket(af_inet, sock_stream, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = af_inet;
servaddr.sin_port = htons(servport);
inet_pton(af_inet, servinetaddr, &servaddr.sin_addr);
if (connect(connfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
printf("
welcome to echoclient\n
"); handle(connfd); /*
do it all
*/ close(connfd);
printf("
exit\n
"); exit(0);
}void handle(int sockfd)
/*//也可以不用標準庫的緩衝流,直接使用系統函式無快取操作
if (read(stdin_fileno, sendline, maxline) == 0)
*/ n = write(sockfd, sendline, strlen(sendline));
n = read(sockfd, recvline, maxline);
if (n == 0)
write(stdout_fileno, recvline, n);
//如果用標準庫的快取流輸出有時會出現問題
//fputs(recvline, stdout);}}
gcc echoserver.c -o echoserver編譯和啟動客戶端./echoserver
gcc echoclient.c -o echoclient./echoclient
EPOLL的簡單使用
epoll是linux下的乙個處理多路i o復用的機制,基於事件通知,能夠高效的處理多個socket連線。使用epoll,基本的函式只有三個 1 建立 epoll create 2 控制 epoll ctl 3 監聽 epoll wait 2.1 建立 函式原型 int epoll create i...
簡單的epoll模型
水平觸發模式 include include include include include include include include include int main int argc,const char ar struct sockaddr in serv addr socklen t ...
epoll簡單說明
首先我們來定義流的概念,乙個流可以是檔案,socket,pipe等等可以進行i o操作的核心物件。不管是檔案,還是套接字,還是管道,我們都可以把他們看作流。之後我們來討論i o的操作,通過read,我們可以從流中讀入資料 通過write,我們可以往流寫入資料。現在假定乙個情形,我們需要從流中讀資料,...