int clientMethod(int* arg) { char responseBuffer1[10]; char responseBuffer2[10]; minithread_fork(serverMethod, NULL); network_address_t my_address; network_get_my_address(my_address); minisocket_error error; //create client client = minisocket_client_create(my_address, PORT_NUM, &error); assert(client != NULL); assert(error == SOCKET_NOERROR); //receive first 10 bytes int response = minisocket_receive(client, responseBuffer1, 10, &error); assert(response == 10); assert(strcmp(responseBuffer1, "aaaaaaaaaa") == 0); //receive second 10 bytes response = minisocket_receive(client, responseBuffer2, 10, &error); assert(response == 10); assert(strcmp(responseBuffer2, "aaaaaaaaaa") == 0); minithread_sleep_with_timeout(15000); //Make sure that the port does not exist response = minisocket_send(client, aText, strlen(aText), &error); assert(response == -1); assert(error == SOCKET_INVALIDPARAMS); error = SOCKET_NOERROR; fprintf(stderr, "Test Part 1 Passed \n"); //recreate client client = minisocket_client_create(my_address, PORT_NUM, &error); assert(client != NULL); assert(error == SOCKET_NOERROR); //send from client int packets_sent = minisocket_send(client, bText, strlen(bText), &error); assert(packets_sent == strlen(bText)); assert(error == SOCKET_NOERROR); minisocket_close(client); minithread_sleep_with_timeout(15000); //assert that minisocket has closed response = minisocket_send(client, aText, strlen(aText), &error); assert(response == -1); assert(error == SOCKET_INVALIDPARAMS); return 0; }
static int receiver(int* arg) { int len; char buffer[BUFFER_SIZE]; network_address_t dest; if (NULL != remote_name) { network_translate_hostname(remote_name, dest); connection = minisocket_client_create(dest, port_num, &error); } else { connection = minisocket_server_create(port_num, &error); } if (NULL == connection) { printf("Connection error.\n"); return -1; } else { minithread_fork(sender, NULL); printf("Connection successful. Type and enter to send messages.\n"); } while (1) { buffer[BUFFER_SIZE] = '\0'; len = minisocket_receive(connection, buffer, BUFFER_SIZE - 1, &error); buffer[len] = '\0'; printf("[Received message]: %s", buffer); } return 0; }
int receive(int* arg) { char buffer[BUFFER_SIZE]; int i; int bytes_received; network_address_t my_address; minisocket_t socket; minisocket_error error; /* * It is crucial that this minithread_yield() works properly * (i.e. it really gives the processor to another thread) * othrevise the client starts before the server and it will * fail (there is nobody to connect to). */ minithread_yield(); network_get_my_address(my_address); int x; /* create a network connection to the local machine */ socket = minisocket_client_create(my_address, port,&error); if (socket==NULL){ printf("3ERROR: %s. Exiting. \n",GetErrorDescription(error)); return -1; } //semaphore_P(done_sending); /* receive the message */ bytes_received=0; while (bytes_received!=BUFFER_SIZE/20){ int received_bytes; if ((received_bytes=minisocket_receive(socket,buffer, 2, &error))==-1){ printf("inside, socket status = %d [%p]\n", socket->status, socket); printf("4ERROR: %s. Exiting. \n",GetErrorDescription(error)); /* close the connection */ minisocket_close(socket); return -1; } /* test the information received */ for (i=0; i<received_bytes; i++){ if (buffer[i]!=(char)( (bytes_received+i)%256 )){ printf("The %d'th byte received is wrong ('%d'-'%d'-'%d'-'%d' vs '%d'-'%d'-'%d'-'%d') [%d].\n", bytes_received+i, buffer[i-3], buffer[i-2], buffer[i-1], buffer[i], (bytes_received+i-3)%256, (bytes_received+i-2)%256, (bytes_received+i-1)%256, (bytes_received+i)%256, received_bytes); /* close the connection */ minisocket_close(socket); return -1; } } bytes_received+=received_bytes; } printf("All bytes received correctly (received %d bytes).\n", bytes_received); minisocket_close(socket); return 0; }
int receive(int* arg) { char buffer[BUFFER_SIZE]; int i; int bytes_received; network_address_t my_address; minisocket_t socket; minisocket_error error; /* * It is crucial that this minithread_yield() works properly * (i.e. it really gives the processor to another thread) * othrevise the client starts before the server and it will * fail (there is nobody to connect to). */ minithread_yield(); network_get_my_address(my_address); /* create a network connection to the local machine */ printf("creating client\n"); socket = minisocket_client_create(my_address, port,&error); printf("client unblocked\n"); if (socket==NULL){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); return -1; } /* receive the message */ bytes_received=0; while (bytes_received!=BUFFER_SIZE){ int received_bytes; if ((received_bytes=minisocket_receive(socket,buffer, BUFFER_SIZE-bytes_received, &error))==-1){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); /* close the connection */ minisocket_close(socket); return -1; } printf("checking bytes\n"); /* test the information received */ for (i=0; i<received_bytes; i++){ if (buffer[i]!=(char)( (bytes_received+i)%256 )){ printf("The %d'th byte received is wrong.\n", bytes_received+i); /* close the connection */ minisocket_close(socket); return -1; } } bytes_received+=received_bytes; } printf("All bytes received correctly.\n"); minisocket_close(socket); return 0; }
int clientMethod(int* arg) { char responseBuffer1[1]; char responseBuffer2[20]; char responseBuffer3[10001]; minithread_fork(serverMethod, NULL); network_address_t my_address; network_get_my_address(my_address); minisocket_error error; //create client client = minisocket_client_create(my_address, PORT_NUM, &error); assert(client != NULL); assert(error == SOCKET_NOERROR); //Test sending output int response = minisocket_receive(client, responseBuffer1, 1, &error); assert(response == 1); assert(responseBuffer1[0] == 'a'); response = minisocket_receive(client, responseBuffer2, 20, &error); assert(response == 20); assert(strcmp(responseBuffer2, "bbbbbbbbbbbbbbbbbbbb") == 0); int bytes_received = 0; while (bytes_received != 10000) { response = minisocket_receive(client, responseBuffer3 + bytes_received, 10000, &error); assert(response > 0); bytes_received += response; } assert(strcmp(responseBuffer3, cText) == 0); fprintf(stderr, "All Tests Passed \n"); return 0; }
int client(int* arg) { network_address_t address; minisocket_t socket; minisocket_error error; char msg[strlen("hello\n") + 1]; network_translate_hostname(hostname, address); /* create a network connection to the local machine */ socket = minisocket_client_create(address, port,&error); printf("made a client\n"); minisocket_receive(socket, msg, strlen("hello\n")+1, &error); return 0; }
int client(int* arg) { char buffer[BUFFER_SIZE]; int i; int bytes_received; network_address_t address; minisocket_t socket; minisocket_error error; network_translate_hostname(hostname, address); /* create a network connection to the local machine */ socket = minisocket_client_create(address, port,&error); if (socket==NULL){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); return -1; } /* receive the message */ bytes_received=0; while (bytes_received!=BUFFER_SIZE){ int received_bytes; if ( (received_bytes=minisocket_receive(socket,buffer,BUFFER_SIZE-bytes_received, &error))==-1){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); /* close the connection */ minisocket_close(socket); return -1; } /* test the information received */ for (i=0; i<received_bytes; i++){ if (buffer[i]!=(char)( (bytes_received+i)%256 )){ printf("The %d'th byte received is wrong.\n", bytes_received+i); /* close the connection */ minisocket_close(socket); return -1; } } bytes_received+=received_bytes; } printf("All bytes received correctly.\n"); minisocket_close(socket); return 0; }
int serverMethod(int* arg) { char responseBuffer[30]; minisocket_error error; //create server server = minisocket_server_create(PORT_NUM, &error); assert(error == SOCKET_NOERROR); assert(server != NULL); //send from server int packets_sent = minisocket_send(server, aText, strlen(aText), &error); assert(packets_sent == strlen(aText)); assert(error == SOCKET_NOERROR); minisocket_close(server); minithread_sleep_with_timeout(15000); int response = minisocket_send(server, aText, strlen(aText), &error); assert(response == -1); assert(error == SOCKET_INVALIDPARAMS); error = SOCKET_NOERROR; //recreate server server = minisocket_server_create(PORT_NUM, &error); assert(error == SOCKET_NOERROR); assert(server != NULL); response = minisocket_receive(server, responseBuffer, 30, &error); assert(response == 20); assert(strcmp(responseBuffer, "bbbbbbbbbbbbbbbbbbbb") == 0); minithread_sleep_with_timeout(15000); //Assert that minisocket has closed response = minisocket_send(server, aText, strlen(aText), &error); assert(response == -1); assert(error == SOCKET_INVALIDPARAMS); fprintf(stderr, "Full Test Passed \n"); return 0; }
int receiver(int* arg) { char buffer[BUFFER_SIZE]; int id = *arg; int bytes_received; minisocket_error error; int received_bytes; /* receive the message */ bytes_received=0; while (bytes_received != BUFFER_SIZE) { received_bytes = BUFFER_SIZE-bytes_received; received_bytes = minisocket_receive(recv_skt, buffer + bytes_received, RECEIVER_SIZE, &error); if (received_bytes<0) { printf("thread %d. Terminated. Code: %d\n", id, error); return 0; } bytes_received+=received_bytes; } printf("thread %d. Terminated. Success.\n"); return 0; }