int main(){ int sock = -1; sock = listen_socket_init(SOCK_DGRAM, 0, SERVER_PORT); if(sock < 0){ printf("init listen sock failed\n"); return -1; } rx_tx_loop(sock); return 0; }
/* * Listen on the specific port for the incomming connections */ void * listen_thread(void *args) { msg_mng_loop_args *mma; pthread_t *hm_thrs; void *status; int i, eid, port, ret, id; int fd, ns, mult = 1; mma = NULL; port = PORT; if ((fd = listen_socket_init(port)) < 0) { ret = errno; goto err; } for (;;) { for (i = 0; i < MAX_THREADS * mult; i++) { if(_PAXOS_DEBUG>2)pax_log(LOG_DEBUG,"Pasive contacting: %d\n", i); if ((ns = listen_socket_accept(fd, &eid, &id)) == SOCKET_CREATION_FAILURE) { ret = errno; goto err; } if ((mma = malloc(sizeof (msg_mng_loop_args))) == NULL) { pax_log(LOG_ERR,"listen_thread: can't allocate memory: %s\n", strerror(errno)); ret = errno; goto err; } mma->fd = ns; mma->eid = eid; mma->m_index = id; if ((ret = pthread_create(&(node.machtab[eid].hm_thr), NULL, msg_mng_loop, (void *) mma)) != 0) { pax_log(LOG_ERR,"can't create thread for site: %s\n", strerror(errno)); free(mma); goto err; } } } /* If we fell out, we ended up with too many threads. */ ret = ENOMEM; /* Do not return until all threads exited. */ while (--i >= 0) if (pthread_join(hm_thrs[i], &status) != 0) pax_log(LOG_ERR,"can't join site thread: %s\n", strerror(errno)); err: return (ret == 0 ? (void *) EXIT_SUCCESS : (void *) EXIT_FAILURE); }