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 transmit(int* arg) { char buffer[BUFFER_SIZE]; int i; int bytes_sent; minisocket_t socket; minisocket_error error; minithread_fork(receive, NULL); // receiver = minithread_fork(receive, NULL); printf("in transmit\n"); socket = minisocket_server_create(port,&error); printf("created a server\n"); if (socket==NULL){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); return -1; } /* Fill in the buffer with numbers from 0 to BUFFER_SIZE-1 */ for (i=0; i<BUFFER_SIZE; i++){ buffer[i]=(char)(i%256); } /* send the message */ bytes_sent=0; while (bytes_sent!=BUFFER_SIZE){ int trans_bytes= minisocket_send(socket,buffer+bytes_sent, BUFFER_SIZE-bytes_sent, &error); printf("Sent %d bytes.\n",trans_bytes); if (error!=SOCKET_NOERROR){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); /* close the connection */ minisocket_close(socket); return -1; } bytes_sent+=trans_bytes; } /* close the connection */ minisocket_close(socket); return 0; }
int receive(int* arg) { network_address_t my_address; minisocket_t socket; minisocket_error error; printf("starting receive\n"); minithread_yield(); printf("continuing receive\n"); network_get_my_address(my_address); /* create a network connection to the local machine */ socket = minisocket_client_create(my_address, port,&error); if (socket==NULL){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); return -1; } printf("client connected\n"); minisocket_close(socket); return 0; }
int sender(int* arg) { char buffer[BUFFER_SIZE]; int i; int id = *arg; int bytes_sent; minisocket_error error; if (server_socket==NULL) { printf("Sender NULL.\n"); return 0; } /* Fill in the buffer with numbers from 0 to BUFFER_SIZE-1 */ for (i=0; i<BUFFER_SIZE; i++) { buffer[i] = i % 128; } /* send the message */ bytes_sent=0; while (bytes_sent != BUFFER_SIZE) { int trans_bytes = minisocket_send(server_socket,buffer+bytes_sent, BUFFER_SIZE-bytes_sent, &error); printf("thread %d. Sent %d bytes.\n", id, trans_bytes); if (trans_bytes==-1) { printf("thread %d. Sending error. Code: %d.\n", id, error); return 0; } bytes_sent+=trans_bytes; } /* close the connection */ minisocket_close(server_socket); 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 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; }
int server(int* arg) { (void)arg; //unused char buffer[BUFFER_SIZE]; minisocket_error error; minisocket_t *socket = minisocket_server_create(port,&error); if (socket==NULL){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); return -1; } // Fill in the buffer with numbers from 0 to BUFFER_SIZE-1 for (int i=0; i<BUFFER_SIZE; i++){ buffer[i]=(char)(i%256); } // send the message int bytes_sent=0; while (bytes_sent!=BUFFER_SIZE){ int trans_bytes= minisocket_send(socket,buffer+bytes_sent, BUFFER_SIZE-bytes_sent, &error); printf("Sent %d bytes.\n",trans_bytes); if (error!=SOCKET_NOERROR){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); /* close the connection */ minisocket_close(socket); return -1; } bytes_sent+=trans_bytes; } 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 server(int* arg) { int port; minisocket_t socket; minisocket_error error; port = *arg; socket = minisocket_server_create(port,&error); if (socket==NULL){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); return -1; } printf("Server connected on port %i\n", port); /* close the connection */ minisocket_close(socket); return 0; }
int client(int* arg) { int port; network_address_t address; minisocket_t socket; minisocket_error error; port = *arg; network_translate_hostname(hostname, address); socket = minisocket_client_create(address, port, &error); if (socket==NULL){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); return -1; } printf("Client connected on port %i\n", port); minisocket_close(socket); return 0; }
int transmit(int* arg) { minisocket_t socket; minisocket_error error; printf("starting transmit\n"); minithread_fork(receive, NULL); printf("continuing transmit\n"); socket = minisocket_server_create(port,&error); if (socket==NULL){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); return -1; } printf("server connected\n"); /* close the connection */ minisocket_close(socket); return 0; }
/* * Send a message to the other end of the socket. * * The send call should block until the remote host has ACKnowledged receipt of * the message. This does not necessarily imply that the application has called * 'minisocket_receive', only that the packet is buffered pending a future * receive. * * It is expected that the order of calls to 'minisocket_send' implies the order * in which the concatenated messages will be received. * * 'minisocket_send' should block until the whole message is reliably * transmitted or an error/timeout occurs * * Arguments: the socket on which the communication is made (socket), the * message to be transmitted (msg) and its length (len). * Return value: returns the number of successfully transmitted bytes. Sets the * error code and returns -1 if an error is encountered. */ int minisocket_send(minisocket_t socket, minimsg_t msg, int len, minisocket_error *error) { int result, send_len; int bytes_sent; mini_header_reliable_t header; //DEBUG // char* the_msg = (char*) msg; // printf("msg: %s length of msg to send: %i\n", msg, len); // Check for valid arguments if (socket == NULL) { fprintf(stderr, "ERROR: minisocket_send() passed NULL minisocket_t\n"); *error = SOCKET_INVALIDPARAMS; // semaphore_V(skt_mutex); return -1; } if (msg == NULL) { fprintf(stderr, "ERROR: minisocket_send() passed NULL minimsg_t\n"); *error = SOCKET_INVALIDPARAMS; // semaphore_V(skt_mutex); return -1; } // Allocate new header header = malloc(sizeof(struct mini_header_reliable)); if (header == NULL) { // Could not allocate header fprintf(stderr, "ERROR: minisocket_send() failed to malloc new mini_header_reliable\n"); *error = SOCKET_OUTOFMEMORY; // semaphore_V(skt_mutex); return -1; } // Exclude other threads from sending from the same socket while I'm sending semaphore_P(socket->sending); bytes_sent = 0; // Fragment long messages into smaller packets while (bytes_sent < len) { socket->seqnum++; send_len = ((len - bytes_sent) > MAX_NETWORK_PKT_SIZE) ? MAX_NETWORK_PKT_SIZE : (len - bytes_sent); // Length of data to send in this packet set_header(socket, header, MSG_ACK); // printf("Message abt to be sent, length of it: (%s, %i)\n", the_msg[0](char*) msg, send_len); result = retransmit_packet(socket, (char*) header, send_len, (/*(char*)*/ msg) + bytes_sent, error); if (result == 1) { // ACK received (packet send successfully) bytes_sent += send_len; } else if (result == 0) { // All timeout attempts failed - close connection semaphore_V(socket->sending); minisocket_close(socket); return -1; } else { // Generic failure semaphore_V(socket->sending); return -1; } } semaphore_V(socket->sending); return 0; }