linux 网络编程 epoll
service代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <string.h>
#define LISTEN_PORT 8000
int main()
{
// create socket
int server_socket = socket(AF_INET,SOCK_STREAM,0);
if (server_socket != -1)
{
//bind
struct sockaddr_in server_addr;
socklen_t len = sizeof(struct sockaddr_in);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(LISTEN_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY; //监听本机全部地址
int nResult = bind(server_socket,(struct sockaddr*)&server_addr,len);
if (nResult != -1)
{
//listen
nResult = listen(server_socket,256);
if (nResult != -1)
{
//epoll
int epfd = epoll_create(10);
if (epfd < 0)
{
perror("epoll_create error \n");
close(server_socket);
exit(-1);
}
struct epoll_event evt;
evt.events = EPOLLIN;
evt.data.fd = server_socket;
nResult = epoll_ctl(epfd,EPOLL_CTL_ADD,server_socket,&evt);
if (nResult < 0)
{
perror("epoll_ctl error \n");
close(server_socket);
exit(-1);
}
while (1)
{
//accept
struct epoll_event event_results[20];
int nRet_Count = epoll_wait(epfd,event_results,20,-1);
for (size_t i = 0; i < nRet_Count; i++)
{
if ((event_results[i].events & EPOLLIN))
{
if (event_results[i].data.fd == server_socket)
{
// client connect
struct sockaddr_in client_addr;
socklen_t nClient_socklen = sizeof(struct sockaddr_in);
int client_sock = accept(server_socket,(struct sockaddr *)&client_addr,&nClient_socklen);
if (client_sock != -1)
{
struct epoll_event client_evn;
client_evn.events = EPOLLIN;
client_evn.data.fd = client_sock;
//add client socket
epoll_ctl(epfd,EPOLL_CTL_ADD,client_sock,&client_evn);
}
}
else
{
//client send data
char buf[BUFSIZ];
int nSize = read(event_results[i].data.fd,buf,BUFSIZ);
if (nSize <= 0)
{
//client close socket
epoll_ctl(epfd,EPOLL_CTL_DEL,event_results[i].data.fd,&event_results[i]);
close(event_results[i].data.fd);
}
else if (nSize > 0)
{
for (size_t i = 0; i < nSize; i++)
{
buf[i] = toupper(buf[i]);
}
write(event_results[i].data.fd,buf,nSize);
}
}
}
}
}
}
}
//close
close(server_socket);
}
return 0;
}
client 代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <string.h>
#define LISTEN_PORT 8000
#define REQ_HOST "127.0.0.1"
int main()
{
int dest_socket = socket(AF_INET,SOCK_STREAM,0);
if (dest_socket != -1)
{
struct sockaddr_in dest_addr;
socklen_t len = sizeof(struct sockaddr_in);
dest_addr.sin_addr.s_addr = inet_addr(REQ_HOST);
dest_addr.sin_port = htons(LISTEN_PORT);
dest_addr.sin_family = AF_INET;
int nResult = connect(dest_socket,(struct sockaddr*)&dest_addr,len);
if (nResult != -1)
{
while (1)
{
char buf [BUFSIZ];
bzero(buf,BUFSIZ);
printf("please input send data:\n");
scanf("%s",buf);
if (!strcmp(buf,"quit"))
{
break;
}
write(dest_socket,buf,strlen(buf));
int nSize = read(dest_socket,buf,BUFSIZ);
if (nSize <= 0)
{
break;
}
else
{
buf[nSize] = '\0';
printf("%s\n",buf);
}
}
}
//close
close(dest_socket);
}
return 0;
}