예제 #1
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;
}
예제 #2
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;
}
예제 #3
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;

}
예제 #4
0
static int
sender(int *arg)
{
    int len = BUFFER_SIZE;
    char buffer[BUFFER_SIZE];

    while (1) {
        len = miniterm_read(buffer, BUFFER_SIZE - 1);
        minisocket_send(connection, buffer, len + 1, &error);
    }

    return 0;
}
예제 #5
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);

    //Test sending a message of length 0
    int response = minisocket_send(server, aText, 0, &error);
    assert(response == 0);
    assert(error == SOCKET_NOERROR);

    //Test sending a message of length -5
    response = minisocket_send(server, aText, -5, &error);
    assert(response == -1);
    assert(error == SOCKET_INVALIDPARAMS);
    error = SOCKET_NOERROR;

    //Test sending a message of length 1, less than length of text buffer
    response = minisocket_send(server, aText, 1, &error);
    assert(response == 1);
    assert(error == SOCKET_NOERROR);

    //Test sending a message with NULL socket
    response = minisocket_send(NULL, aText, 1, &error);
    assert(response == -1);
    assert(error == SOCKET_INVALIDPARAMS);
    error = SOCKET_NOERROR;

    //Test sending a message with NULL message
    response = minisocket_send(server, NULL, 10, &error);
    assert(response == -1);
    assert(error == SOCKET_INVALIDPARAMS);
    error = SOCKET_NOERROR;

    //Test sending a message with length equal to length of strin buffer
    response = minisocket_send(server, bText, 20, &error);
    assert(response == 20);
    assert(error == SOCKET_NOERROR);

    //Test sending a message of length > MAX_LENGTH of message 
    response = minisocket_send(server, cText, 10000, &error);
    assert(response == 10000);
    assert(error == SOCKET_NOERROR);

    return 0;

}
예제 #6
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;
}
예제 #7
0
int server(int* arg) {
  minisocket_error error;
  minisocket_t socket;
  char* msg;
  int data_len;

  msg = "hello\n";
  data_len = strlen(msg) + 1;
  socket = minisocket_server_create(port,&error);
  printf("made a server\n");
  if (socket==NULL){
    printf("ERROR: %s. Exiting. \n",GetErrorDescription(error));
    return -1;
  }
  minisocket_send(socket, msg, data_len, &error);
  printf("sent my thang!\n");
  return 0;
}
예제 #8
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;
}