Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.92 KB | None | 0 0
  1. /*
  2. Joshua Butler
  3. EECE 4840: Operating Systems & Kernal Design
  4. Lab 4
  5. Written using code provided by Dr. Magherbi & John DiZoglio.
  6. */
  7.  
  8.  
  9.  
  10.  
  11.  
  12. #include<sys/types.h>
  13. #include<sys/socket.h>
  14. #include<netdb.h>
  15. #include<netinet/in.h>
  16. #include<stdio.h>
  17. #include<stdlib.h>
  18. #include<sys/socket.h>
  19. #include<sys/time.h>
  20. #include<errno.h>
  21. #include<arpa/inet.h>
  22. #include<string.h>
  23. #include<time.h>
  24. #include <malloc.h>
  25. #include <sys/param.h>
  26. #include <sys/stat.h>
  27. #include <sys/fcntl.h>
  28. #include <stddef.h>
  29. #include <dirent.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <pthread.h>
  33. #include <semaphore.h>
  34. #define MSGL 1024 //message length maximum
  35. #define PERMS 0666
  36.  
  37. #define RECEIVER_HOST "anaconda2.uml.edu" /* Server machine */
  38.  
  39. // timeout checking
  40. #define SUCCESS 0
  41. #define SEND_FAILURE 1
  42. #define RC_FAILURE 2
  43.  
  44. // used for AnythingThere function
  45.  
  46. #define TIMEOUT_SECONDS 3 //seconds until timeout
  47. #define NOTHING_THERE 0
  48. #define SOMETHING_THERE 1
  49.  
  50. #define TRIALS 3
  51. int s; //global variable for easy access. (big no-no)
  52.  
  53. typedef int Socket;
  54.  
  55. typedef char Command;
  56.  
  57. typedef struct sockaddr_in SocketAddress;
  58.  
  59. typedef int Status;
  60.  
  61. int BUFSIZE = 200;
  62. char received[200];
  63.  
  64. void to_server(char *msg);
  65.  
  66. /* Declaring errno */
  67. extern int errno;
  68.  
  69. /* Function for error */
  70. void report_error(char *s)
  71. {
  72. printf("sender: error in %s, errno = %d\n",s,errno);
  73. exit(1);
  74. }
  75.  
  76. int AnythingThere(int s);
  77.  
  78. /* Giving 'size' of message dynamically as argument */
  79. void main(int argc, char *argv[])
  80. {
  81. int i;
  82. char msg[BUFSIZE];
  83.  
  84. //this loop keeps the command prompt open until the user enters 'quit'.
  85. while(1)
  86. {
  87. printf("Enter 'quit' to exit or enter the message to be sent ('ls', or 'cd'): \n");
  88. scanf("%s",msg);
  89.  
  90. if(strcmp(msg,"quit") == 0)
  91. {
  92. exit(1);
  93. }
  94.  
  95. pthread_t tid;
  96. pthread_attr_t attr;
  97. pthread_attr_init(&attr);
  98.  
  99. // This loop implements the timeout. If the server does not respond it
  100. // AnythingThere will return as false and there will be 3 attempts (3 seconds apart) to
  101. // try again. After 3 attempts the command prompt will return and the program will not be hung up.
  102. for(i = 0; i < TRIALS; i++)
  103. {
  104.  
  105. pthread_create(&tid,&attr,to_server,msg); //creates thread
  106. if(!AnythingThere(s))
  107. {  
  108.  printf("Failed to get response (%d)\n", i+1);
  109. pthread_cancel(tid);    //cancel thread
  110. }
  111.  
  112. else if(AnythingThere(s))
  113. {
  114. break; //exit this for loop success.
  115. }
  116.  
  117. }
  118. printf("Failed to connect after 3 attempts!\n");
  119. }
  120. }
  121.  
  122. //Moved this code to its own function since it is easier to make this into a thread
  123. // and kill cancel it if there is a failure to get a response from the server.
  124. void to_server(char* msg)
  125. {
  126.  
  127. int i;
  128. struct sockaddr_in sa= {0};
  129. int length = sizeof(sa);
  130. struct hostent *hp;
  131.  
  132.  
  133.  
  134. /* FILL SOCKET ADDRESS*/
  135. if((hp = gethostbyname(RECEIVER_HOST))==NULL)
  136. report_error("gethostbyname");
  137. bcopy((char*)hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
  138. sa.sin_family = hp->h_addrtype;
  139. sa.sin_port = htons(6303+ 20000); /* define port
  140. number based on student ID*/
  141.  
  142. /* Creating the socket and returns error if unsuccessfull */
  143. if((s=socket(AF_INET, SOCK_DGRAM, PF_UNSPEC))== -1)
  144. report_error("socket");
  145. printf("Socket= %d\n",s);
  146.  
  147. /* Sending the message to server and returns error if unsuccesfull */
  148. if(sendto(s, msg, BUFSIZE, 0, (struct sockaddr *) &sa, length)== -1)
  149. report_error("sendto");
  150.  
  151. /* Receives message from server and returns error if unsuccesfull */
  152. recvfrom(s, received, BUFSIZE, 0, (struct sockaddr *) &sa, &length);
  153. printf("%s\n",received);
  154. close(s);
  155. }
  156.  
  157.  
  158.  
  159. // this function returns as 1 if there is a response server, and 0 if there isn't after 3 seconds.
  160. int AnythingThere(int s)
  161. {
  162.  
  163. fd_set read_mask;
  164. struct timeval timeout;
  165. int ret;
  166.  
  167. timeout.tv_sec = TIMEOUT_SECONDS;
  168. timeout.tv_usec = 0;
  169.  
  170. FD_ZERO (&read_mask);
  171. FD_SET(s, &read_mask);
  172. if((ret=select(32,&read_mask,0,0,&timeout))<0)
  173. report_error("select");
  174. return((ret==0) ? NOTHING_THERE : SOMETHING_THERE);
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement