Example #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;
}
Example #2
0
File: handshake.c Project: obsc/os
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;
}
Example #3
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;
}
Example #5
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;
}
Example #6
0
int client(int* arg)
{
    int i;
    minisocket_error error;
    network_address_t address;

    network_translate_hostname(hostname, address);
    recv_skt = minisocket_client_create(address, port, &error);
    for (i=0; i<THREAD_COUNTER; i++) {
        minithread_fork(receiver,&thread_id[i]);
    }

    return 0;
}
Example #7
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;
}
Example #9
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;
}
Example #10
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;
}