網(wǎng)絡(luò)編程——C++實(shí)現(xiàn)socket通信(TCP)高并發(fā)之epoll模式_tcp通信c++ 多客戶端epoll_n大橘為重n的博客-CSDN博客
網(wǎng)絡(luò)編程——C++實(shí)現(xiàn)socket通信(TCP)高并發(fā)之select模式_n大橘為重n的博客-CSDN博客
server.cpp?文章來源:http://www.zghlxwxcb.cn/news/detail-653941.html
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <ctype.h>
#include <sys/epoll.h> //epoll頭文件
#define MAXSIZE 1024
#define IP_ADDR "127.0.0.1"
#define IP_PORT 8888
int main()
{
int i_listenfd, i_connfd;
struct sockaddr_in st_sersock;
char msg[MAXSIZE];
int nrecvSize = 0;
struct epoll_event ev, events[MAXSIZE];
int epfd, nCounts; //epfd:epoll實(shí)例句柄, nCounts:epoll_wait返回值
if((i_listenfd = socket(AF_INET, SOCK_STREAM, 0) ) < 0) //建立socket套接字
{
printf("socket Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
memset(&st_sersock, 0, sizeof(st_sersock));
st_sersock.sin_family = AF_INET; //IPv4協(xié)議
st_sersock.sin_addr.s_addr = htonl(INADDR_ANY); //INADDR_ANY轉(zhuǎn)換過來就是0.0.0.0,泛指本機(jī)的意思,也就是表示本機(jī)的所有IP,因?yàn)橛行C(jī)子不止一塊網(wǎng)卡,多網(wǎng)卡的情況下,這個就表示所有網(wǎng)卡ip地址的意思。
st_sersock.sin_port = htons(IP_PORT);
if(bind(i_listenfd,(struct sockaddr*)&st_sersock, sizeof(st_sersock)) < 0) //將套接字綁定IP和端口用于監(jiān)聽
{
printf("bind Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
if(listen(i_listenfd, 20) < 0) //設(shè)定可同時排隊的客戶端最大連接個數(shù)
{
printf("listen Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
if((epfd = epoll_create(MAXSIZE)) < 0) //創(chuàng)建epoll實(shí)例
{
printf("epoll_create Error: %s (errno: %d)\n", strerror(errno), errno);
exit(-1);
}
ev.events = EPOLLIN;
ev.data.fd = i_listenfd;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, i_listenfd, &ev) < 0)
{
printf("epoll_ctl Error: %s (errno: %d)\n", strerror(errno), errno);
exit(-1);
}
printf("======waiting for client's request======\n");
//準(zhǔn)備接受客戶端連接
while(1)
{
if((nCounts = epoll_wait(epfd, events, MAXSIZE, -1)) < 0)
{
printf("epoll_ctl Error: %s (errno: %d)\n", strerror(errno), errno);
exit(-1);
}
else if(nCounts == 0)
{
printf("time out, No data!\n");
}
else
{
for(int i = 0; i < nCounts; i++)
{
int tmp_epoll_recv_fd = events[i].data.fd;
if(tmp_epoll_recv_fd == i_listenfd) //有客戶端連接請求
{
if((i_connfd = accept(i_listenfd, (struct sockaddr*)NULL, NULL)) < 0) //阻塞等待客戶端連接
{
printf("accept Error: %s (errno: %d)\n", strerror(errno), errno);
// continue;
}
else
{
printf("Client[%d], welcome!\n", i_connfd);
}
ev.events = EPOLLIN;
ev.data.fd = i_connfd;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, i_connfd, &ev) < 0)
{
printf("epoll_ctl Error: %s (errno: %d)\n", strerror(errno), errno);
exit(-1);
}
}
else //若是已連接的客戶端發(fā)來數(shù)據(jù)請求
{
//接受客戶端發(fā)來的消息并作處理(小寫轉(zhuǎn)大寫)后回寫給客戶端
memset(msg, 0 ,sizeof(msg));
if((nrecvSize = read(tmp_epoll_recv_fd, msg, MAXSIZE)) < 0)
{
printf("read Error: %s (errno: %d)\n", strerror(errno), errno);
continue;
}
else if( nrecvSize == 0) //read返回0代表對方已close斷開連接。
{
printf("client has disconnected!\n");
epoll_ctl(epfd, EPOLL_CTL_DEL, tmp_epoll_recv_fd, NULL);
close(tmp_epoll_recv_fd); //
continue;
}
else
{
printf("recvMsg:%s", msg);
for(int i=0; msg[i] != '\0'; i++)
{
msg[i] = toupper(msg[i]);
}
if(write(tmp_epoll_recv_fd, msg, strlen(msg)+1) < 0)
{
printf("write Error: %s (errno: %d)\n", strerror(errno), errno);
}
}
}
}
}
}//while
close(i_listenfd);
close(epfd);
return 0;
}
?client.cpp文章來源地址http://www.zghlxwxcb.cn/news/detail-653941.html
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <signal.h>
#include <arpa/inet.h>
#define MAXSIZE 1024
#define IP_ADDR "127.0.0.1"
#define IP_PORT 8888
int i_sockfd = -1;
void SigCatch(int sigNum) //信號捕捉函數(shù)(捕獲Ctrl+C)
{
if(i_sockfd != -1)
{
close(i_sockfd);
}
printf("Bye~! Will Exit...\n");
exit(0);
}
int main()
{
struct sockaddr_in st_clnsock;
char msg[1024];
int nrecvSize = 0;
signal(SIGINT, SigCatch); //注冊信號捕獲函數(shù)
if((i_sockfd = socket(AF_INET, SOCK_STREAM, 0) ) < 0) //建立套接字
{
printf("socket Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
memset(&st_clnsock, 0, sizeof(st_clnsock));
st_clnsock.sin_family = AF_INET; //IPv4協(xié)議
//IP地址轉(zhuǎn)換(直接可以從物理字節(jié)序的點(diǎn)分十進(jìn)制 轉(zhuǎn)換成網(wǎng)絡(luò)字節(jié)序)
if(inet_pton(AF_INET, IP_ADDR, &st_clnsock.sin_addr) <= 0)
{
printf("inet_pton Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
st_clnsock.sin_port = htons(IP_PORT); //端口轉(zhuǎn)換(物理字節(jié)序到網(wǎng)絡(luò)字節(jié)序)
if(connect(i_sockfd, (struct sockaddr*)&st_clnsock, sizeof(st_clnsock)) < 0) //主動向設(shè)置的IP和端口號的服務(wù)端發(fā)出連接
{
printf("connect Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
printf("======connect to server, sent data======\n");
while(1) //循環(huán)輸入,向服務(wù)端發(fā)送數(shù)據(jù)并接受服務(wù)端返回的數(shù)據(jù)
{
fgets(msg, MAXSIZE, stdin);
printf("will send: %s", msg);
if(write(i_sockfd, msg, MAXSIZE) < 0) //發(fā)送數(shù)據(jù)
{
printf("write Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
memset(msg, 0, sizeof(msg));
if((nrecvSize = read(i_sockfd, msg, MAXSIZE)) < 0) //接受數(shù)據(jù)
{
printf("read Error: %s (errno: %d)\n", strerror(errno), errno);
}
else if(nrecvSize == 0)
{
printf("Service Close!\n");
}
else
{
printf("Server return: %s\n", msg);
}
}
return 0;
}
到了這里,關(guān)于基于epoll的TCP服務(wù)器端(C++)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!