Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. void prepare_data(int32_t *data, int len)
  2. {
  3.         for (int i = 0; i < len; ++i)
  4.         {
  5.                 int val = rand() % 1000;
  6.                 data[i] = htonl(val);
  7.         }  
  8. }
  9. int main(int argc, char** argv) {
  10.         int fd;
  11.         int32_t data[3];
  12.         int32_t data_recv[3];
  13.         if(argc!=3) {
  14.                 usage(argv[0]);
  15.                 return EXIT_FAILURE;
  16.         }
  17.         if(sethandler(SIG_IGN,SIGPIPE)) ERR("Seting SIGPIPE:");
  18.         prepare_data(data, 3);
  19.         //~ for (int i = 0; i < 3; ++i)
  20.         //~ {
  21.                 //~ //struct timeval time = {0, 750000L};
  22.                 //~ fd=connect_socket(argv[1],argv[2]);
  23.                 //~ struct timeval time = {3, 0};
  24.                 //~ fprintf(stdout, "Process [%d] sends val: %d\n", getpid(), ntohl(data[i]));
  25.                 //~ if(bulk_write(fd,(char *)&data[i],sizeof(int32_t[1]))<0) ERR("write:");
  26.                 //~ if(bulk_read(fd,(char *)&data_recv[i],sizeof(int32_t[1]))<(int)sizeof(int32_t[1])) ERR("read:");
  27.                 //~ fprintf(stdout, "Process [%d] received val: %d\n", getpid(), ntohl(data_recv[i]));
  28.                 //~ select(1, NULL, NULL, NULL, &time);
  29.                 //~ if(TEMP_FAILURE_RETRY(close(fd))<0)ERR("close");
  30.         //~ }
  31.         fd=connect_socket(argv[1],argv[2]);
  32.         for (int i = 0; i < 3; ++i)
  33.         {
  34.                 struct timeval time = {3, 0};
  35.                 fprintf(stdout, "Process [%d] sends val: %d\n", getpid(), ntohl(data[i]));
  36.                 if(send(fd, (void*) &data[i], sizeof(int32_t[1]), 0) < 0 ) ERR("send");
  37.  
  38.                 if(recv(fd, (void*) &data[i], sizeof(int32_t[1]), 0) < 0) ERR("recv");
  39.                 fprintf(stdout, "Process [%d] received val: %d\n", getpid(), ntohl(data_recv[i]));
  40.                 select(1, NULL, NULL, NULL, &time);
  41.         }
  42.         if(TEMP_FAILURE_RETRY(close(fd))<0)ERR("close");
  43.  
  44.         return EXIT_SUCCESS;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement