一、客户端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
extern int errno;
#define SERVERPORT 1001
int main(int argc, char const *argv[])
{
int clifd;
const char serverIp[] = "10.243.0.160";
struct sockaddr_in cliaddr_in;
char sendBuf[1024], recvBuf[1024];
if((clifd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
fprintf(stderr, "创建套接字失败,%s\n", strerror(errno));
exit(errno);
}
cliaddr_in.sin_family = AF_INET;
cliaddr_in.sin_addr.s_addr = inet_addr(serverIp);
cliaddr_in.sin_port = htons(SERVERPORT);
if(connect(clifd, (struct sockaddr *)&cliaddr_in, sizeof(struct sockaddr)) == -1){
fprintf(stderr,"请求连接服务器失败, %s\n", strerror(errno));
exit(errno);
}
strcpy(sendBuf, "hi,hi, severs!\n");
if(send(clifd, sendBuf, 1024, 0) == -1){
fprintf(stderr, "send message error:(, %s\n", strerror(errno));
exit(errno);
}
recv(clifd, recvBuf, 1024, 0);
printf("get message from server : %s\n", recvBuf);
while(1){
fgets(sendBuf, 1024, stdin);
printf("sendBuf is %s\n", sendBuf);
if(send(clifd, sendBuf, 1024, 0) == -1){
fprintf(stderr, "send message error:(, %s\n", strerror(errno));
}
recv(clifd, recvBuf, 1024, 0);
printf("get message from server : %s\n", recvBuf);
}
close(clifd);
return 0;
}
二、服务端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define MAXLINE 50
#define OPEN_MAX 100
#define LISTENQ 20
#define SERV_PORT 5000
#define INFTIM 1000
void setnonblocking(int sock)
{
int opts;
opts = fcntl(sock, F_GETFL);
if (opts < 0) {
perror("fcntl(sock,GETFL)");
exit(1);
}
opts = opts | O_NONBLOCK;
if (fcntl(sock, F_SETFL, opts) < 0) {
perror("fcntl(sock,SETFL,opts)");
exit(1);
}
}
int main(int argc, char *argv[])
{
int i, maxi, connfd, sockfd, nfds, portnumber = 1001;
ssize_t n;
char line[MAXLINE];
socklen_t clilen;
struct epoll_event ev, events[20];
int epfd = epoll_create(256);
struct sockaddr_in clientaddr;
struct sockaddr_in serveraddr;
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
setnonblocking(listenfd);
ev.data.fd = listenfd;
ev.events = EPOLLIN | EPOLLET;
epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &ev);
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
char *local_addr = "10.243.0.160";
inet_aton(local_addr, &(serveraddr.sin_addr));
serveraddr.sin_port = htons(portnumber);
bind(listenfd, (sockaddr *)&serveraddr, sizeof(serveraddr));
listen(listenfd, LISTENQ);
maxi = 0;
cout << "start working" << endl;
for (;;) {
nfds = epoll_wait(epfd, events, 20, 500);
for (i = 0; i < nfds; ++i) {
if (events[i].data.fd == listenfd) {
connfd = accept(listenfd, (sockaddr *)&clientaddr, &clilen);
if (connfd < 0) {
perror("connfd<0");
exit(1);
}
setnonblocking(connfd);
char *str = inet_ntoa(clientaddr.sin_addr);
cout << "accapt a connection from " << str << endl;
cout << "create a connfd : " << connfd << endl;
ev.data.fd = connfd;
ev.events = EPOLLIN | EPOLLET;
epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &ev);
} else if (events[i].events & EPOLLIN) {
if ((sockfd = events[i].data.fd) < 0) {
continue;
}
cout << "EPOLLIN Event fd = " << events[i].data.fd << endl;
n = read(sockfd, line, MAXLINE);
if (n < 0) {
if (errno == ECONNRESET) {
close(sockfd);
events[i].data.fd = -1;
} else {
std::cout << "readline error" << std::endl;
}
} else if (n == 0) {
close(sockfd);
events[i].data.fd = -1;
} else {
cout << "get datd size n = " << n << endl;
line[n] = '/0';
cout << "read " << line << endl;
ev.data.fd = sockfd;
ev.events = EPOLLOUT | EPOLLET;
epoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &ev);
}
} else if (events[i].events & EPOLLOUT) {
cout << "EPOLLOUT Event fd = " << sockfd << endl;
sockfd = events[i].data.fd;
char str[MAXLINE] = "hi hi I am server!!!";
strcpy(line, str);
write(sockfd, line, MAXLINE);
ev.data.fd = sockfd;
ev.events = EPOLLIN | EPOLLET;
epoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &ev);
} else if (events[i].events & EPOLLERR) {
}
}
}
return 0;
}