#include "stdafx.h"
int main()
{
int mainfd = socket(AF_INET, SOCK_STREAM, 0);
if(mainfd == -1)
{
perror("socket");
return -1;
}
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(8080);
int flags = fcntl(mainfd, F_GETFL, 0);
if(flags == -1)
{
perror("fcntl");
return -2;
}
flags |= O_NONBLOCK;
if(fcntl(mainfd, F_SETFL, flags))
{
perror("fcntl");
return -3;
}
int opt = 1;
int* sockopt;
sockopt = new int(setsockopt(mainfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)));
if(*sockopt == -1)
{
perror("setsockopt");
return -4;
}
delete sockopt;
sockopt = nullptr;
int* bind_result = new int(bind(mainfd, (sockaddr*)&addr, sizeof(addr)));
if(*bind_result == -1)
{
perror("bind");
return -5;
}
delete bind_result;
bind_result = nullptr;
listen(mainfd, 5);
int epfd = epoll_create1(0);
if(epfd == -1)
{
perror("epoll_create1");
return -6;
}
epoll_event epev;
memset(&epev, 0, sizeof(epev));
epev.events = EPOLLIN;
epev.data.fd = mainfd;
int* epctl_result = new int(epoll_ctl(epfd, EPOLL_CTL_ADD, mainfd, &epev));
if(*epctl_result == -1)
{
perror("epoll_ctl");
return -7;
}
delete epctl_result;
epctl_result = nullptr;
int wait;
epoll_event evs[10];
const size_t BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];
while(true)
{
wait = epoll_wait(epfd, &epev, 10, -1);
//for(int i = 0; i < 10; ++i)
}
}