?? server.c
字號:
/* server.c -- main server source code * * This file is part of 'netcast' program, released under BSD License. * (c) 2001-2002 Stanis砤w Pa秌o <staszek@nutki.com>. All rights reserved. */#include <pthread.h>#include <string.h>#include <stdlib.h>#include "mdist.h"#include "server.h"pthread_t thr[2];int wait_count=2;int client_count=0;int server_port=0;struct sempack *msp;/* Initialize sync data. Could be done with less semaphores, but this is more understandable */void init_sempack(struct sempack *sp) { if (sem_init(&sp->init,0,0)<0) ERROR("sem_init()"); if (sem_init(&sp->accept,0,0)<0) ERROR("sem_init()"); if (sem_init(&sp->prepare,0,0)<0) ERROR("sem_init()"); if (sem_init(&sp->ready,0,0)<0) ERROR("sem_init()"); if (sem_init(&sp->finish,0,0)<0) ERROR("sem_init()");}/* Wrapper for pthread_create, additionaly may initialize/pass thru a sempack parameter */struct sempack* spawn_server (pthread_t *thr, void * (*start) (void *), struct sempack *spi) { struct sempack *sp; if (!spi) { sp = (struct sempack *) malloc(sizeof(struct sempack)); init_sempack(sp); } else sp=spi; if (pthread_create(thr,0,start,(void *) sp)!=0) ERROR("pthread_create"); return sp;} int main(int argc, char *argv[]) { if (argc<2) { printf("Usage: server num_of_clients [port]\n"); return 0; } wait_count=atoi(argv[1]); if (!wait_count) { printf("Usage: server num_of_clients [port]\n"); return 0; } if (argc>2) server_port=atoi(argv[2]); if (!server_port) server_port=DEF_PORT; /* spawn servers */ msp=spawn_server(&thr[0],mcast_srv_main,0); spawn_server(&thr[1],mdtcp_srv_main,msp); /* and wait till they finish */ if (pthread_join(thr[0],0)<0) ERROR("pthread_join()"); if (pthread_join(thr[1],0)<0) ERROR("pthread_join()"); sleep(1); return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -