#include #include #include #include int create_socket(int ); main(int argc, char** argv) { int i, connection, port, descriptor; int size; float factor1, factor2, product; /*****************************************************************/ /* Check command line arguments */ if(argc != 3) { printf("Usage: my_server -p # \n"); printf(" where -p # is port number\n"); exit(0); } /* Get the argument after -p and assign it to port */ for( i = 0; i < argc; i++) { if( !strcmp(*(argv + i), "-p")) { port = (int) atoi( *(argv+i+1)); } } /* Make the socket and connection */ descriptor = create_socket(port); if(descriptor == 0) { printf("failed to make a socket\n"); exit(0); } /*****************************************************************/ connection = 1; while(connection != 0) { i = read(descriptor, (char*) &connection, sizeof(int)); printf("I have just read %d bytes representing %d\n",i,connection); if(connection == 0) { printf("I have been told my services are no longer needed\n"); } else { i = read(descriptor, (char *) &factor1, sizeof(float)); printf("I have just read %d bytes representing %f\n",i,factor1); i = read(descriptor, (char *) &factor2, sizeof(float)); printf("I have just read %d bytes representing %f\n",i,factor2); product = factor1 * factor2; i = write(descriptor, (char *) &product, sizeof(float)); printf("I have just WRITTEN %d bytes representing %f\n",i,product); } } close(descriptor); printf("Goodbye now\n"); }