int main(int argc, char *argv[]) { int sock; struct sockaddr_in addr; int port = get_port(argc, argv); char server_address_string[16]; char buf[BUF_SIZE]; char repeat = 1; int id; set_address(argc, argv, server_address_string, LENGTH_OF(server_address_string)); srand(time(NULL)); id = rand(); sock = socket(AF_INET, SOCK_STREAM, 0); if(WRONG_SOC(sock)) { perror("socket"); exit(1); } addr.sin_family = AF_INET; addr.sin_port = htons(port); /* addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); */ addr.sin_addr.s_addr = inet_addr(server_address_string); if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("connect"); exit(2); } printf("%s:%d, ID: %d\n", server_address_string, port, id); send(sock, &id, sizeof(id), EMPTY_FLAGS); while(repeat) { printf("-> "); read_stdin(buf, LENGTH_OF(buf)); if(strlen(buf) == 0) continue; if(!strncmp(buf, "close", LENGTH_OF(buf))) repeat = 0; send_to(sock, buf); // download file if its exists if(strstr(buf, "download") && receive_int(sock)) { receive_file(sock); printf("%s\n", FILE_DOWNLOADED_MSG); continue; } // prints info or error mesages receive_and_print_msg(sock); } close(sock); return 0; }
void receive_file(int sock) { char file_name[BUF_SIZE]; int file_size, buf_size, bytes, readed_bytes = 0; FILE *f; //receive file name readed_bytes = recv(sock, file_name, LENGTH_OF(file_name), 0); file_name[readed_bytes] = '\0'; //receive file size file_size = receive_int(sock); //receive buf size buf_size = receive_int(sock); print_file_info(file_name, file_size, buf_size); if((f = fopen(file_name, "wb")) == NULL) { printf("Can't create file\n"); return; } char buf[buf_size]; do { bytes = recv(sock, buf, LENGTH_OF(buf), 0); printf("%i\n", bytes); // if last package if(bytes < buf_size - 1){ char new_buf[bytes]; strncpy(new_buf, buf, bytes); new_buf[bytes] = '\0'; fprintf(f, "%s", new_buf); } else { fprintf(f, "%s", buf); fseek(f, readed_bytes, SEEK_SET); } readed_bytes += bytes; printf("%i\n", readed_bytes); } while(bytes >= buf_size - 1); fclose(f); }
/*** receive_str ****************************************************** Receive a character string. This function will block if the data is not available. Input: socket - the socket to receive the string from. str - pointer to char array to put result into. It is assumed to be of sufficient size. Output: str will have the string received Return: 0 if the connection was lost non-zero if successful **********************************************************************/ int receive_str( int socket, char str[] ) { int len; if ( receive_int(socket, &len) ) { if ( receive_data(socket, str, len)==len ) { str[len] = '\0'; return 1; } } return 0; }
Service* service_request (string service_name) { int socket; Service_description* s_description = NULL; if (service_name == "" || !socket_initialization_client (&socket, SR_address, SR_port)) return NULL; if (send_string (socket, "get_service") && receive_int (socket) != -1 && send_string (socket, service_name)) s_description = receive_service_description (socket); close(socket); return (s_description == NULL) ? NULL : new Service (s_description); }
bool register_service (Service_description* s_description) { int socket; bool result = false; if (s_description == NULL || !socket_initialization_client (&socket, SR_address, SR_port)) return false; if (send_string (socket, "add_service") && receive_int (socket) != -1 && send_service_description(socket, s_description)) result = true; close(socket); return result; }
bool register_service_provider (string address, string port) { int socket; bool result = false; if (!check_address (address) || atoi(port.c_str()) < 1023 || atoi(port.c_str()) > 65535 || !socket_initialization_client (&socket, SR_address, SR_port)) return false; if (send_string (socket, "add_service_provider") && receive_int (socket) != -1 && send_string (socket, address) && send_string (socket, port)) result = true; close(socket); return result; }
int main(int argc, char ** argv){ /* variables declaration */ int i; int *size_of_buffer = 0; int num_of_ones = 0; /* Number of rounds that the algorithm will run */ int rounds = 0; /* buffer that master will send */ char *buffer = NULL; /* sessionC handler */ session *s; join_session(&argc, &argv, &s, "Slave3.spr"); role *Master = s->get_role(s, "Master"); /* receive size of buffer */ receive_int(Master, &size_of_buffer); printf("size of buffer is %d\n", *size_of_buffer); /* Dynamic memory allocation */ buffer = (char *) malloc( (*size_of_buffer) * sizeof(char)); /* Receive the buffer */ receive_string(Master, &buffer); /* For 100000 rounds */ while(rounds++ < 100000){ /* Compute the number of ones */ num_of_ones = number_of_ones(buffer); } printf("num of ones is %d\n", num_of_ones); /* Send the result to the master node */ send_int(Master, num_of_ones); /* Deallocate memory */ free(buffer); /* End current session */ end_session(s); return EXIT_SUCCESS; }
/* Main function */ int main(int argc, char **argv){ /* Variables declaration */ int i; int j; /* The number of non zero elements */ int *num_of_no_zeros = NULL; /* number of rounds */ int rounds = 0; /* Session start */ session *s; join_session(&argc, &argv, &s, "worker3.spr"); role *Master = s->get_role(s, "Master"); printf("I am here\n"); /* Receive number of non zero elements */ receive_int(Master, &num_of_no_zeros); printf("NUm of zeros is %d\n", *num_of_no_zeros); /* Number of non zero elements */ int no_zeros = *num_of_no_zeros; /* Declaration and dynamic memory allocation af array row_ptr */ int *row_ptr = (int *) malloc((nrows + 1) * sizeof(int)); if(row_ptr == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Decalration and dynamic memory allocation af array values */ int *values = (int *) malloc(no_zeros * sizeof(int)); if(values == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Declaration and dynamic memory allocation af col_ind array */ int *col_ind = (int *) malloc(no_zeros * sizeof(int)); if(col_ind == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Declaration and dynamic memory allocation af x array */ int *x = (int *) malloc(ncolumns * sizeof(int)); if(x == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Expected size of array */ size_t sz_rows = 1001; size_t sz_cols = 1000; size_t sz_no_zeros = no_zeros; /* Receive row_ptr from master */ recv_int_array(Master, row_ptr, &sz_rows); /* Receive values array from master */ recv_int_array(Master, values, &sz_no_zeros); /* Receive col_ind array from master */ recv_int_array(Master, col_ind, &sz_no_zeros); /* Receive x array from master */ recv_int_array(Master, x, &sz_cols); printf("value[%d] = %d\n", 0, values[0]); /* The array that will hold the results of the main computation */ int *results = NULL; /* Define the amount of work that worker 1 will do */ int start_work; int end_work; int amount_of_work; /* The amount of work that the master will do */ start_work = floor((worker3 * nrows)/participants) / 10; end_work = floor(((worker3 + 1) * nrows)/participants) / 10; amount_of_work = (end_work - start_work); printf("start = %d - end = %d - amount of work = %d\n", start_work, end_work, amount_of_work); /* Declaration and dynamic memory allocation af x array */ results = (int *) malloc( 2 * amount_of_work * sizeof(int)); if(results == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Run for 100 rounds */ while(rounds++ < 10000){ /* Main computation of the result. Worker1 computes the work that is assigned to it*/ for(i = start_work; i < end_work; i++){ for(j = row_ptr[i]; j < row_ptr[i + 1]; j++) results[i] += values[j] * x[col_ind[j]]; } } int array[amount_of_work]; for(i = 0; i < amount_of_work; i++) array[i] = 0; //send_int_array(Master, array, amount_of_work); //printf("here i am \n"); /* Send the results to the master */ //send_int_array(Master, values, amount_of_work); send_int_array(Master, results, amount_of_work); //send_int(Master, amount_of_work); //recv_int_array(Master, col_ind, &sz_no_zeros); printf("gamw!!!\n"); /* End current session */ end_session(s); /* Free memory */ free(row_ptr); row_ptr = NULL; free(values); values = NULL; free(col_ind); col_ind = NULL; free(x); x = NULL; free(results); results = NULL; return EXIT_SUCCESS; }