// // File name: netc.C // // This file is written by Ee-Chien Chang, and with minor change // by Ting-jen Yen. // It is the main program of the client. // #include #include #include #include #include #include #include #include #include // read(), write(), close() #include #include // thread #include "tcp.h" #include "tcpstream.h" //----------- S H A R E D _ M E M O R Y ------------------- #include "sparse_mask.h" // must be included for GLOBAL_VAR.h #include "sparse_2.h" // must be included for GLOBAL_VAR.h #include "GLOBAL_VARIABLE.h" void client_network ( tcp_Stream * ); // file client.c void *display_thread ( void * ); // display.c main(int argc,char ** argv) { FILE *fp; // optional char hostname[80]; int sd; int p_argc, port_no = 0; p_argc = 0; // Look for port option if (argc > p_argc+2 && !strcmp(argv[p_argc+1], "-p")) { port_no = atoi(argv[p_argc+2]); p_argc += 2; } // Look for server name. If there is none, using default value if (argc > p_argc+1) { p_argc++; strcpy(hostname, argv[p_argc]); } else strcpy(hostname, "slinky.cs.nyu.edu"); // look for port number again if (argc > p_argc+2 && !strcmp(argv[p_argc+1], "-p")) { port_no = atoi(argv[p_argc+2]); p_argc += 2; } // default port number 1222 if (port_no == 0) port_no = 1222; printf("Connecting %s %d\n", hostname, port_no); sd = establish_connection ( hostname, port_no, &fp ); tcp_Stream netstream ( sd ); ptr_stream = & netstream; //------------ initialise semaphore ------------------// sem_init ( &DATA_READY , 0, 1 ); sem_init ( &NEW_DATA_READY, 0, 1); sem_init ( &QUEUE_EMPTY, 0, 0); // And initialize mutex exclusive locks pthread_mutex_init( &USE_QUEUE, NULL); pthread_mutex_init( &END_THREAD, NULL); pthread_mutex_lock( &END_THREAD); sem_wait ( &DATA_READY ); // hold it. will be release by network thread Q.startup(); // Send first request //------------ start the //------------ D I S P L A Y t h r e a d. pthread_t display_id; pthread_create ( &display_id, NULL, display_thread, NULL ); //------------ the main thread will continue to be the //------------ N E T W O R K t h r e a d. client_network ( &netstream ); //------------ q u i t pthread_mutex_unlock( & END_THREAD); pthread_join ( display_id, NULL ); }