本文整理汇总了C++中setnonblocking函数的典型用法代码示例。如果您正苦于以下问题:C++ setnonblocking函数的具体用法?C++ setnonblocking怎么用?C++ setnonblocking使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了setnonblocking函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: add_client_socket
void add_client_socket(int server_socket, int epoll) {
struct sockaddr_in cliaddr;
socklen_t addr_size = sizeof cliaddr;
int client_socket = accept(server_socket, (struct sockaddr*) &cliaddr, &addr_size);
if (client_socket < 0) {
perror("accept");
return;
}
setnonblocking(client_socket);
struct epoll_event connev;
connev.data.fd = client_socket;
connev.events = EPOLLIN | EPOLLOUT | EPOLLET | EPOLLHUP;
if (!epoll_ctl(epoll, EPOLL_CTL_ADD, client_socket, &connev) < 0) {
perror("Epoll fd add");
close(client_socket);
return;
}
}
示例2: accept_cb
void accept_cb(EV_P_ ev_io *w, int revents)
{
struct listen_ctx *listener = (struct listen_ctx *)w;
int serverfd = accept(listener->fd, NULL, NULL);
if (serverfd == -1) {
ERROR("accept");
return;
}
setnonblocking(serverfd);
int opt = 1;
setsockopt(serverfd, SOL_TCP, TCP_NODELAY, &opt, sizeof(opt));
#ifdef SO_NOSIGPIPE
setsockopt(serverfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
#endif
struct server *server = new_server(serverfd, listener->method);
server->listener = listener;
ev_io_start(EV_A_ & server->recv_ctx->io);
}
示例3: pxy_start_listen
int
pxy_start_listen()
{
struct sockaddr_in addr1;
master->listen_fd = socket(AF_INET,SOCK_STREAM,0);
if (master->listen_fd < 0) {
D("create listen fd error");
return -1;
}
int reuse = 1;
setsockopt(master->listen_fd,
SOL_SOCKET,
SO_REUSEADDR,
&reuse,
sizeof(int));
if (setnonblocking(master->listen_fd) < 0) {
D("set nonblocling error");
return -1;
}
addr1.sin_family = AF_INET;
addr1.sin_port = htons(setting.client_port);
inet_aton(setting.ip, &addr1.sin_addr);
if (bind(master->listen_fd, (struct sockaddr*)&addr1,
sizeof(addr1)) < 0) {
E("bind address %s:%d error", setting.ip,
setting.client_port);
return -1;
}
if (listen(master->listen_fd,1000) < 0) {
E("listen error");
return -1;
}
return 0;
}
示例4: accept_agent_side
int accept_agent_side( agent_t *agent, event_info_t *event_info)
{
socklen_t sin_size;
struct sockaddr_storage their_addr;
sin_size = sizeof(their_addr);
int index = find_empty_agent_sock(agent);
struct epoll_event event;
if(index < 0)
{
printf("agent sock table full!!!\n");
exit(1);
}
if(( agent->agent_fd_pool[index] =
accept(agent->listen_fds.agent_listen_sock[event_info->fd],
(struct sockaddr *) &their_addr, &sin_size)) == -1)
{
perror("");
printf("%s %d\n", __FILE__, __LINE__);
exit(1);
}
setnonblocking(agent->agent_fd_pool[index]);
event.events = EPOLLOUT;
event.data.ptr = &agent->agent_fd_pool_event[index];
agent->agent_fd_pool_event[index].type = AGENT_CONNECTED_UUID;
agent->agent_fd_pool_event[index].fd = index;
if( epoll_ctl(agent->event_pool, EPOLL_CTL_ADD,
agent->agent_fd_pool[index], &event))
{
perror("");
printf("%s %d\n", __FILE__, __LINE__);
exit(1);
}
return EXIT_SUCCESS;
}
示例5: build_server
int build_server() {
struct addrinfo hints, *res;
int s;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
s = getaddrinfo("0.0.0.0", setting.faddr_port, &hints, &res);
if (s != 0) {
perror("getaddrinfo error");
return -1;
}
serv_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(serv_sock < 0) {
perror("socket error");
exit(1);
}
int opt = 1;
setsockopt(serv_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
setsockopt(serv_sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
#ifdef SO_NOSIGPIPE
setsockopt(serv_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
#endif
if ( bind(serv_sock, res->ai_addr, res->ai_addrlen) == -1) {
perror("bind error");
exit(1);
}
if( listen(serv_sock, SOMAXCONN) == -1 ) {
perror("listen error");
exit(1);
}
setnonblocking(serv_sock);
}
示例6: create_socket
/* Creates a new unix domain socket. */
static int
create_socket(char *name)
{
int s;
struct sockaddr_un sockun;
if (strlen(name) > sizeof(sockun.sun_path) - 1)
{
errno = ENAMETOOLONG;
return -1;
}
s = socket(PF_UNIX, SOCK_STREAM, 0);
if (s < 0)
return -1;
sockun.sun_family = AF_UNIX;
strcpy(sockun.sun_path, name);
if (bind(s, (struct sockaddr*)&sockun, sizeof(sockun)) < 0)
{
close(s);
return -1;
}
if (listen(s, 128) < 0)
{
close(s);
return -1;
}
if (setnonblocking(s) < 0)
{
close(s);
return -1;
}
/* chmod it to prevent any suprises */
if (chmod(name, 0600) < 0)
{
close(s);
return -1;
}
return s;
}
示例7: rand
static remote_t *create_remote(listen_ctx_t *listener,
struct sockaddr *addr)
{
struct sockaddr *remote_addr;
int index = rand() % listener->remote_num;
if (addr == NULL) {
remote_addr = listener->remote_addr[index];
} else {
remote_addr = addr;
}
int remotefd = socket(remote_addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
if (remotefd < 0) {
ERROR("socket");
return NULL;
}
int opt = 1;
setsockopt(remotefd, SOL_TCP, TCP_NODELAY, &opt, sizeof(opt));
#ifdef SO_NOSIGPIPE
setsockopt(remotefd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
#endif
// Setup
setnonblocking(remotefd);
#ifdef SET_INTERFACE
if (listener->iface) {
setinterface(remotefd, listener->iface);
}
#endif
remote_t *remote = new_remote(remotefd, listener->timeout);
remote->addr_len = get_sockaddr_len(remote_addr);
memcpy(&(remote->addr), remote_addr, remote->addr_len);
remote->remote_index = index;
return remote;
}
示例8: setUpServer
void setUpServer(int argc, char *argv[]) //Sets up the server
{
timeout.tv_sec = TIMEOUT_SEC;
timeout.tv_usec = TIMEOUT_MS;
if (argc < MIN_ARGS)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
//set up the socket. we are using TCP
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) //make sure we can open the socket
{
error("ERROR opening socket");
}
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr)); //allow reuse of address
setnonblocking(sockfd); //nonblocking
//port stuff
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[PORT_ARG_SERVER]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) //check it we can bind it
{
error("ERROR on binding");
}
listen(sockfd, LISTENING_QUEUE); //set up a listening queue
clilen = sizeof(cli_addr); //set the length of the address
}
示例9: accept_host_side
int accept_host_side(agent_t *agent, client_t *new_client)
{
socklen_t sin_size;
struct sockaddr_in their_addr;
struct epoll_event event;
sin_size = sizeof(their_addr);
if(( new_client->host_sock = accept(agent->listen_fds.host_listen_sock,
(struct sockaddr *) &their_addr, &sin_size)) == -1)
{
perror("");
printf("%s %d\n", __FILE__, __LINE__);
exit(1);
}
strcpy(new_client->source_ip, inet_ntoa(their_addr.sin_addr));
new_client->source_port = htons(their_addr.sin_port);
new_client->host_fd_poll = OFF;
setnonblocking(new_client->host_sock);
event.events = EPOLLOUT;
event.data.ptr = &new_client->host_side_event_info;
new_client->host_side_event_info.type = HOST_CONNECTED;
new_client->host_side_event_info.client = new_client;
if( epoll_ctl(agent->event_pool, EPOLL_CTL_ADD,
new_client->host_sock, &event))
{
perror("");
printf("%s %d\n", __FILE__, __LINE__);
exit(1);
}
return EXIT_SUCCESS;
}
示例10: CSock
CAcceptor::CAcceptor(CInetAddr& sockAddr,
int backlog, int timeout) : CSock(AF_INET, SOCK_STREAM)
{
int res;
if (setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &res, sizeof(int)) < 0)
{
KL_SYS_ERRORLOG("file: "__FILE__", line: %d, " \
"set SO_REUSEADDR failed, err: %s", \
__LINE__, strerror(errno));
throw errno;
}
if(bind(m_fd, sockAddr.getsockaddr(), sizeof(struct sockaddr)) == -1)
{
KL_SYS_ERRORLOG("file: "__FILE__", line: %d, " \
"call bind failed, err: %s", \
__LINE__, strerror(errno));
throw errno;
}
listen(m_fd, backlog);
if(timeout > 0) //server option
{
if((res = setserveropt(timeout)) != 0)
{
KL_SYS_ERRORLOG("file: "__FILE__", line: %d, " \
"acceptor set server option failed, err: %s", \
__LINE__, strerror(res));
throw res;
}
}
if((res = setnonblocking()) != 0)
{
KL_SYS_ERRORLOG("file: "__FILE__", line: %d, " \
"acceptor set nonblocking failed, err: %s", \
__LINE__, strerror(res));
throw res;
}
}
示例11: init_socket
int
init_socket(int efd, uint16_t sock_type, struct sockaddr *srv, uint16_t srvlen, size_t simultaneous_connection)
{
struct epoll_event ev;
static int optval = 1;
int fd;
if ((fd = socket(sock_type, SOCK_STREAM, 0)) == -1)
return -1;
if (!setnonblocking(fd))
goto error;
memset(&ev, 0, sizeof(struct epoll_event));
ev.data.fd = fd;
ev.events = EPOLLIN | EPOLLET;
if (epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev) == -1)
goto error;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
int on = 1;
if (srv->sa_family == AF_INET6)
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
if (bind(fd, srv, srvlen) == -1)
goto error;
if (listen(fd, simultaneous_connection) == -1)
goto error;
return fd;
error:
close(fd);
return -1;
}
示例12: create_main_socket
/* Create the bound socket in the parent process */
static int create_main_socket() {
struct addrinfo *ai, hints;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
const int gai_err = getaddrinfo(OPTIONS.FRONT_IP, OPTIONS.FRONT_PORT,
&hints, &ai);
if (gai_err != 0) {
ERR("{getaddrinfo}: [%s]\n", gai_strerror(gai_err));
exit(1);
}
int s = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
int t = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &t, sizeof(int));
#ifdef SO_REUSEPORT
setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &t, sizeof(int));
#endif
setnonblocking(s);
if (bind(s, ai->ai_addr, ai->ai_addrlen)) {
fail("{bind-socket}");
}
#if TCP_DEFER_ACCEPT
int timeout = 1;
setsockopt(s, IPPROTO_TCP, TCP_DEFER_ACCEPT, &timeout, sizeof(int) );
#endif /* TCP_DEFER_ACCEPT */
prepare_proxy_line(ai->ai_addr);
freeaddrinfo(ai);
listen(s, OPTIONS.BACKLOG);
return s;
}
示例13: inet_init
/*
* 初始化网络
*/
int inet_init()
{
socket_t s;
int buf_size = 8*1024;
int reuse = 1;
if ((listening.s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
log_error(errno, "socket()");
exit(1);
}
if (setsockopt(listening.s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1 ||
setsockopt(listening.s, SOL_SOCKET, SO_REUSEADDR, &buf_size, sizeof(buf_size)) == -1) {
log_error(errno, "setsockopt()");
exit(1);
}
bzero(&listening.ls_addr, sizeof(listening.ls_addr));
listening.ls_addr.sin_family = AF_INET;
listening.ls_addr.sin_port = htons(LISTEN_PORT);
listening.ls_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(listening.s, (struct sockaddr *)&listening.ls_addr, sizeof(listening.ls_addr)) == -1) {
log_error(errno, "bind()");
exit(1);
}
if (listen(listening.s, LISTEN_BACKLOG) == -1) {
log_error(errno, "listen()");
exit(1);
}
setnonblocking(listening.s);
//注册一个accept()后的处理钩子
//listening.handler = http_connection_init;
}
示例14: LogErrorMsg
bool CLoveTcp::CreateSocket()
{
if((m_nSocket = socket(AF_INET,SOCK_STREAM,0))==-1)
{
LogErrorMsg("create socket error");
return false;
}
if (setnonblocking(m_nSocket) == -1)
{
LogErrorMsg("setnonblocking error");
}
int nSock = 0;
if (setsockopt(m_nSocket,SOL_SOCKET,SO_REUSEADDR,&nSock,sizeof(nSock)) < 0)
{
/* code */
LogErrorMsg("setsockopt failed");
}
m_nEpoll = epoll_create1(EPOLL_CLOEXEC);
if (EpollAdd(m_nSocket) == false)
{
return false;
};
return true;
}
示例15: handle_accept
int handle_accept(int fd)
{
struct epoll_event ev;
struct echo_data* pecho_data;
int newsock = 0;
do{//edge-trigger
newsock = accept(fd,NULL,NULL);
//if( newsock <=0 ){
if( newsock == -1 ){ //error occur
if( (errno == EAGAIN) || (errno == EWOULDBLOCK) ) break;
else{
perror("accept error");
return -1;
}
} else {
printf("Client accept with descriptor #%d\n" ,newsock);
pecho_data = alloc_echo_data(newsock, MAX_ECHO_BUF);
if( NULL == pecho_data ){
perror("malloc error");
return -1;
}
setnonblocking(newsock); //set nonblocking
ev.data.ptr = pecho_data;
ev.events = EPOLLIN|EPOLLPRI|EPOLLET; //edge-trigger
if( -1 == epoll_ctl(epfd, EPOLL_CTL_ADD, newsock, &ev) ){
perror("epoll_ctl error!");
return -1;
}
}
}while(1);
return 0;
}