示例#1
0
/*
** @brief it will set our server
**
** we will check every fd form our range ports and set good function
** for read and write.
** we will set our ports
** if we try to set more than max fd then we will stop to set them
** and warn the user
**
** @param s our structure we will set
** @param port argv[1] like 42:500 (range port)
** @param i index used to travel array of port
** @param sck socket fd
** @return -1 if any problem, else 0
*/
int	create_server(t_server *s, char *port, int i, int sck)
{
  if (_set_server_fd(s, port) == -1 || !(s->forks = create_root()))
    return (-1);
  while (sck < s->nb_fd && s->ports[++i] != -1 && i < 10)
    {
      if ((sck = my_s(AF_INET, SOCK_STREAM, my_name("TCP")->p_proto))
	  >= s->nb_fd)
	break;
      if (_change_port(s, i, sck, i + 1) == -1)
	{
	  i = -1;
	  break;
	}
      nbr_log("Server started on port", s->ports[i], "create_server",
	      TYPE_INFO);
      my_printf(1, "Server started on port %d\n", s->ports[i]);
      my_listen(sck, s->nb_fd);
      s->type_fd[sck] = FD_SERVER;
      s->fct_read[sck] = read_server;
      s->fct_write[sck] = 0;
      s->curr_max = sck < s->curr_max ? s->curr_max : sck;
    }
  if (sck >= s->nb_fd || i == -1)
    my_close(sck);
  return (0);
}
示例#2
0
文件: unix_gc.c 项目: 2asoft/freebsd
static void
listen_connect_drop(void)
{
	struct sockaddr_in sin;
	int slisten, sconnect, sv[2];
	int inflight, openfiles;
	socklen_t len;

	test = "listen_connect_drop";
	printf("%s\n", test);
	save_sysctls(&inflight, &openfiles);

	slisten = my_socket(PF_INET, SOCK_STREAM, 0);
	my_bind(slisten, (struct sockaddr *)&sin, sizeof(sin));
	my_listen(slisten, -1);

	my_socketpair(sv);

	len = sizeof(sin);
	my_getsockname(slisten, (struct sockaddr *)&sin, &len);

	sconnect = my_socket(PF_INET, SOCK_STREAM, 0);
	setnonblock(sconnect);
	my_connect(sconnect, (struct sockaddr *)&sin, len);

	sleep(1);
	sendfd(sv[0], slisten);
	close3(slisten, sv[0], sv[1]);
	sleep(1);
	close(sconnect);

	test_sysctls(inflight, openfiles);
}
示例#3
0
int			init_serveur(t_serv *serv)
{
  if ((serv->s = socket(AF_INET, SOCK_STREAM, serv->proto->p_proto)) == -1)
    {
      perror("");
      return (EXIT_FAILURE);
    }
  if (get_bind(serv) == EXIT_FAILURE)
    return (EXIT_FAILURE);
  start_serv();
  if (my_listen(&serv->s) == EXIT_FAILURE)
    return (EXIT_FAILURE);
  serv->client_len = sizeof(serv->sin_client);
  return (EXIT_SUCCESS);
}
示例#4
0
文件: unix_gc.c 项目: 2asoft/freebsd
/*
 * Listen sockets can also be passed over UNIX domain sockets, so test
 * various cases, including ones where listen sockets have waiting sockets
 * hanging off them...
 */
static void
listen_nothing(void)
{
	struct sockaddr_un sun;
	struct sockaddr_in sin;
	int inflight, openfiles;
	int s;

	test = "listen_nothing_unp";
	printf("%s\n", test);
	bzero(&sun, sizeof(sun));
	sun.sun_family = AF_LOCAL;
	sun.sun_len = sizeof(sun);
	snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/%s", dpath, test);
	save_sysctls(&inflight, &openfiles);
	s = my_socket(PF_LOCAL, SOCK_STREAM, 0);
	my_bind(s, (struct sockaddr *)&sun, sizeof(sun));
	my_listen(s, -1);
	close(s);
	(void)unlink(sun.sun_path);
	test_sysctls(inflight, openfiles);

	test = "listen_nothing_inet";
	printf("%s\n", test);
	bzero(&sin, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_len = sizeof(sin);
	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
	sin.sin_port = htons(0);
	save_sysctls(&inflight, &openfiles);
	s = my_socket(PF_INET, SOCK_STREAM, 0);
	my_bind(s, (struct sockaddr *)&sin, sizeof(sin));
	my_listen(s, -1);
	close(s);
	test_sysctls(inflight, openfiles);
}
示例#5
0
int main(int argc,char *argv[]) {
  int fd;
  ssize_t data_read;
  char buffer[1024];
  int ret;
  fd_set fds_read;

  if ((fd = my_listen(PORT)) < 0) {
    	exit(-1);	
  }
  printf("Waiting for messages on port: %ld\n", PORT);
  fflush(stdout);
  /* Initialize fd_set */
  FD_ZERO(&fds_read);
  FD_SET(fd, &fds_read);

  while (1) 
  {
    /* Wait for data available to be read (no timeout) */
   
    ret = select(4, &fds_read, NULL, NULL, NULL);
    if (ret < 0) {
    	fprintf(stderr, "Error on select: %d\n", errno);
    	exit(-1);
    }
    if (!FD_ISSET(fd, &fds_read)) {
    	printf("Error: network fd is not ready (?)\n");
    	exit(-1);
    }
    
    printf("Ready to receive...\n");
    /* Read received data */
    data_read = read(fd, &buffer, 1024);
    printf("Received data: %s\n", buffer);
                                
    /* Can exit if the received string == exit */
    if (!strcmp(buffer,"exit")) 
      break; 

    /* Write the same back  */
    write(fd, &buffer, data_read);             
  }               
  printf("Connection finished\n");
  close(fd);
}                       
示例#6
0
int main(int argc, char **argv)
{
    int     listenfd = 0;
    int     connfd = 0;
    pid_t   childpid ;
    socklen_t   clilen;
    struct  sockaddr_in     cliaddr;
    struct  sockaddr_in     servaddr;

    listenfd = my_socket(AF_INET, SOCK_STREAM, 0);
	
	signal(SIGCHLD, tu5_11sig_chld);						///
	
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT);

    my_bind(listenfd, (SA *)&servaddr, sizeof(servaddr));
    my_listen(listenfd, LISTENQ);

    while(1)
    {
        clilen = sizeof(cliaddr);
        if ( (connfd = my_accept(listenfd, (SA*)&cliaddr, &clilen)) < 0)
        {
        
        }

        if ((childpid = my_fork()) == 0)
        {
            my_close(listenfd);
            tu5_17str_echo(connfd);
            exit(0);
        }
        my_close(connfd);
    }

    return 0;
}
示例#7
0
int main(int argc, char **argv) {

    int port;                                // Listen port number
    int fd;                                  // Socket descriptor
    int listen;                              // Listen socket
    ErlConnect conn;                         // Connection data
    char *cookie;                            // Erlang magic cookie
    pid_t pid;
    int x;
    pthread_t thread;
    pthread_attr_t thread_attrs;
    thread_data_t *data;                     // Per thread data: thread ID,
                                             // connection node name
    static unsigned int tidx = 0;

    pid = fork();

    if (pid < 0)
        exit(EXIT_FAILURE);

    if (pid > 0)
        exit(EXIT_SUCCESS);

    if (setsid() < 0)
        exit(EXIT_FAILURE);

    umask(0);
    chdir("/");

    signal(SIGCHLD, SIG_DFL);
    signal(SIGHUP, SIG_DFL);

    if (argc == 3)
    {
        port = atoi(argv[1]);
        cookie = argv[2];
    } else {
        fprintf(stderr, "Usage: %s <port number> <erlang secret cookie>\n\r",argv[0]);
        exit(EXIT_FAILURE);
    }

    for (x = sysconf(_SC_OPEN_MAX); x>0; x--)
        close (x);

    setlogmask (LOG_UPTO (LOG_NOTICE));
    openlog("nfq_node", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0);

    erl_init(NULL, 0);

    if (erl_connect_init(1, cookie, 0) == -1)
        erl_err_quit("erl_connect_init");

    if ((listen = my_listen(port)) <= 0)
        erl_err_quit("my_listen");

    if (erl_publish(port) == -1)
        erl_err_quit("erl_publish");

    if (pthread_attr_init(&thread_attrs)) {
        syslog(LOG_ERR, "error while init pthread attr struct\n\r");
        exit(EXIT_FAILURE);
    }

    if ((pthread_attr_setdetachstate(&thread_attrs, PTHREAD_CREATE_DETACHED))) {
        syslog(LOG_ERR, "error while set pthread attributes\n\r");
        exit(EXIT_FAILURE);
    }

    syslog(LOG_NOTICE,"Waiting for connections...\n\r");

    for(;;) {

        while((fd = erl_accept(listen, &conn)) == ERL_ERROR)
            syslog(LOG_ERR,"%s Connection error\n\r", argv[0]);

        if((data = (thread_data_t *)malloc(sizeof(thread_data_t))) == NULL) {
            syslog(LOG_ERR, "Memory allocation error\n\r");
            exit(EXIT_FAILURE);
        }

        if((data->node = (char *)malloc(strlen(conn.nodename)+1)) == NULL) {
            syslog(LOG_ERR, "Memory allocation error\n\r");
            exit(EXIT_FAILURE);
        }

        data->fd = fd;
        data->idx = tidx;
        strcpy(data->node, conn.nodename);

        if (pthread_create(&thread, &thread_attrs, erl_message_read_loop, data)) {
            syslog(LOG_ERR, "Thread create failure\n\r");
            exit(EXIT_FAILURE);
        }
        tidx++;
    }

    closelog();
    return 0;
}
bool
SocketServer::setup(SocketServer::Service * service, 
		    unsigned short * port,
		    const char * intface){
  DBUG_ENTER("SocketServer::setup");
  DBUG_PRINT("enter",("interface=%s, port=%u", intface, *port));
  struct sockaddr_in servaddr;
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(*port);
  
  if(intface != 0){
    if(Ndb_getInAddr(&servaddr.sin_addr, intface))
      DBUG_RETURN(false);
  }
  
  const NDB_SOCKET_TYPE sock = my_socket_create(AF_INET, SOCK_STREAM, 0);
  if (!my_socket_valid(sock))
  {
    DBUG_PRINT("error",("socket() - %d - %s",
			socket_errno, strerror(socket_errno)));
    DBUG_RETURN(false);
  }

  DBUG_PRINT("info",("NDB_SOCKET: " MY_SOCKET_FORMAT,
                     MY_SOCKET_FORMAT_VALUE(sock)));

  if (my_socket_reuseaddr(sock, true) == -1)
  {
    DBUG_PRINT("error",("setsockopt() - %d - %s",
			errno, strerror(errno)));
    NDB_CLOSE_SOCKET(sock);
    DBUG_RETURN(false);
  }

  if (my_bind_inet(sock, &servaddr) == -1) {
    DBUG_PRINT("error",("bind() - %d - %s",
			socket_errno, strerror(socket_errno)));
    NDB_CLOSE_SOCKET(sock);
    DBUG_RETURN(false);
  }

  /* Get the port we bound to */
  if(my_socket_get_port(sock, port))
  {
    ndbout_c("An error occurred while trying to find out what"
	     " port we bound to. Error: %d - %s",
             socket_errno, strerror(socket_errno));
    my_socket_close(sock);
    DBUG_RETURN(false);
  }

  DBUG_PRINT("info",("bound to %u", *port));

  if (my_listen(sock, m_maxSessions > 32 ? 32 : m_maxSessions) == -1)
  {
    DBUG_PRINT("error",("listen() - %d - %s",
			socket_errno, strerror(socket_errno)));
    my_socket_close(sock);
    DBUG_RETURN(false);
  }

  ServiceInstance i;
  i.m_socket = sock;
  i.m_service = service;
  m_services.push_back(i);

  // Increase size to allow polling all listening ports
  m_services_poller.set_max_count(m_services.size());

  DBUG_RETURN(true);
}
示例#9
0
文件: eiaccnode.c 项目: 0x00evil/otp
static DWORD WINAPI
#else
static void*
#endif
    einode_thread(void* num)
{
    int n = (int)num;
    ei_cnode ec;
    char myname[100], destname[100];
    int r, fd, listen;
    ErlConnect conn;
    erlang_msg msg;
/*    FILE* f;*/

    sprintf(myname, "eiacc%d", n);
    printf("thread %d (%s) listening\n", n, myname, destname);
    r = ei_connect_init(&ec, myname, cookie, 0);
    if ((listen = my_listen(port+n)) <= 0) {
	printf("listen err\n");
	exit(7);
    }
    if (ei_publish(&ec, port + n) == -1) {
	printf("ei_publish port %d\n", port+n);
	exit(8);
    }
    fd = ei_accept(&ec, listen, &conn);
    printf("ei_accept %d\n", fd);
    if (fd >= 0) {
	ei_x_buff x, xs;
	int index, version;
	erlang_pid pid;

	ei_x_new(&x);
	for (;;) {
	    int got = ei_xreceive_msg(fd, &msg, &x);
	    if (got == ERL_TICK)
		continue;
	    if (got == ERL_ERROR) {
		printf("receive error %d\n", n);
		return 0;
	    }
	    printf("received %d\n", got);
	    break;
	}
	index = 0;
	if (ei_decode_version(x.buff, &index, &version) != 0) {
	    printf("ei_decode_version %d\n", n);
	    return 0;
	}
	if (ei_decode_pid(x.buff, &index, &pid) != 0) {
	    printf("ei_decode_pid %d\n", n);
	    return 0;
	}
/*	fprintf(f, "got pid from %s \n", pid.node);*/
	ei_x_new_with_version(&xs);
	ei_x_encode_tuple_header(&xs, 2);
	ei_x_encode_long(&xs, n);
	ei_x_encode_pid(&xs, &pid);
	r = ei_send(fd, &pid, xs.buff, xs.index);
/*	fprintf(f, "sent %d bytes %d\n", xs.index, r);*/
	shutdown(fd, SD_SEND);
	closesocket(fd);
	ei_x_free(&x);
	ei_x_free(&xs);
    } else {
	printf("coudn't connect fd %d r %d\n", fd, r);
    }
    printf("done thread %d\n", n);
/*    fclose(f);*/
    return 0;
}
示例#10
0
文件: server.c 项目: guozesheng/test
int main(int argc, const char *argv[])
{
    int listenfd, connfd;
    struct sockaddr_in servaddr, cliaddr;
    socklen_t cliaddr_len;
    char buf[MAXLINE];
    char str[INET_ADDRSTRLEN];
    int n, i;
    pid_t pid;

    listenfd = my_socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT);

    my_bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

    my_listen(listenfd, 20);

    printf("Accepting connections...\n");

    while (1) 
    {
        cliaddr_len = sizeof(cliaddr);
        connfd = my_accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len);

        pid = fork();
        if (pid == -1) 
        {
            perror("call to fork");
            exit(1);
        }
        else if (pid == 0) 
        {
            my_close(listenfd);
            while (1) 
            {
                n = my_read(connfd, buf, MAXLINE);
                if (n == 0)
                {
                    printf("The other side has been closed.\n");
                    break;
                }
                printf("Received from %s at PORT %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port));

                for (i = 0; i < n; i++) 
                {
                    buf[i] = toupper(buf[i]);
                }
                my_write(connfd, buf, n);
            }
            my_close(connfd);
            exit(0);
        }
        else 
        {
            my_close(connfd);
        }
    }

    return 0;
}
示例#11
0
int main(int argc, char **argv) {
  struct in_addr addr;           /* 32-bit IP number of host */
  int port;                      /* Listen port number */
  int listen;                    /* Listen socket */
  int fd;                        /* fd to Erlang node */
  ErlConnect conn;               /* Connection data */
  
  int loop = 1;                  /* Lopp flag */
  int got;                       /* Result of receive */
  unsigned char buf[BUFSIZE];    /* Buffer for incoming message */
  ErlMessage emsg;               /* Incoming message */
  
  ETERM *fromp, *tuplep, *fnp, *argp, *resp;
  int res;
  
  port = atoi(argv[1]);
  
  erl_init(NULL, 0);
  
  addr.s_addr = inet_addr("127.0.0.1");
  if (erl_connect_xinit("alpha", "cnode", "*****@*****.**", 
    &addr, "secretcookie", 0) == -1)
      erl_err_quit("erl_connect_xinit");
  
  /* Make a listen socket */
  if ((listen = my_listen(port)) <= 0)
    erl_err_quit("my_listen");

  if (erl_publish(port) == -1)
    erl_err_quit("erl_publish");

  if ((fd = erl_accept(listen, &conn)) == ERL_ERROR)
    erl_err_quit("erl_accept");
  fprintf(stderr, "Connected to %s\n\r", conn.nodename);
  
  while (loop) {
    
    got = erl_receive_msg(fd, buf, BUFSIZE, &emsg);
    if (got == ERL_TICK) {
      /* ignore */
    } else if (got == ERL_ERROR) {
      loop = 0;
    } else {
      
      if (emsg.type == ERL_REG_SEND) {
        fromp = erl_element(2, emsg.msg);
        tuplep = erl_element(3, emsg.msg);
        fnp = erl_element(1, tuplep);
        argp = erl_element(2, tuplep);
        
        if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) {
          res = foo(ERL_INT_VALUE(argp));
        } else if (strncmp(ERL_ATOM_PTR(fnp), "bar", 3) == 0) {
          res = bar(ERL_INT_VALUE(argp));
        }
        
        resp = erl_format("{cnode, ~i}", res);
        erl_send(fd, fromp, resp);

        erl_free_term(emsg.from);
        erl_free_term(emsg.msg);
        erl_free_term(fromp);
        erl_free_term(tuplep);
        erl_free_term(fnp);
        erl_free_term(argp);
        erl_free_term(resp);
      }
    }
  } /* while */
}
示例#12
0
文件: unix_gc.c 项目: 2asoft/freebsd
/*
 * Send a listen UDP socket over a UNIX domain socket.
 *
 * Send a listen TCP socket over a UNIX domain socket.
 *
 * Do each twice, with closing of the listen socket vs. socketpair in
 * different orders.
 */
static void
listen_drop(void)
{
	struct sockaddr_un sun;
	struct sockaddr_in sin;
	int inflight, openfiles;
	int s, sv[2];

	bzero(&sun, sizeof(sun));
	sun.sun_family = AF_LOCAL;
	sun.sun_len = sizeof(sun);

	/*
	 * Close listen socket first.
	 */
	test = "listen_drop_unp1";
	printf("%s\n", test);
	snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/%s", dpath, test);
	save_sysctls(&inflight, &openfiles);
	s = my_socket(PF_LOCAL, SOCK_STREAM, 0);
	my_bind(s, (struct sockaddr *)&sun, sizeof(sun));
	my_listen(s, -1);
	my_socketpair(sv);
	sendfd(sv[0], s);
	close3(s, sv[0], sv[1]);
	test_sysctls(inflight, openfiles);

	/*
	 * Close socketpair first.
	 */
	test = "listen_drop_unp2";
	printf("%s\n", test);
	snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/%s", dpath, test);
	save_sysctls(&inflight, &openfiles);
	s = my_socket(PF_LOCAL, SOCK_STREAM, 0);
	my_bind(s, (struct sockaddr *)&sun, sizeof(sun));
	my_listen(s, -1);
	my_socketpair(sv);
	sendfd(sv[0], s);
	close3(sv[0], sv[1], s);
	test_sysctls(inflight, openfiles);

	sin.sin_family = AF_INET;
	sin.sin_len = sizeof(sin);
	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
	sin.sin_port = htons(0);

	/*
	 * Close listen socket first.
	 */
	test = "listen_drop_inet1";
	printf("%s\n", test);
	bzero(&sun, sizeof(sun));
	save_sysctls(&inflight, &openfiles);
	s = my_socket(PF_INET, SOCK_STREAM, 0);
	my_bind(s, (struct sockaddr *)&sin, sizeof(sin));
	my_listen(s, -1);
	my_socketpair(sv);
	sendfd(sv[0], s);
	close3(s, sv[0], sv[1]);
	test_sysctls(inflight, openfiles);

	/*
	 * Close socketpair first.
	 */
	test = "listen_drop_inet2";
	printf("%s\n", test);
	bzero(&sun, sizeof(sun));
	save_sysctls(&inflight, &openfiles);
	s = my_socket(PF_INET, SOCK_STREAM, 0);
	my_bind(s, (struct sockaddr *)&sin, sizeof(sin));
	my_listen(s, -1);
	my_socketpair(sv);
	sendfd(sv[0], s);
	close3(sv[0], sv[1], s);
	test_sysctls(inflight, openfiles);
}
示例#13
0
文件: server.c 项目: guozesheng/test
int main(int argc, const char *argv[])
{
    int listenfd, connfd;
    struct sockaddr_in servaddr, cliaddr;
    socklen_t cliaddr_len;
    char buf[MAXLINE];
    char str[INET_ADDRSTRLEN];
    int n;
    pid_t pid;
    int filefd;

    listenfd = my_socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT);

    my_bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

    my_listen(listenfd, 20);

    printf("Accepting connections...\n");

    while (1) 
    {
        cliaddr_len = sizeof(cliaddr);
        connfd = my_accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len);

        pid = fork();
        if (pid == -1) 
        {
            perror("call to fork");
            exit(1);
        }
        else if (pid == 0) 
        {
            my_close(listenfd);
            n = my_read(connfd, buf, MAXLINE);
            filefd = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0666);
            my_write(filefd, &buf[strlen(buf)+1], strlen(&buf[strlen(buf)+1]));
            while ((n = my_read(connfd, buf, MAXLINE))) 
            {
                printf("Received from %s at PORT %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port));

                if (filefd < 0) 
                {
                    perror("create file error");
                    exit(1);
                }

                //for (i = 0; i < n; i++) 
                //{
                    //buf[i] = toupper(buf[i]);
                //}
                my_write(filefd, buf, n);
            }
            my_close(filefd);
            exit(0);
        }
        else 
        {
            my_close(connfd);
        }
    }

    return 0;
}
示例#14
0
//p258
//执行服务器的通常步骤
int
tcp_listen(const char *host, const char *serv, socklen_t *addrlenp)
{
    int    listenfd = 0;
    int    n = 0;
    const  int         on = 1;
    struct addrinfo    hints;
    struct addrinfo    *res = NULL;
    struct addrinfo    *ressave = NULL;

    bzero(&hints, sizeof(struct addrinfo));
    hints.ai_flags     = AI_PASSIVE;
    hints.ai_family    = AF_UNSPEC;
    hints.ai_socktype  = SOCK_STREAM;

    printf("listen中!!!");
    //if n != 0, 表示error
    if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0 )
    {
        err_quit("tcp_listen error for %s, %s: %s",
                 host, serv, gai_strerror(n));
    }

    ressave = res;             //保存res

    do
    {
        //系统调用
        listenfd = socket(res->ai_family,
                          res->ai_socktype,
                          res->ai_protocol);

        if (listenfd < 0)
        {
            continue;        //error, try next one
        }

        my_setsockopt(listenfd, SOL_SOCKET,
                      SO_REUSEADDR,
                      &on, sizeof(on));

        //系统调用
        if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
        {
            break;       //success!
        }

        my_close(listenfd);
    } while( (res = res->ai_next) != NULL );

    if (res == NULL)
    {
        err_sys("tcp_listen error for %s, %s", host, serv);
    }

    my_listen(listenfd, LISTENQ);

    if (addrlenp)      //addrlenp是一个指针
    {
        //return size of protocal address(值结果参数)
        *addrlenp = res->ai_addrlen;
    }


    //printf("")
    struct sockaddr   test ;
    printf("ai_addr = %s\n", my_inet_ntop(AF_INET, (const char *)ressave->ai_addr, (char*)&test, sizeof(test)));


    freeaddrinfo(ressave);

    return (listenfd);
}