• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ create_and_bind函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中create_and_bind函数的典型用法代码示例。如果您正苦于以下问题:C++ create_and_bind函数的具体用法?C++ create_and_bind怎么用?C++ create_and_bind使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了create_and_bind函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: main

int main(int argc, char argv[])
{
    const char *ip = "127.0.0.1";
    const char *port = "2016";

    int sfd = create_and_bind(ip, port);
    if (sfd == -1)
    {
        return -1;
    }

    struct event_base *base = event_init();
    if (base == NULL)
    {
        return -1;
    }


    struct event ev;
    event_set(&ev, sfd, EV_READ|EV_PERSIST, accept_cb, NULL);
    event_add(&ev, NULL);

    event_base_dispatch(base);
    return 0;
}
开发者ID:spch2008,项目名称:codelab,代码行数:25,代码来源:echo_server.c


示例2: main

int main (int argc, char *argv[])
{
    int sfd, s;
    int efd;
    struct epoll_event event;
    struct epoll_event events[MAXEVENTS];

    if (argc < 2){
        fprintf (stderr, "Usage: %s [port]\n", basename(argv[0]));
        exit (EXIT_FAILURE);
    }

    //  const char *ip = argv[1];
    //  int port = atoi(argv[2]);
    sfd = create_and_bind (argv[1]);
    if (sfd == -1) abort ();

    //listen for connection coming
    s = listen (sfd, SOMAXCONN);
    if (s == -1) {
        perror ("listen");
        abort ();
    }

    //create a epoll object
    efd = epoll_create1 (0);
    if (efd == -1) {
        perror ("epoll_create");
        abort ();
    }

    //add the listen socket to the event poll
    addfd(efd, sfd, 1);

    while(1)
    {
        int ret = epoll_wait(efd, events, MAXEVENTS, -1);
        if(ret < 0){
            printf("epoll wait failture..\n");
            break;
        }
        
        //et(events, ret, efd, sfd);
        lt(events, ret, efd, sfd);
    }


    close (sfd);

    return EXIT_SUCCESS;
}
开发者ID:CCoder123,项目名称:codemisc,代码行数:51,代码来源:epoll_example.c


示例3: main

void main(int argc, char *argv[])
{
	
	// Create Socket
	int infd = create_and_bind("1234");

	// Bind Socket
	int outfd = bind_socket(infd)
	
	// Listen on Socket
	int r = listen(outfd)
	
	// Accept Socket	

}
开发者ID:goyalankit,项目名称:basic_programs,代码行数:15,代码来源:yoserv.c


示例4: main

int main(int argc, char *argv[])
{
	if(argc <= 1)
	{
		printf("Usage: %s port\n", basename(argv[0]));
		return -1;
	}

	int ret = 0;
	int sfd = create_and_bind(argv[1]);
	if(sfd < 0) {printf("create_and_bind failed.\n"); return -2;}

	listen(sfd, 5);

	struct sockaddr_in client_address;
	socklen_t client_addrlength = sizeof( client_address );
	int connfd = accept( sfd, ( struct sockaddr* )&client_address, &client_addrlength );
	if ( connfd < 0 )
	{
		printf( "errno is: %d\n", errno );
		close( sfd);
		return -3;
	}

	char buff[1024];
	struct pollfd pfd[FDNUM];
	pfd[0].fd = connfd;
	pfd[0].events = POLLIN | POLLPRI;

	while(1)
	{
		memset( buff, '\0', sizeof( buff ) );
		int n = poll(pfd, 1, -1);
		printf("poll get %d fd ready.\n");

		if(pfd[0].revents & POLLIN)
		{
			int nr = read(connfd, buff, sizeof(buff));
			if(nr > 0)
			{
				printf("get %d bytes data: %s\n", nr, buff);
			}
			else if(nr == 0)
			{
				// client close the connection
				printf("client close the connection.\n");
				break;
			}
			else
			{
				// error occures
				perror("read");
				ret = -4;
				break;
			}
		}

		if(pfd[0].revents & POLLPRI)
		{
			int nr = recv(connfd, buff, sizeof(buff) - 1, MSG_OOB);
			if(nr < 0)
			{
				perror("recv");
				ret = -5;
				break;
			}
			printf("recv %d bytes OOB data: %s\n", nr, buff);
		}

	}


	close(sfd);

	return ret;
}
开发者ID:huntinux,项目名称:unpvol1,代码行数:76,代码来源:poll.c


示例5: main

