int main(void) { int x = 0, sock = 0; struct ifreq ifreq; //make a socket if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) perr_quit("socket"); //loop thru indices starting at 1, print each one out //quit on a bad one for(x = 1; x < 10; x++){ ifreq.ifr_ifindex = x; if(ioctl(sock, SIOCGIFNAME, &ifreq) < 0 ) perr_quit("ioctl"); printf("index %d is %s\n", x, ifreq.ifr_name); } return 0; }
int main(int argc, char **argv) { u_int rbufsz = 64000; int on = 1, fd = 0, lsock = 0, asock = 0, nread = 0, nthreads = 0, x = 0; char buf[BUFSIZE]; struct sockaddr_in sa, csa; struct in_addr *inptr = NULL; socklen_t len = sizeof(sa); pthread_t threadid[MAXTHREADS]; //initialize the socket -- returns < 0 on error if ( (lsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) perr_quit("socket"); if(setsockopt(lsock, SOL_SOCKET, SO_RCVBUF, &rbufsz, sizeof(rbufsz)) != 0) perr_quit("setsockopt"); if(setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) perr_quit("setsockopt"); //setup the sock_addrin structure memset(&sa, 0, sizeof(sa)); memset(&csa, 0, sizeof(csa)); sa.sin_family = AF_INET; sa.sin_port = htons(PORT); sa.sin_addr.s_addr = htonl(INADDR_ANY); //bind socket if( (bind(lsock, (struct sockaddr *) &sa, sizeof(sa))) < 0) err_quit("bind"); //go into listen mode if( (listen(lsock, 5)) < 0) err_quit("listen"); while(1){ //accept a connection if( (asock = accept(lsock, (struct sockaddr *)&csa, &len)) < 0) perr_quit("accept"); printf("address is %d.%d.%d.%d\n", NIPQUAD(csa.sin_addr.s_addr) ); if(setsockopt(lsock, SOL_SOCKET, SO_RCVBUF, &rbufsz, sizeof(rbufsz)) != 0) perr_quit("setsockopt"); //spawn a thread for each clien if(pthread_create(&threadid[x++], NULL, (void *)&f00, (int)asock) != 0) perr_quit("pthread"); if(nthreads++ >= MAXTHREADS) break; } //wait for all threads for(x = 0; x < nthreads; x++) if(pthread_join(threadid[x], NULL) < 0) perr_quit("pthread join"); //cleanup close(lsock); return 0; }