欢迎使用CSDN-markdown编辑器

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

using namespace std;

define MAXLINE 5

define OPEN_MAX 100

define LISTENQ 20

define SERV_PORT 4800

define INFTIM 1000

char *g_local_addr=”10.12.22.241”;

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 i, maxi, listenfd, connfd, sockfd,epfd,nfds;
ssize_t n;
char line[MAXLINE];
socklen_t clilen;

struct epoll_event ev,events[20];

struct sockaddr_in clientaddr;
struct sockaddr_in serveraddr;

listenfd = socket(AF_INET, SOCK_STREAM, 0);
epfd = epoll_create(256);

setnonblocking(listenfd);

ev.data.fd=listenfd;
ev.events=EPOLLIN|EPOLLOUT|EPOLLET;
epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&ev);

bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
inet_aton(g_local_addr,&(serveraddr.sin_addr));
serveraddr.sin_port=htons(SERV_PORT);
bind(listenfd,(sockaddr *)&serveraddr, sizeof(serveraddr));

listen(listenfd, LISTENQ);
cout<<" server on ip : "<<g_local_addr<<" :"<<SERV_PORT<<endl;
maxi = 0;

for ( ; ; )
{

    nfds=epoll_wait(epfd,events,20,500);

    for (i=0;i < nfds; i++)
    {
        if (events[i].data.fd==listenfd)
        {
            if (events[i].events&EPOLLIN)
            {
                cout<<" listenfd :"<<listenfd<< " with EPOLLIN"<<endl;
            }
            else
            {
                cout<<" listenfd :"<<listenfd<< " not with EPOLLIN"<<endl;
            }
            if (events[i].events&EPOLLOUT)
            {
                cout<<" listenfd :"<<listenfd<< " with EPOLLOUT"<<endl;
            }
            else
            {
                cout<<" listenfd :"<<listenfd<< " not with EPOLLOUT"<<endl;
            }
            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;

            ev.data.fd=connfd;
            ev.events=EPOLLIN|EPOLLOUT|EPOLLET;
            epoll_ctl(epfd,EPOLL_CTL_ADD,connfd,&ev);
            cout<<"---------------------------------------------------"<<endl;
        }
        else if (events[i].events&EPOLLIN)
        {
            cout<<"***********process in event**********"<<endl;
            if (events[i].events&EPOLLIN)
            {
                cout<<" fd :"<<events[i].data.fd<< " with EPOLLIN"<<endl;
            }
            else
            {
                cout<<" fd :"<<events[i].data.fd<< " not with EPOLLIN"<<endl;
            }
            if ( (sockfd = events[i].data.fd) < 0)
                continue;
            if ( (n = read(sockfd, line, MAXLINE)) < 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;
            }
            line[n] = '\0';
            cout << "read " << line << endl;
            if (events[i].events&EPOLLOUT)
            {
                cout<<" fd :"<<events[i].data.fd<< " with EPOLLOUT"<<endl;
                write(sockfd, line, n);
            }
            else
            {
                cout<<" fd :"<<events[i].data.fd<< " not with EPOLLOUT"<<endl;
            }

            //ev.data.fd=sockfd;
            //ev.events=EPOLLIN|EPOLLOUT|EPOLLET;
            //epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
            cout<<"---------------------------------------------------"<<endl;
        }
        else if (events[i].events&EPOLLOUT)
        {
            cout<<"***********process out event**********"<<endl;
            if (events[i].events&EPOLLIN)
            {
                cout<<" fd :"<<events[i].data.fd<< " with EPOLLIN"<<endl;
            }
            else
            {
                cout<<" fd :"<<events[i].data.fd<< " not with EPOLLIN"<<endl;
            }
            if (events[i].events&EPOLLOUT)
            {
                cout<<" fd :"<<events[i].data.fd<< " with EPOLLOUT"<<endl;
            }
            else
            {
                cout<<" fd :"<<events[i].data.fd<< " not with EPOLLOUT"<<endl;
            }
            sockfd = events[i].data.fd;
            write(sockfd, line, n);
            //ev.data.fd=sockfd;
            //ev.events=EPOLLIN|EPOLLOUT|EPOLLET;
            //epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
            cout<<"---------------------------------------------------"<<endl;
        }
    }
}
return 0;

}

//client

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

using namespace std;

define MAXLINE 80

define SERV_PORT 4800

char *g_local_addr=”10.12.22.241”;

int main(int argc, char *argv[])
{
struct sockaddr_in servaddr;
char buf[MAXLINE];
int sockfd, n;
char *str;

if (argc != 2)
{
fputs(“usage: ./client message\n”, stderr);
exit(1);
}
str = argv[1];

sockfd = socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, g_local_addr, &servaddr.sin_addr);
servaddr.sin_port = htons(SERV_PORT);

connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

write(sockfd, str, strlen(str));
sleep(1);
n = read(sockfd, buf, MAXLINE);
cout<<” read “<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值