Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Joshua Butler
- EECE 4840: Operating Systems & Kernal Design
- Lab 4
- Written using code provided by Dr. Magherbi & John DiZoglio.
- */
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<netdb.h>
- #include<netinet/in.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/socket.h>
- #include<sys/time.h>
- #include<errno.h>
- #include<arpa/inet.h>
- #include<string.h>
- #include<time.h>
- #include <malloc.h>
- #include <sys/param.h>
- #include <sys/stat.h>
- #include <sys/fcntl.h>
- #include <stddef.h>
- #include <dirent.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <semaphore.h>
- #define MSGL 1024 //message length maximum
- #define PERMS 0666
- #define RECEIVER_HOST "anaconda2.uml.edu" /* Server machine */
- // timeout checking
- #define SUCCESS 0
- #define SEND_FAILURE 1
- #define RC_FAILURE 2
- // used for AnythingThere function
- #define TIMEOUT_SECONDS 3 //seconds until timeout
- #define NOTHING_THERE 0
- #define SOMETHING_THERE 1
- #define TRIALS 3
- int s; //global variable for easy access. (big no-no)
- typedef int Socket;
- typedef char Command;
- typedef struct sockaddr_in SocketAddress;
- typedef int Status;
- int BUFSIZE = 200;
- char received[200];
- void to_server(char *msg);
- /* Declaring errno */
- extern int errno;
- /* Function for error */
- void report_error(char *s)
- {
- printf("sender: error in %s, errno = %d\n",s,errno);
- exit(1);
- }
- int AnythingThere(int s);
- /* Giving 'size' of message dynamically as argument */
- void main(int argc, char *argv[])
- {
- int i;
- char msg[BUFSIZE];
- //this loop keeps the command prompt open until the user enters 'quit'.
- while(1)
- {
- printf("Enter 'quit' to exit or enter the message to be sent ('ls', or 'cd'): \n");
- scanf("%s",msg);
- if(strcmp(msg,"quit") == 0)
- {
- exit(1);
- }
- pthread_t tid;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- // This loop implements the timeout. If the server does not respond it
- // AnythingThere will return as false and there will be 3 attempts (3 seconds apart) to
- // try again. After 3 attempts the command prompt will return and the program will not be hung up.
- for(i = 0; i < TRIALS; i++)
- {
- pthread_create(&tid,&attr,to_server,msg); //creates thread
- if(!AnythingThere(s))
- {
- printf("Failed to get response (%d)\n", i+1);
- pthread_cancel(tid); //cancel thread
- }
- else if(AnythingThere(s))
- {
- break; //exit this for loop success.
- }
- }
- printf("Failed to connect after 3 attempts!\n");
- }
- }
- //Moved this code to its own function since it is easier to make this into a thread
- // and kill cancel it if there is a failure to get a response from the server.
- void to_server(char* msg)
- {
- int i;
- struct sockaddr_in sa= {0};
- int length = sizeof(sa);
- struct hostent *hp;
- /* FILL SOCKET ADDRESS*/
- if((hp = gethostbyname(RECEIVER_HOST))==NULL)
- report_error("gethostbyname");
- bcopy((char*)hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
- sa.sin_family = hp->h_addrtype;
- sa.sin_port = htons(6303+ 20000); /* define port
- number based on student ID*/
- /* Creating the socket and returns error if unsuccessfull */
- if((s=socket(AF_INET, SOCK_DGRAM, PF_UNSPEC))== -1)
- report_error("socket");
- printf("Socket= %d\n",s);
- /* Sending the message to server and returns error if unsuccesfull */
- if(sendto(s, msg, BUFSIZE, 0, (struct sockaddr *) &sa, length)== -1)
- report_error("sendto");
- /* Receives message from server and returns error if unsuccesfull */
- recvfrom(s, received, BUFSIZE, 0, (struct sockaddr *) &sa, &length);
- printf("%s\n",received);
- close(s);
- }
- // this function returns as 1 if there is a response server, and 0 if there isn't after 3 seconds.
- int AnythingThere(int s)
- {
- fd_set read_mask;
- struct timeval timeout;
- int ret;
- timeout.tv_sec = TIMEOUT_SECONDS;
- timeout.tv_usec = 0;
- FD_ZERO (&read_mask);
- FD_SET(s, &read_mask);
- if((ret=select(32,&read_mask,0,0,&timeout))<0)
- report_error("select");
- return((ret==0) ? NOTHING_THERE : SOMETHING_THERE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement