欢迎使用CSDN-markdown编辑器

来源:互联网 发布:爱心地图的软件 编辑:程序博客网 时间:2024/06/06 21:44

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 “<

原创粉丝点击