int main(void)
{
    char in[3000],  sent[500], code[50], file[200], mime[100], moved[200], length[100], auth[200], auth_dir[500], start[100], end[100];
    char *result=NULL, *hostname, *hostnamef, *lines, *ext=NULL, *extf, *auth_dirf=NULL, *authf=NULL, *rangetmp;
    int buffer_chunks;
    long filesize, range=0;

    get_conf();
    create_and_bind();

    //Important stuff happens here.

    while(1) {
        sin_size = sizeof(struct sockaddr_in);
        if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
            perror("accept");
            continue;
        }

        if (!fork()) {
            close(sockfd);
        if (read(new_fd, in, 3000) == -1) {
    	perror("recive");
        } else {
    	lines = strtok(in, "\n\r");
    	do {
    	    hostname = strtok(NULL, "\n\r");
    		if (hostname[0] == 'R' && hostname[1] == 'a' && hostname[2] == 'n' && hostname[3] == 'g' && hostname[4] == 'e') {
    			rangetmp = hostname;
    			strcpy(code, "206 Partial Content");
    		}
    	} while (hostname[0] != 'H' || hostname[1] != 'o' || hostname[2] != 's' || hostname[3] != 't');
    	hostnamef = strtok(hostname, " ");
    	hostnamef = strtok(NULL, " ");
    	result = strtok(lines, " ");
    	result = strtok(NULL, " ");
    	if (strcmp(code, "206 Partial Content") == 0 ) {
    		rangetmp = strtok(strpbrk(rangetmp, "="), "=-");
    		range = atoi(rangetmp);
    	}

    	strcpy(file, result);
    	if (opendir(file)){
    	    if (file[strlen(file)-1] == '/'){
                    strcat(file, "/index.html");
    		openfile=fopen (file, "r");
                        if (openfile){
                            strcpy(code, "200 OK");
                        } else {
    		    //Here should be some kind of directory listing
    		    strcpy(file, "/404.html");
    		    openfile = fopen (file, "r");
    		    strcpy(code, "404 Not Found");
    		}
    	    } else {
    		strcpy(code, "301 Moved Permanently");
    		strcpy(moved, "Location: http://");
    		strcat(moved, hostnamef);
    		strcat(moved, result);
    		strcat(moved, "/");
    	    }
    	} else {
    	    openfile=fopen (file, "rb");
                if (openfile){
    		if (strlen(code) < 1) {
                    	strcpy (code, "200 OK");
    		}
                } else {
    		strcpy(file, "/404.html");
    		openfile = fopen (file, "r");
                    strcpy(code, "404 Not Found");
                }
                }
        }
        if (strcmp(code, "301 Moved Permanently") != 0){
    	fseek (openfile , 0 , SEEK_END);
                filesize = ftell (openfile);
        	rewind (openfile);
    	if (range > 0) {
    		sprintf(end, "%d", filesize);
    		filesize = filesize - range;
    		sprintf(start, "%d", range);
    		fseek (openfile , range , SEEK_SET);
    	}
    	buffer_chunks = filesize/1048576;
    	if(filesize%1048576 > 0){
    		buffer_chunks++;
    	}
    	sprintf(length, "%d", filesize);
    	buffer_counter = 0;
    	buffer = (char*) malloc (sizeof(char)*1048576);
        }

        if (strcmp(code, "404 Not Found") != 0 && strcmp(code, "301 Moved Permanently") !=0){
    	ext = strtok(file, ".");
            while(ext != NULL){
    	    ext = strtok(NULL, ".");
        	    if (ext != NULL){
    		extf = ext;
    	    }
//.........这里部分代码省略.........
开发者ID:dan-boa,项目名称:EE450,代码行数:101,代码来源:tinys.c


示例6: main


//.........这里部分代码省略.........

    if (timeout == NULL) {
        timeout = "60";
    }

    if (local_addr == NULL) {
        local_addr = "127.0.0.1";
    }

    if (pid_flags) {
        USE_SYSLOG(argv[0]);
        daemonize(pid_path);
    }

    // parse tunnel addr
    parse_addr(tunnel_addr_str, &tunnel_addr);

    if (tunnel_addr.port == NULL) {
        FATAL("tunnel port is not defined");
    }

#ifdef __MINGW32__
    winsock_init();
#else
    // ignore SIGPIPE
    signal(SIGPIPE, SIG_IGN);
    signal(SIGABRT, SIG_IGN);
#endif

    // Setup keys
    LOGI("initialize ciphers... %s", method);
    int m = enc_init(password, method);

    // Setup proxy context
    struct listen_ctx listen_ctx;
    listen_ctx.tunnel_addr = tunnel_addr;
    listen_ctx.remote_num = remote_num;
    listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *) * remote_num);
    for (i = 0; i < remote_num; i++) {
        char *host = remote_addr[i].host;
        char *port = remote_addr[i].port == NULL ? remote_port :
                     remote_addr[i].port;
        struct sockaddr_storage *storage = malloc(sizeof(struct sockaddr_storage));
        memset(storage, 0, sizeof(struct sockaddr_storage));
        if (get_sockaddr(host, port, storage, 1) == -1) {
            FATAL("failed to resolve the provided hostname");
        }
        listen_ctx.remote_addr[i] = (struct sockaddr *)storage;
    }
    listen_ctx.timeout = atoi(timeout);
    listen_ctx.iface = iface;
    listen_ctx.method = m;

    struct ev_loop *loop = EV_DEFAULT;

    if (mode != UDP_ONLY) {
        // Setup socket
        int listenfd;
        listenfd = create_and_bind(local_addr, local_port);
        if (listenfd < 0) {
            FATAL("bind() error:");
        }
        if (listen(listenfd, SOMAXCONN) == -1) {
            FATAL("listen() error:");
        }
        setnonblocking(listenfd);

        listen_ctx.fd = listenfd;

        ev_io_init(&listen_ctx.io, accept_cb, listenfd, EV_READ);
        ev_io_start(loop, &listen_ctx.io);
    }

    // Setup UDP
    if (mode != TCP_ONLY) {
        LOGI("UDP relay enabled");
        init_udprelay(local_addr, local_port, listen_ctx.remote_addr[0],
                      get_sockaddr_len(listen_ctx.remote_addr[0]),
                      tunnel_addr, m, listen_ctx.timeout, iface);
    }

    if (mode == UDP_ONLY) {
        LOGI("TCP relay disabled");
    }

    LOGI("listening at %s:%s", local_addr, local_port);

    // setuid
    if (user != NULL) {
        run_as(user);
    }

    ev_run(loop, 0);

#ifdef __MINGW32__
    winsock_cleanup();
#endif

    return 0;
}
开发者ID:xiaodongus,项目名称:shadowsocks-libev,代码行数:101,代码来源:tunnel.c


示例7: main

int main(int argc, char *argv[])
{
  int sfd, s;
  int efd;
  int so_rcvbuf = 0;
  int verbose = 0;
  struct epoll_event event;
  struct epoll_event *events;
  struct edata {
    int fd;
    int content_length;
    int content_offset;
    char *header_buf;
    int header_off;
    int header_max;
  };

  if (argc < 2 || argc > 3)
    {
      fprintf(stderr, "Usage: %s [port] [rcvbuf]\n", argv[0]);
      exit(EXIT_FAILURE);
    }

  sfd = create_and_bind(argv[1]);
  if (sfd == -1)
    abort();

  if (argc == 3)
      so_rcvbuf = atoi(argv[2]);

  s = make_socket_non_blocking(sfd);
  if (s == -1)
    abort();

  s = listen(sfd, SOMAXCONN);
  if (s == -1)
    {
      perror("listen");
      abort();
    }

  efd = epoll_create1(0);
  if (efd == -1)
    {
      perror("epoll_create");
      abort();
    }

  struct edata *ed = malloc(sizeof(*ed));
  ed->fd = sfd;
  ed->header_buf = 0;

  event.data.ptr = ed;
  event.events = EPOLLIN | EPOLLET;
  s = epoll_ctl(efd, EPOLL_CTL_ADD, sfd, &event);
  if (s == -1)
    {
      perror("epoll_ctl");
      abort();
    }

  /* Buffer where events are returned */
  events = calloc(MAXEVENTS, sizeof event);

  /* The event loop */
  while (1)
    {
      int n, i;

      n = epoll_wait(efd, events, MAXEVENTS, -1);
      for (i = 0; i < n; i++)
	{
	  struct edata *ed = events[i].data.ptr;
	  int fd = ed->fd;

	  if ((events[i].events & EPOLLERR) ||
              (events[i].events & EPOLLHUP) ||
              (!(events[i].events & EPOLLIN)))
	    {
	      fprintf(stderr, "epoll error\n");

	      s = epoll_ctl(efd, EPOLL_CTL_DEL, fd, 0);
	      if (s == -1)
		{
		  perror("epoll_ctl");
		  abort();
		}
	      close(fd);
	      free(ed);
	      continue;
	    }

	  else if (sfd == fd)
	    {
              while (1)
                {
                  struct sockaddr in_addr;
                  socklen_t in_len;
                  int infd;
                  char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
//.........这里部分代码省略.........
开发者ID:sling-incubator,项目名称:container-perf-tests,代码行数:101,代码来源:epoll-server.c


示例8: main

int main(int argc, char **argv)
{
	int lfd, s, efd;
	struct epoll_event event;
	struct epoll_event *events;

	char path[MAX_LINE];
	int port;
	configuration(&port, path); 

	char port_str[6];
	bzero(port_str, 6);
	snprintf(port_str, 5, "%d", port);
	lfd=create_and_bind(port_str);
	if(lfd==-1) abort();

	s=listen(lfd, SOMAXCONN);
	if(s==-1)
	{
		perror("listen");
		abort();
	}

	efd=epoll_create1(0);
	if(efd==-1)
	{
		perror("epoll_create");
		abort();
	}
	addfd(efd, lfd, false);

	events=(struct epoll_event*)calloc(MAXEVENTS, sizeof(event));

	while(1)
	{
		int n, i;
		n=epoll_wait(efd, events, MAXEVENTS, -1);
		for(i=0; i<n; i++)
		{
			if ((events[i].events&EPOLLERR)||
				(events[i].events&EPOLLHUP)||
				(!(events[i].events&EPOLLIN)))
			{
				/* A error has occured on this fd, or the socket is not 
				 * ready for reading (why were we notified then?) */
				fprintf(stderr, "epoll error\n");
				close(events[i].data.fd);
				continue;
			}
			else if(lfd==events[i].data.fd)
			{
				/* We have a notification on the listening socket,
				 * which means one or more incoming connections. */
				while(1)
				{
					struct sockaddr in_addr;
					socklen_t in_len;
					int infd;
					char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];

					in_len=sizeof(in_addr);
					infd=accept(lfd, &in_addr, &in_len);
					if(infd==-1)
					{
						if((errno==EAGAIN)||(errno==EWOULDBLOCK))
						{
							/* We have processed all incoming connections. */
							break;
						}
						else
						{
							perror("accept");
							break;
						}
					}
				
					s=getnameinfo(&in_addr, in_len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST|NI_NUMERICSERV);

					if(s==0)
					{
						print("Accepted connnection on descriptor %d: (host=%s, port=%s)\n", infd, hbuf, sbuf);
					}

					/* Make the incoming socket non-blocking and 
					 * add it to the list of fds to monitor. */
					addfd(efd, infd, true); 
				}
				continue;
			}
			else
			{
				/* We have data on the fd waiting to be read. Read and display it. 
				 * We must read whatever data is available completely, as we are running 
				 * in edge-triggered mode and won't get a notification again for the same data. */
					pthread_t thread;
					args param;
					param.cfd=events[i].data.fd;
					param.efd=efd;
				//	pthread_create(&thread, NULL, worker, (void*)&param);
				 	worker((void*)&param);
//.........这里部分代码省略.........
开发者ID:linwanfeng,项目名称:ows,代码行数:101,代码来源:main.c


示例9: start_ss_local_server

int start_ss_local_server(profile_t profile)
{
    srand(time(NULL));

    char *remote_host = profile.remote_host;
    char *local_addr = profile.local_addr;
    char *method = profile.method;
    char *password = profile.password;
    char *log = profile.log;
    int remote_port = profile.remote_port;
    int local_port = profile.local_port;
    int timeout = profile.timeout;

    mode = profile.mode;
    fast_open = profile.fast_open;
    verbose = profile.verbose;

    char local_port_str[16];
    char remote_port_str[16];
    sprintf(local_port_str, "%d", local_port);
    sprintf(remote_port_str, "%d", remote_port);

    USE_LOGFILE(log);

    if (profile.acl != NULL) {
        acl = !init_acl(profile.acl);
    }

    if (local_addr == NULL) {
        local_addr = "127.0.0.1";
    }

#ifdef __MINGW32__
    winsock_init();
#else
    // ignore SIGPIPE
    signal(SIGPIPE, SIG_IGN);
    signal(SIGABRT, SIG_IGN);
#endif

    struct ev_signal sigint_watcher;
    struct ev_signal sigterm_watcher;
    ev_signal_init(&sigint_watcher, signal_cb, SIGINT);
    ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM);
    ev_signal_start(EV_DEFAULT, &sigint_watcher);
    ev_signal_start(EV_DEFAULT, &sigterm_watcher);

    // Setup keys
    LOGI("initialize ciphers... %s", method);
    int m = enc_init(password, method);

    struct sockaddr_storage *storage = malloc(sizeof(struct sockaddr_storage));
    memset(storage, 0, sizeof(struct sockaddr_storage));
    if (get_sockaddr(remote_host, remote_port_str, storage, 1) == -1) {
        return -1;
    }

    // Setup proxy context
    struct ev_loop *loop = EV_DEFAULT;
    struct listen_ctx listen_ctx;

    listen_ctx.remote_num = 1;
    listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *));
    listen_ctx.remote_addr[0] = (struct sockaddr *)storage;
    listen_ctx.timeout = timeout;
    listen_ctx.method = m;
    listen_ctx.iface = NULL;

    // Setup socket
    int listenfd;
    listenfd = create_and_bind(local_addr, local_port_str);
    if (listenfd < 0) {
        ERROR("bind()");
        return -1;
    }
    if (listen(listenfd, SOMAXCONN) == -1) {
        ERROR("listen()");
        return -1;
    }
    setnonblocking(listenfd);

    listen_ctx.fd = listenfd;

    ev_io_init(&listen_ctx.io, accept_cb, listenfd, EV_READ);
    ev_io_start(loop, &listen_ctx.io);

    // Setup UDP
    if (mode != TCP_ONLY) {
        LOGI("udprelay enabled");
        struct sockaddr *addr = (struct sockaddr *)storage;
        init_udprelay(local_addr, local_port_str, addr,
                      get_sockaddr_len(addr), m, timeout, NULL);
    }

    LOGI("listening at %s:%s", local_addr, local_port_str);

    // Init connections
    cork_dllist_init(&connections);

    // Enter the loop
//.........这里部分代码省略.........
开发者ID:robinhunan,项目名称:shadowsocks-libev,代码行数:101,代码来源:local.c


示例10: start_ss_local_server

int start_ss_local_server(profile_t profile)
{
    srand(time(NULL));

    char *remote_host = profile.remote_host;
    char *local_addr = profile.local_addr;
    char *method = profile.method;
    char *password = profile.password;
    char *log = profile.log;
    int remote_port = profile.remote_port;
    int local_port = profile.local_port;
    int timeout = profile.timeout;

    udprelay = profile.udp_relay;
    fast_open = profile.fast_open;
    verbose = profile.verbose;

    char local_port_str[16];
    char remote_port_str[16];
    sprintf(local_port_str, "%d", local_port);
    sprintf(remote_port_str, "%d", remote_port);

    USE_LOGFILE(log);

    if (profile.acl != NULL) {
        acl = !init_acl(profile.acl);
    }

    if (local_addr == NULL) {
        local_addr = "0.0.0.0";
    }

#ifdef __MINGW32__
    winsock_init();
#else
    // ignore SIGPIPE
    signal(SIGPIPE, SIG_IGN);
    signal(SIGABRT, SIG_IGN);
#endif

    struct ev_signal sigint_watcher;
    struct ev_signal sigterm_watcher;
    ev_signal_init(&sigint_watcher, signal_cb, SIGINT);
    ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM);
    ev_signal_start(EV_DEFAULT, &sigint_watcher);
    ev_signal_start(EV_DEFAULT, &sigterm_watcher);

    // Setup keys
    LOGD("initialize ciphers... %s", method);
    int m = enc_init(password, method);

    // Setup socket
    int listenfd;
    listenfd = create_and_bind(local_addr, local_port_str);
    if (listenfd < 0) {
        FATAL("bind()");
    }
    if (listen(listenfd, SOMAXCONN) == -1) {
        FATAL("listen()");
    }
    setnonblocking(listenfd);
    LOGD("server listening at port %s.", local_port_str);

    // Setup proxy context
    struct listen_ctx listen_ctx;

    listen_ctx.remote_num = 1;
    listen_ctx.remote_addr = malloc(sizeof(ss_addr_t));
    listen_ctx.remote_addr[0].host = remote_host;
    listen_ctx.remote_addr[0].port = remote_port_str;
    listen_ctx.timeout = timeout;
    listen_ctx.fd = listenfd;
    listen_ctx.method = m;
    listen_ctx.iface = NULL;

    struct ev_loop *loop = EV_DEFAULT;
    if (!loop) {
        FATAL("ev_loop error.");
    }
    ev_io_init(&listen_ctx.io, accept_cb, listenfd, EV_READ);
    ev_io_start(loop, &listen_ctx.io);

    // Setup UDP
    if (udprelay) {
        LOGD("udprelay enabled.");
        init_udprelay(local_addr, local_port_str, remote_host, remote_port_str,
                      m, listen_ctx.timeout, NULL);
    }

    // Init connections
    cork_dllist_init(&connections);

    // Enter the loop
    ev_run(loop, 0);

    if (verbose) {
        LOGD("closed nicely.");
    }

    // Clean up
//.........这里部分代码省略.........
开发者ID:Tuochenlyu,项目名称:shadowsocks-libev,代码行数:101,代码来源:local.c


示例11: main

int
main(int argc, char *argv[])
{
    int sfd, res, epoll, cnt, i, sd;
    struct epoll_event event, events[MAX_EVENTS];
    
    
    if (argc != 2) {
        perror("usage : port \n");
        exit(1);
    }
    
    sfd = create_and_bind(argv[1]);
    if (sfd == -1) {
        perror("error : cannot create socket!\n");
        exit(1);
    }
    
    res = make_socket_non_binding(sfd);
    if (res == -1) {
        perror("error : connot set flags!\n");
        exit(1);
    }
    
    res = listen(sfd, SOMAXCONN);
    if (res == -1) {
        perror("error : cannot listen!\n");
        exit(1);
    }
    
    epoll = epoll_create(1);
    if (epoll == -1) {
        perror("error : cannot create epoll!\n");
        exit(1);
    }
    
    event.events  = EPOLLIN | EPOLLOUT | EPOLLET;
    event.data.fd = sfd;
    res = epoll_ctl(epoll, EPOLL_CTL_ADD, sfd, &event);
    if (res == -1) {
        perror("error : can not add event to epoll!\n");
        exit(1);
    }
    
    while (1) {
        cnt = epoll_wait(epoll, events, MAX_EVENTS, -1);
        
        for (i = 0; i < cnt; i++) {
            
            if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)
                || !(events[i].events & EPOLLIN))
            {
                perror("error : socket fd error!\n");
                close(events[i].data.fd);
                continue;
            
            } else if (events[i].data.fd == sfd) {
                
                while (1) { 
                    struct sockaddr client_addr;
                    int addrlen = sizeof(struct sockaddr);
                    
                    sd = accept(sfd, &client_addr, &addrlen);
                    if (sd == -1) {
                    
                        if (errno == EAGAIN || errno == EWOULDBLOCK) {
                            break;
                        
                        } else {
                            perror("error : cannot accept new socket!\n");
                            continue;
                        }
                        
                    } 
                    
                    res = make_socket_non_binding(sd);
                    if (res == -1) {
                        perror("error : cannot set flags!\n");
                        exit(1);
                    }
                    
                    event.data.fd = sd;
                    event.events  = EPOLLET | EPOLLIN;
                    res = epoll_ctl(epoll, EPOLL_CTL_ADD, sd, &event);
                    if (res == -1) {
                        perror("error : cannot add to epoll!\n");
                        exit(1);
                    }
                }  
                
            } else {
                int cnt;
                char buf[BUF_SIZE];
                
                while (1) {
                
                    cnt = read(events[i].data.fd,  buf, BUF_SIZE);
                    if (cnt == -1) {
                        if (errno == EAGAIN) {
                            break;
//.........这里部分代码省略.........
开发者ID:908245708,项目名称:Socket,代码行数:101,代码来源:epollsrv.c


示例12: main

int
main (int argc, char *argv[])
{
    int sfd, s;
    int efd;
    struct epoll_event event;
    struct epoll_event *events;
    if (argc != 2)
    {
        fprintf (stderr, "Usage: %s [port]\n", argv[0]);
        exit (EXIT_FAILURE);
    }
    sfd = create_and_bind (argv[1]);
    if (sfd == -1)
        abort ();
    s = make_socket_non_blocking (sfd);
    if (s == -1)
        abort ();
    s = listen (sfd, SOMAXCONN);
    if (s == -1)
    {
        perror ("listen");
        abort ();
    }
    efd = epoll_create1 (0);
    if (efd == -1)
    {
        perror ("epoll_create");
        abort ();
    }
    event.data.fd = sfd;
    event.events = EPOLLIN | EPOLLET;
    // READ AND ET
    s = epoll_ctl (efd, EPOLL_CTL_ADD, sfd, &event);
    if (s == -1)
    {
        perror ("epoll_ctl");
        abort ();
    }
    /* Buffer where events are returned */
    /* function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. */
    /* max event is 64 */
    events = calloc (MAXEVENTS, sizeof event);
    /* The event loop */
    while (1)
    {
        printf("wait here\n");
        int n, i;
        // RETURN THE ERADY NUMBS
        // ET AND LT only for the epoll_wait
        n = epoll_wait (efd, events, MAXEVENTS, -1);
        for (i = 0; i < n; i++)
        {
            if ((events[i].events & EPOLLERR) ||
                    (events[i].events & EPOLLHUP) ||
                    (!(events[i].events & EPOLLIN)))
            {
                /* An error has occured on this fd, or the socket is not
                   ready for reading (why were we notified then?) */
                fprintf (stderr, "epoll error\n");
                close (events[i].data.fd);
                continue;
            }

            else if (sfd == events[i].data.fd)
            {
                /* We have a notification on the listening socket, which
                   means one or more incoming connections. */
                while (1)
                {
                    struct sockaddr in_addr;
                    socklen_t in_len;
                    int infd;
                    char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];

                    in_len = sizeof in_addr;
                    // if there is connection wait to process it will return the
                    // infd
                    infd = accept (sfd, &in_addr, &in_len);
                    if (infd == -1)
                    {
                        if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
                        {
                            /* We have processed all incoming
                               connections. */
                            break;
                        }
                        else
                        {
                            perror ("accept");
                            break;
                        }
                    }
                    //it converts a socket address to a corresponding host and service
                    //return numeric host and service
                    s = getnameinfo (&in_addr, in_len,
                                     hbuf, sizeof hbuf,
                                     sbuf, sizeof sbuf,
                                     NI_NUMERICHOST | NI_NUMERICSERV);
                    if (s == 0)
//.........这里部分代码省略.........
开发者ID:colin-zhou,项目名称:c-homework,代码行数:101,代码来源:epoll-example.c


示例13: main

int main(int argc, char *argv[]) {
  int sfd, s;
  int efd;
  struct epoll_event event;
  struct epoll_event *events;

  if(argc != 2) {
    fprintf(stderr, "Usage: %s [port]\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  sfd = create_and_bind(argv[1]);
  if(sfd == -1) 
    abort();

  s = make_socket_non_blocking(sfd);
  if(s == -1) 
    abort();

  s = listen(sfd, SOMAXCONN);
  if(s == -1) {
    perror("listen");
    abort();  
  }

  efd = epoll_create1(0);
  if(efd == -1) {
    perror("epoll_create1");
    abort();
  }
  event.data.fd = sfd;
  event.events = EPOLLIN | EPOLLET;
  s = epoll_ctl(efd, EPOLL_CTL_ADD, sfd, &event);
  if(s == -1) {
    perror("epoll_ctl");
    abort();
  }

  events = calloc(MAXEVENTS, sizeof(event));
  /* The event loop */
  while(1) {
    int n, i;
    
    n = epoll_wait(efd, events, MAXEVENTS, -1);
    for(i = 0; i < n; i++) {
      if((events[i].events & EPOLLERR) || 
	(events[i].events & EPOLLHUP) || 
	(!(events[i].events & EPOLLIN))) {
        /*An error occurred, notified but no ready*/
        fprintf(stderr, "epoll error\n");
        close(events[i].data.fd);
        continue;
      }
      else if(sfd == events[i].data.fd) {
        /*listener port is notified, which means incoming connection*/
        while(1) {
          struct sockaddr in_addr;
          socklen_t in_len;
          int infd;
          char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
   
          in_len = sizeof(in_addr);
          infd = accept(sfd, &in_addr, &in_len);
          if(infd == -1) {
            if((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
              /*we have processed all incoming connections*/
              break;
            }
            else {
              perror("accept");
              break;
            }
          }

          s = getnameinfo(&in_addr, in_len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
          if(s == 0) {
            printf("Accepted connection on descriptor %d (host=%s, port=%s)\n", infd, hbuf, sbuf);
          }

          /*make the socket non-blocking and add it to monitor list*/
          s = make_socket_non_blocking(infd);
          if(s == -1)
            abort();
  
          event.data.fd = infd;
          event.events = EPOLLIN | EPOLLET;
          s = epoll_ctl(efd, EPOLL_CTL_ADD, infd, &event);
          if(s == -1) {
            perror("epoll_ctl");
            abort();
          }
        }
        continue;
      }
      else {
        /*read is ready*/ 
        int done = 0;
        while(1) {
          ssize_t count;
          char buf[512];
//.........这里部分代码省略.........
开发者ID:AllenShi,项目名称:CLinuxExam,代码行数:101,代码来源:epoll_tcp_server.c


示例14: main


//.........这里部分代码省略.........

    // ignore SIGPIPE
    signal(SIGPIPE, SIG_IGN);
    signal(SIGCHLD, SIG_IGN);
    signal(SIGABRT, SIG_IGN);

    // setup asyncns
    asyncns_t *asyncns;
    if (!(asyncns = asyncns_new(dns_thread_num)))
    {
        FATAL("asyncns failed");
    }

    // setup keys
    LOGD("initialize ciphers... %s", method);
    int m = enc_init(password, method);

    // inilitialize ev loop
    struct ev_loop *loop = EV_DEFAULT;

    // inilitialize listen context
    struct listen_ctx listen_ctx_list[server_num + 1];

    // bind to each interface
    while (server_num > 0)
    {
        int index = --server_num;
        const char* host = server_host[index];
        int success = 1;
        int listenfd;

        if (start_port > 0) {
            server_port = itoa(start_port);
        }
        do {
            // Bind to port
            
            listenfd = create_and_bind(host, server_port);
            success = 1;
            if (listenfd < 0)
            {
                success = 0;
            }
            if (listen(listenfd, SOMAXCONN) == -1)
            {
                success = 0;
            }
            if (!success) {
                if (start_port < end_port) {
                    start_port++;
                    server_port = itoa(start_port);
                } else
                {
                    FATAL("Out of listen ports!");
                    exit(1);
                }
            }
        } while (!success);
        setnonblocking(listenfd);
        LOGD("server listening at port %s.", server_port);

        struct listen_ctx *listen_ctx = &listen_ctx_list[index + 1];

        // Setup proxy context
        listen_ctx->timeout = atoi(timeout);
        listen_ctx->asyncns = asyncns;
        listen_ctx->fd = listenfd;
        listen_ctx->method = m;
        listen_ctx->iface = iface;

        ev_io_init (&listen_ctx->io, accept_cb, listenfd, EV_READ);
        ev_io_start (loop, &listen_ctx->io);
    }

    // initialize the DNS
    struct listen_ctx *listen_ctx = &listen_ctx_list[0];
    int asyncnsfd = asyncns_fd(asyncns);
    listen_ctx->timeout = atoi(timeout);
    listen_ctx->asyncns = asyncns;
    listen_ctx->fd = asyncnsfd;
    listen_ctx->method = m;
    listen_ctx->iface = iface;
    ev_io_init (&listen_ctx->io, server_resolve_cb, asyncnsfd, EV_READ);
    ev_io_start (loop, &listen_ctx->io);

    // Setup UDP
    if (udprelay)
    {
        LOGD("udprelay enabled.");
        udprelay_init(server_host[0], server_port, dns_thread_num, m, listen_ctx->timeout, iface);
    }

    // setuid
    if (user != NULL)
        run_as(user);

    // start ev loop
    ev_run (loop, 0);
    return 0;
}
开发者ID:lsibjornrun,项目名称:shadowsocks-libev,代码行数:101,代码来源:server.c


示例15: sock_boot

void*
sock_boot (void *v_options)
{
  int sfd, s;
  int efd;
  struct epoll_event event;
  struct epoll_event *events;

  s_options* options = (s_options *) v_options;
  sfd = create_and_bind (options->port);
  if (sfd == -1)
    abort ();

  s = make_socket_non_blocking (sfd);
  if (s == -1)
    abort ();

  s = listen (sfd, SOMAXCONN);
  if (s == -1)
    {
      perror ("listen");
      abort ();
    }

  efd = epoll_create (1);
  if (efd == -1)
    {
      perror ("epoll_create");
      abort ();
    }

  event.data.fd = sfd;
  event.events = EPOLLIN | EPOLLET;
  s = epoll_ctl (efd, EPOLL_CTL_ADD, sfd, &event);
  if (s == -1)
    {
      perror ("epoll_ctl");
      abort ();
    }

  /* Buffer where events are returned */
  events = calloc (MAXEVENTS, sizeof event);

  /* The event loop */
  while (1)
    {
      int n, i;

      n = epoll_wait (efd, events, MAXEVENTS, -1);
      for (i = 0; i < n; i++)
	{
	  if ((events[i].events & EPOLLERR) ||
              (events[i].events & EPOLLHUP) ||
              (!(events[i].events & EPOLLIN)))
	    {
              /* An error has occured on this fd, or the socket is not
                 ready for reading (why were we notified then?) */
	      fprintf (stderr, "epoll error\n");
	      close (events[i].data.fd);
	      continue;
	    }

	  else if (sfd == events[i].data.fd)
	    {
              /* We have a notification on the listening socket, which
                 means one or more incoming connections. */
              while (1)
                {
                  struct sockaddr in_addr;
                  socklen_t in_len;
                  int infd;
                  char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];

                  in_len = sizeof in_addr;
                  infd = accept (sfd, &in_addr, &in_len);
                  if (infd == -1)
                    {
                      if ((errno == EAGAIN) ||
                          (errno == EWOULDBLOCK))
                        {
                          /* We have processed all incoming
                             connections. */
                          break;
                        }
                      else
                        {
                          perror ("accept");
                          break;
                        }
                    }

                  s = getnameinfo (&in_addr, in_len,
                                   hbuf, sizeof hbuf,
                                   sbuf, sizeof sbuf,
                                   NI_NUMERICHOST | NI_NUMERICSERV);
                  if (s == 0)
                    {
                      printf("Accepted connection on descriptor %d "
                             "(host=%s, port=%s)\n", infd, hbuf, sbuf);
                    }
//.........这里部分代码省略.........
开发者ID:keiya,项目名称:Volatile_Warmth,代码行数:101,代码来源:epollsock.c


示例16: main


//.........这里部分代码省略.........
    
    if (conf_path != NULL) {
        jconf_t *conf = read_jconf(conf_path);
        if (remote_num == 0) {
            remote_num = conf->remote_num;
            for (i = 0; i < remote_num; i++) {
                remote_addr[i] = conf->remote_addr[i];
            }
        }
        if (remote_port == NULL) {
            remote_port = conf->remote_port;
        }
        if (local_addr == NULL) {
            local_addr = conf->local_addr;
        }
        if (local_port == NULL) {
            local_port = conf->local_port;
        }
        if (password == NULL) {
            password = conf->password;
        }
        if (method == NULL) {
            method = conf->method;
        }
        if (timeout == NULL) {
            timeout = conf->timeout;
        }
    }

    if (remote_num == 0 || remote_port == NULL ||
        local_port == NULL || password == NULL) {
        usage();
        exit(EXIT_FAILURE);
    }

    if (timeout == NULL) {
        timeout = "10";
    }

    if (local_addr == NULL) {
        local_addr = "127.0.0.1";
    }

    if (pid_flags) {
        USE_SYSLOG(argv[0]);
        daemonize(pid_path);
    }

    // ignore SIGPIPE
    signal(SIGPIPE, SIG_IGN);
    signal(SIGABRT, SIG_IGN);

    // Setup keys
    LOGI("initialize ciphers... %s", method);
    int m = enc_init(password, method);

    // Setup socket
    int listenfd;
    listenfd = create_and_bind(local_addr, local_port);
    if (listenfd < 0) {
        FATAL("bind() error");
    }
    if (listen(listenfd, SOMAXCONN) == -1) {
        FATAL("listen() error");
    }
    setnonblocking(listenfd);
    LOGI("listening at %s:%s", local_addr, local_port);

    // Setup proxy context
    struct listen_ctx listen_ctx;
    listen_ctx.remote_num = remote_num;
    listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *) * remote_num);
    for (int i = 0; i < remote_num; i++) {
        char *host = remote_addr[i].host;
        char *port = remote_addr[i].port == NULL ? remote_port :
                     remote_addr[i].port;
        struct sockaddr_storage *storage = malloc(sizeof(struct sockaddr_storage));
        memset(storage, 0, sizeof(struct sockaddr_storage));
        if (get_sockaddr(host, port, storage, 1) == -1) {
            FATAL("failed to resolve the provided hostname");
        }
        listen_ctx.remote_addr[i] = (struct sockaddr *)storage;
    }
    listen_ctx.timeout = atoi(timeout);
    listen_ctx.fd = listenfd;
    listen_ctx.method = m;

    struct ev_loop *loop = EV_DEFAULT;
    ev_io_init(&listen_ctx.io, accept_cb, listenfd, EV_READ);
    ev_io_start(loop, &listen_ctx.io);

    // setuid
    if (user != NULL) {
        run_as(user);
    }

    ev_run(loop, 0);

    return 0;
}
开发者ID:ItsLucas,项目名称:shadowsocks-libev,代码行数:101,代码来源:redir.c


示例17: main


//.........这里部分代码省略.........
        {
            remote_num = conf->remote_num;
            for (i = 0; i < remote_num; i++)
            {
                remote_addr[i] = conf->remote_addr[i];
            }
        }
        if (remote_port == NULL) remote_port = conf->remote_port;
        if (local_addr == NULL) local_addr = conf->local_addr;
        if (local_port == NULL) local_port = conf->local_port;
        if (password == NULL) password = conf->password;
        if (method == NULL) method = conf->method;
        if (timeout == NULL) timeout = conf->timeout;
    }

    if (remote_num == 0 || remote_port == NULL ||
            local_port == NULL || password == NULL)
    {
        usage();
        exit(EXIT_FAILURE);
    }

    if (timeout == NULL) timeout = "10";

    if (local_addr == NULL) local_addr = "0.0.0.0";

    if (pid_flags)
    {
        USE_SYSLOG(argv[0]);
        demonize(pid_path);
    }

#ifdef __MINGW32__
    winsock_init();
#else
    // ignore SIGPIPE
    signal(SIGPIPE, SIG_IGN);
    signal(SIGABRT, SIG_IGN);
#endif

    // Setup keys
    LOGD("initialize ciphers... %s", method);
    int m = enc_init(password, method);

    // Setup socket
    int listenfd;
    listenfd = create_and_bind(local_addr, local_port);
    if (listenfd < 0)
    {
        FATAL("bind() error..");
    }
    if (listen(listenfd, SOMAXCONN) == -1)
    {
        FATAL("listen() error.");
    }
    setnonblocking(listenfd);
    LOGD("server listening at port %s.", local_port);

    // Setup proxy context
    struct listen_ctx listen_ctx;
    listen_ctx.remote_num = remote_num;
    listen_ctx.remote_addr = malloc(sizeof(ss_addr_t) * remote_num);
    while (remote_num > 0)
    {
        int index = --remote_num;
        if (remote_addr[index].port == NULL) remote_addr[index].port = remote_port;
        listen_ctx.remote_addr[index] = remote_addr[index];
    }
    listen_ctx.timeout = atoi(timeout);
    listen_ctx.fd = listenfd;
    listen_ctx.iface = iface;
    listen_ctx.method = m;

    struct ev_loop *loop = ev_default_loop(0);
    if (!loop)
    {
        FATAL("ev_loop error.");
    }
    ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ);
    ev_io_start (loop, &listen_ctx.io);

    // Setup UDP
    if (udprelay)
    {
        LOGD("udprelay enabled.");
        udprelay_init(local_addr, local_port, remote_addr[0].host, remote_addr[0].port, m, listen_ctx.timeout, iface);
    }

    // setuid
    if (user != NULL)
        run_as(user);

    ev_run (loop, 0);

#ifdef __MINGW32__
    winsock_cleanup();
#endif

    return 0;
}
开发者ID:KennethLaw

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ create_area函数代码示例发布时间:2022-05-30
下一篇:
C++ createWindow函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap