int main(int argc, char **argv) { struct addrinfo *p, *listp, hints; char buf[MAXLINE]; int rc, flags; if (argc != 2) { fprintf(stderr, "usage: %s <domain name>\n", argv[0]); exit(0); } /* Get a list of addrinfo records */ memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_INET; /* IPv4 only */ //line:netp:hostinfo:family hints.ai_socktype = SOCK_STREAM; /* Connections only */ //line:netp:hostinfo:socktype if ((rc = getaddrinfo(argv[1], NULL, &hints, &listp)) != 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(rc)); exit(1); } /* Walk the list and display each IP address */ flags = NI_NUMERICHOST; /* Display address string instead of domain name */ for (p = listp; p; p = p->ai_next) { Getnameinfo(p->ai_addr, p->ai_addrlen, buf, MAXLINE, NULL, 0, flags); printf("%s\n", buf); } /* Clean up */ Freeaddrinfo(listp); exit(0); }
int main(int argc, char **argv) { int listenfd, connfd; char hostname[MAXLINE], port[MAXLINE]; socklen_t clientlen; struct sockaddr_storage clientaddr; pthread_t tid; if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); } listenfd = Open_listenfd(argv[1]); sbuf_init(&sbuf, bufferSize); //初始化结构 int i; for(i = 0; i < MAXThreads; ++i) Pthread_create(&tid,NULL,thread,NULL); while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf("Accepted connection from (%s, %s)\n", hostname, port); sbuf_insert(&sbuf, connfd); //生产者加入描述符 } sbuf_deinit(&sbuf); //释放buffer return 0; }
/* * NAME: inet_ntostr * USAGE: Convert a "sockaddr name" (SA) into a Hostname/p-addr * PLAIN ENGLISH: Convert getpeername() into "foo.bar.com" * ARGS: name - The socket address, possibly returned by getpeername(). * retval - A string to store the hostname/paddr (RETURN VALUE) * size - The length of 'retval' in bytes * RETURN VALUE: "retval" is returned upon success * "empty_string" is returned for any error. * * NOTES: 'flags' should be set to NI_NAMEREQD if you don't want the remote * host's p-addr if it does not have a DNS hostname. */ int inet_ntostr (SA *name, char *host, int hsize, char *port, int psize, int flags) { int retval; socklen_t len; len = socklen(name); if ((retval = Getnameinfo(name, len, host, hsize, port, psize, flags | NI_NUMERICSERV))) { yell("Getnameinfo (sockaddr->p_addr): %s", gai_strerror(retval)); return retval; } return 0; }
int main(int argc, char **argv) { int listenfd, connfd; char hostname[MAXLINE], port[MAXLINE]; socklen_t clientlen; struct sockaddr_storage clientaddr; /* Check command line args */ if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); } listenfd = Open_listenfd(argv[1]); while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); //line:netp:tiny:accept Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf("Accepted connection from (%s, %s)\n", hostname, port); doit(connfd); //line:netp:tiny:doit Close(connfd); //line:netp:tiny:close } }
int main(int argc, char **argv) { int listenfd; int * connfd; char hostname[MAXLINE], port[MAXLINE]; socklen_t clientlen; struct sockaddr_storage clientaddr; /****************************** Section to setup the DLL ******************************/ // This will be the function stub used for addition // This would be any service that we want. // For test purposes, we are keeping it simple // Later we will extend it to add even more // Services char * add_service = "adder_lib.so"; dlhandle = dlopen ("./cgi-bin/adder_lib.so", RTLD_LAZY); // This handle will be used to access particular functions // Within the library marked. //dlhandle = dlopen(add_service, RTLD_LAZY); // No need to have staticly // linked to program specified // in "service". if (dlhandle == NULL) { printf("Service %s Not Found: %s\n", add_service, dlerror()); } // Now we shall get the add function stub functionstub = dlsym(dlhandle, "add"); if (!functionstub) { printf("Problem calling add_function(): %s\n", dlerror()); } // The function stub will be available to all the threads for use. /******** Section Ends *********/ /* Check command line args */ if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); } listenfd = Open_listenfd(argv[1]); while (1) { clientlen = sizeof(clientaddr); connfd = (int *) malloc( sizeof( int ) ) ; *connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); //line:netp:tiny:accept Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf("Accepted connection from (%s, %s)\n", hostname, port); //doit(connfd); //line:netp:tiny:doit pthread_t pth; pthread_create(&pth,NULL,doit, connfd); } }
int main(int argc, char *argv[]) { int listenfd, connfd; char hostname[MAXLINE], port[MAXLINE]; socklen_t clientlen; struct sockaddr_storage clientaddr; int acc_count = 0; //if (strcmp(argv[2], "compute-0-29.local") == 0) //{ printf("%s listening\n", argv[2]); listenfd = Open_listenfd ("15618"); //} sleep(5); //int send_val = 5; printf("Hostname is %s\n", argv[2]); printf("Int got as %d\n", atoi(argv[1])); if (strcmp(argv[2], "compute-0-29.local") == 0) { while (1) { clientlen = sizeof (clientaddr); // accept connections connfd = Accept (listenfd, (SA *) & clientaddr, &clientlen); Getnameinfo ((SA *) & clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf ("Accepted connection from (%s, %s). Connfd is %d\n", hostname, port, connfd); //newfd = (int *) malloc (sizeof (int)); //newfd = connfd; // go serve this client! // pthread_create (&tid, NULL, doit, newfd); acc_count++; double send_double = 232.23; int retval = Rio_writen (connfd, (void *)&send_double, sizeof(double)); if (retval < 0) { printf("Rio_writen to %d encountered a problem.\n", connfd); unix_error ("Rio_writen error"); } retval = Rio_writen (connfd, (void *)&send_double, sizeof(double)); if (retval < 0) { printf("Rio_writen to %d encountered a problem.\n", connfd); unix_error ("Rio_writen error"); } int len = Rio_readn (connfd, (void *)&send_double, sizeof(double)); if (len < 0) { unix_error ("Rio_readlineb error"); } printf("Host %s got len as %d and receive_val as %lf\n", argv[2], len, send_double); len = Rio_readn (connfd, (void *)&send_double, sizeof(double)); if (len < 0) { unix_error ("Rio_readlineb error"); } printf("Host %s got len as %d and receive_val as %lf\n", argv[2], len, send_double); if (acc_count == 3) { printf("Accepted 3 connections.\n"); break; } } } else { int serverfd = Open_clientfd ("10.22.1.241", "15618"); printf("In host %s, serverfd is %d\n", argv[2], serverfd); double buf; int len = Rio_readn (serverfd, (void *)&buf, sizeof(double)); if (len < 0) { unix_error ("Rio_readlineb error"); } printf("Host %s got len as %d and buf as %lf\n", argv[2], len, buf); len = Rio_readn (serverfd, (void *)&buf, sizeof(double)); if (len < 0) { unix_error ("Rio_readlineb error"); } printf("Host %s got len as %d and buf as %lf\n", argv[2], len, buf); buf = 99.104; int retval = Rio_writen (serverfd, (void *)&buf, sizeof(double)); if (retval < 0) { printf("Rio_writen to %d encountered a problem.\n", serverfd); unix_error ("Rio_writen error"); } retval = Rio_writen (serverfd, (void *)&buf, sizeof(double)); if (retval < 0) { printf("Rio_writen to %d encountered a problem.\n", serverfd); unix_error ("Rio_writen error"); } } return 0; }
int main(int argc, char *argv[]) { int listenfd, connfd; char hostname[MAXLINE], port[MAXLINE]; socklen_t clientlen; struct sockaddr_storage clientaddr; char *common_port = "15618"; listenfd = Open_listenfd (common_port); sleep(5); total_workers = atoi(argv[3]); numchunks = atoi(argv[4]); numtrain = atoi(argv[5]); dimensions = atoi(argv[6]); prefix_input_file_name = std::string(argv[7]); prefix_label_file_name = std::string(argv[8]); printf("Hostname %s is listening on port %s with listenfd = %d\n", argv[1], common_port, listenfd); printf("Node is %s and Master is %s. Number of workers is %d\n", argv[1], argv[2], total_workers); printf("Number of shards (chunks) = %d\n", numchunks); printf("Number of traning points for each expert = %d, with D = %d\n", numtrain, dimensions); printf("Input file prefix: %s, Label file prefix = %s\n", prefix_input_file_name.c_str(), prefix_label_file_name.c_str()); if (strcmp(argv[1], argv[2]) == 0) { for (int i = 0; i < total_workers - 1; i++) { clientlen = sizeof (clientaddr); // accept connections connfd = Accept (listenfd, (SA *) & clientaddr, &clientlen); Getnameinfo ((SA *) & clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf ("Accepted connection from (%s, %s). Connfd is %d\n", hostname, port, connfd); worker_conn_fds.push_back(connfd); int new_worker_id = i + 1; Rio_writen (connfd, (void *)&new_worker_id, sizeof(int)); } } else { connfd = Open_clientfd (argv[2], common_port); printf("Host %s connected to master, connfd is %d\n", argv[1], connfd); Rio_readn (connfd, (void *)&worker_id, sizeof(int)); printf("Host %s got worker id as %d\n", argv[1], worker_id); } if (strcmp(argv[1], argv[2]) == 0) { printf("Master calling cg_solve()\n"); setup(numtrain, dimensions); BCM_log_hyperparams = new double[3]; Eigen::VectorXd initval(3); for(int i = 0 ; i < 3; i++){ initval[i] = 2.0; } set_loghyper_eigen_multinode(initval); double startime = CycleTimer::currentSeconds(); cg_solve(argv[1]); double endtime = CycleTimer::currentSeconds(); printf("TOTAL training time = %lf\n", endtime - startime); destruct_cublas_cusoler(); // testing_phase(numtrain,numtrain); } else { printf("Worker skipping cg_solve(), instead calling accept_commands\n"); setup(numtrain, dimensions); accept_commands(argv[1], connfd); } return 0; }