00001 /* 00002 FIFO-based client 00003 AUP2, Sec. 7.02.2 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 #include "smsg.h" 00023 00024 #define WANT_BUGFIX 00025 00026 /* Client */ 00027 /* Assumes no partial reads */ 00028 00029 /*[main]*/ 00030 int main(int argc, char *argv[]) 00031 { 00032 int fd_server, fd_client = -1, i; 00033 #ifdef WANT_BUGFIX 00034 int fd_client_w = -1; 00035 #endif 00036 ssize_t nread; 00037 struct simple_message msg; 00038 char fifo_name[100]; 00039 char *work[] = { 00040 "applesauce", 00041 "tiger", 00042 "mountain", 00043 NULL 00044 }; 00045 00046 printf("client %ld started\n", (long)getpid()); 00047 msg.sm_clientpid = getpid(); 00048 ec_false( make_fifo_name(msg.sm_clientpid, fifo_name, 00049 sizeof(fifo_name)) ) 00050 if (mkfifo(fifo_name, PERM_FILE) == -1 && errno != EEXIST) 00051 EC_FAIL 00052 ec_neg1( fd_server = open(SERVER_FIFO_NAME, O_WRONLY) ) 00053 for (i = 0; work[i] != NULL; i++) { 00054 strcpy(msg.sm_data, work[i]); 00055 ec_neg1( write(fd_server, &msg, sizeof(msg)) ) 00056 if (fd_client == -1) 00057 ec_neg1( fd_client = open(fifo_name, O_RDONLY) ) 00058 #ifdef WANT_BUGFIX 00059 if (fd_client_w == -1) 00060 ec_neg1( fd_client_w = open(fifo_name, O_WRONLY) ) 00061 #endif 00062 ec_neg1( nread = read(fd_client, &msg, sizeof(msg)) ) 00063 //printf("client got %d from read\n", nread); 00064 if (nread == 0) { 00065 errno = ENETDOWN; 00066 EC_FAIL 00067 } 00068 printf("client %ld: %s --> %s\n", (long)getpid(), 00069 work[i], msg.sm_data); 00070 sleep(i); 00071 } 00072 ec_neg1( close(fd_server) ) 00073 ec_neg1( close(fd_client) ) 00074 #ifdef WANT_BUGFIX 00075 ec_neg1( close(fd_client_w) ) 00076 #endif 00077 ec_neg1( unlink(fifo_name) ) 00078 printf("Client %ld done\n", (long)getpid()); 00079 exit(EXIT_SUCCESS); 00080 00081 EC_CLEANUP_BGN 00082 exit(EXIT_FAILURE); 00083 EC_CLEANUP_END 00084 }