/** * Destructor */ NutellaSearch::~NutellaSearch() { // we only use the sockets inside of this class if (this->vflag) std::cout << "NutellaSearch: Calling destructor" << std::endl; if (this->q_msock >= 0) msockdestroy(this->q_msock); if (this->r_msock >= 0) msockdestroy(this->r_msock); if (this->np != NULL) delete this->np; }
int main(int argc) { char message[MESSAGE_LEN]; char movie_title[MOVIE_TITLE_LEN]; int len, check_sock, found_sock, cnt; int sock, length, n; socklen_t fromlen; struct sockaddr_in server; struct sockaddr_in from; char buf[1024]; struct hostent *hp; printf("Nutella Server Started\n"); printf("Listening for movie requests\n"); char hostname[HOST_NAME_MAX]; if (gethostname(hostname, sizeof hostname) != 0) { printf("hostname error"); exit(1); } while (1) { /* set up socket */ if ((check_sock=msockcreate(RECV, CHECK_ADDR, CHECK_PORT)) < 0) { perror("msockcreate"); exit(1); } /* get multicasted message */ cnt = mrecv(check_sock, movie_title, MOVIE_TITLE_LEN); if (cnt < 0) { perror("mrecv"); exit(1); } else if (cnt==0) { return 0; } printf("Received search request for \"%s\"\n", movie_title); char *nut_file; int num_movies; char movies[100][25]; char movie_locs[100][25]; char frame[1440]; char temp_frame[960]; fstream inStream; nut_file = ".nutella"; inStream.open(nut_file, ios :: in); if(inStream.fail()) { //return false; cout << "couldn't open\n"; return 0; } //get number of movies from .nutella file inStream >> num_movies; int i; for (i = 0; i < num_movies; i++) { inStream >> movies[i]; inStream >> movie_locs[i]; } inStream.close(); int movie_loc; int movie_found = 0; for (i = 0; i < num_movies; i++) { if (strcmp(movies[i], lowercase(movie_title)) == 0) { //printf("Found the movie %s\n", movie_title); movie_found = 1; movie_loc = i; break; } } if (movie_found != 1) { printf("Couldn't find the movie %s\n", movie_title); return 0; } else { printf("Found the movie %s\n", movie_title); /* set up socket */ if ((found_sock=msockcreate(SEND, FOUND_ADDR, FOUND_PORT)) < 0) { perror("msockcreate"); exit(1); } printf("Sending port %d and address %s to client \n", STREAM_PORT, STREAM_ADDR); char found_message[100]; sprintf(found_message, "%d %s %s", STREAM_PORT, STREAM_ADDR, hostname); /* multicast message */ cnt = msend(found_sock, found_message, strlen(found_message)+1); if (cnt < 0) { perror("msend"); exit(1); } } sock=socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) printf("Opening socket error"); length = sizeof(server); bzero(&server,length); server.sin_family=AF_INET; //server.sin_addr.s_addr=STREAM_ADDR; if (inet_aton(STREAM_ADDR, &server.sin_addr) == 0) { perror("inet_aton"); exit(EXIT_FAILURE); } /* bcopy((char *)STREAM_ADDR, (char *)&server.sin_addr, strlen(STREAM_ADDR)); */ //hp = gethostbyaddr(stream_addr); hp = gethostbyname(hostname); if (hp==0) printf("Unknown host"); bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length); server.sin_port=htons(STREAM_PORT); if (bind(sock,(struct sockaddr *)&server,length)<0) printf("binding error"); fromlen = sizeof(struct sockaddr_in); n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen); if (n < 0) printf("recvfrom"); write(1,"Received a datagram: ",21); write(1,buf,n); char *filename = movie_locs[movie_loc]; FILE *file = fopen ( filename, "r" ); if ( file != NULL ) { char line [ 512 ]; /* or other suitable maximum line size */ strcpy(frame, ""); strcpy(temp_frame, ""); bzero(frame ,1440); struct timespec tim, tim2; tim.tv_sec = 0; tim.tv_nsec = 100000000; while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */ { //printf("%s", frame); if (strcmp(line, "end\n") == 0) { //bzero(buf ,256); //sprintf(buffer, "stream_request:%s", movie_title); printf("Sending: \n%s\n", frame); n = sendto(sock, frame, strlen(frame), 0,(struct sockaddr *)&from,fromlen); //sleep(.09); nanosleep(&tim , &tim2); if (n < 0) printf("sendto error"); memset(&frame[0], 0, sizeof(frame)); printf("Starting new cycle \n"); } else strcat(frame, line); //strcat(frame, "\n"); //fputs ( line, stdout ); /* write the line */ } fclose ( file ); n = sendto(sock, "End Movie", 9, 0,(struct sockaddr *)&from,fromlen); } else { printf ( "file didn't open" ); /* why didn't the file open? */ } /* while (1) { n = sendto(sock,"Got your message\n",17, 0,(struct sockaddr *)&from,fromlen); if (n < 0) printf("sendto error"); return 0; } */ msockdestroy(check_sock); msockdestroy(found_sock); close(sock); } }
void startClient(){ puts("Client Mode Selected..."); int length = 128; char* movieName = (char*) calloc(length, sizeof(char)); char* respBuff; short int port; int receivedLength = 0; int i = 0; struct timeval timeout; timeout.tv_sec = 3; timeout.tv_usec = 0; while(1) { respBuff = calloc(1024, sizeof(char)); memset(movieName,0, 128 * sizeof(char)); printf("Enter movie name: "); movieName = readInput(movieName,&length); int querySock = msockcreate(SEND, QUERYGROUP, QUERYPORT); int responseSock = msockcreate(RECV, RESPGROUP, RESPPORT); msend(querySock, movieName, strlen(movieName)); if (setsockopt (responseSock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) error("setsockopt failed\n"); receivedLength = mrecv(responseSock, respBuff, 1024); msockdestroy(querySock); msockdestroy(responseSock); if(receivedLength == -1) { puts("Movie not found on any listening servers..."); free(respBuff); continue; } i = 0; while( respBuff[i] != ':') i++; char* host = calloc(i+1, sizeof(char)); strncpy(host, respBuff, i); i++; sscanf(respBuff+i, "%hu", &port); puts("Server and socket received!"); struct hostent *hostname = gethostbyname(host); // Convert server name into 32bit ip breakdownURL(host); int sd; if(0 > (sd = socket(AF_INET, SOCK_STREAM, 0))) { // Create the socket to the server puts("Error creating socket."); printf("\n Error : Connect Failed \nReason: %s\n", strerror(errno)); } struct sockaddr_in serv_addr; memset(&serv_addr, '\0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(port); if(inet_pton(AF_INET, host, &serv_addr.sin_addr)<=0) { fprintf(stderr, "ERROR: %s\n", strerror(errno)); exit(1); } if( connect(sd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { fprintf(stderr, "ERROR: Connect Failed \nReason: %s\n", strerror(errno)); exit(1); } char* recvBuffer = calloc(1024, sizeof(char)); int c = 0; while(read(sd, recvBuffer, 1) > 0){ if(c == 0 && recvBuffer[0] == '\n'){ c++; } else if(c == 1 && recvBuffer[0] == 'd'){ c++; } else if(c == 2 && recvBuffer[0] == 'o'){ c++; } else if(c == 3 && recvBuffer[0] == 'n'){ c++; } else if(c == 4 && recvBuffer[0] == 'e'){ c++; } else if(c == 5 && recvBuffer[0] == '\n') { break; } printf("%s", recvBuffer); fflush(stdout); } puts(""); free(recvBuffer); free(respBuff); } }
/* function definition: listener() * listen on query port for movie requests * stream if found to user */ int listener(){ int query_socket_id, reply_socket_id, tcp_socket_id, stream_socket_id, bytesRecv, flag_for_replay, incorrect_response_counter; int number_of_movies_discovered = 0; char query_buffer[80]; char ip_of_stream[IP_LEN]; char response_buffer[120]; char list_of_movies[LIST_OF_MOVIES][80]; char handshake_buffer[80]; char handshake_reponse_buffer[120]; FILE *movie_fd; fd_set readFrom; struct timeval timeout, timeout_replay; timeout.tv_sec = 5; timeout.tv_usec = 0; timeout_replay.tv_sec = 10; timeout_replay.tv_usec = 0; if ((movie_fd = fopen(LOCATION_OF_MOVIE_LIST, "r")) == NULL){ perror("error: opening movie file"); exit(2); } else{ while (number_of_movies_discovered < LIST_OF_MOVIES && (fgets(list_of_movies[number_of_movies_discovered], 80 + 1, movie_fd)) != NULL){ list_of_movies[number_of_movies_discovered][strlen(list_of_movies[number_of_movies_discovered]) - 1] = '\0'; number_of_movies_discovered++; } if (fclose(movie_fd)){ perror("error: closing file"); exit(2); } } if (getIPAddress(ip_of_stream) < 0){ perror("error: getting IP address"); exit(2); } /* open query socket */ if ((query_socket_id = msockcreate(RECV, ADDRESS_FOR_QUERY, PORT_FOR_QUERY)) < 0){ perror("error: query_buffer socket creation"); exit(2); } /* open response socket */ if ((reply_socket_id = msockcreate(SEND, ADDRESS_FOR_REPLY, PORT_FOR_REPLY)) < 0){ perror("error: response socket creation"); exit(2); } /* open TCP socket */ if ((tcp_socket_id = openAndBindStream(atoi(PORT_TO_CONNECT_ON))) < 0){ perror("error: streaming socket creation"); exit(2); } /* set socket to nonblocking so it can time out*/ if (fcntl(tcp_socket_id, F_SETFL, O_NONBLOCK) < 0){ perror("error: set nonblocking"); exit(2); } while(1){ if (mrecv(query_socket_id, query_buffer, 80 + 1) < 0){ perror("error: receiving query"); exit(2); } /* search movie array to see if queried name exists */ if (searchForInputMovieName(query_buffer, list_of_movies, number_of_movies_discovered) == 0){ /* movie found, send ip and port information */ strncpy(response_buffer, query_buffer, strlen(query_buffer) + 1); strcat(response_buffer, ASCII_DELIMITER); strcat(response_buffer, ip_of_stream); strcat(response_buffer, ASCII_DELIMITER); strcat(response_buffer, PORT_TO_CONNECT_ON); sleep(1); //give client time to setup recieve port /* send response message */ if (msend(reply_socket_id, response_buffer, strlen(response_buffer) + 1) < 0){ perror("error: sending response_buffer"); exit(2); } /* add TCP socket to readset */ FD_ZERO(&readFrom); if (FD_SET(tcp_socket_id, &readFrom) < 0){ perror("error: could not add tcp_socket_id to readset"); exit(2); } //code from http://web.cs.wpi.edu/~cs4513/d14/samples/select.c was adapted //for the select function bytesRecv = select(tcp_socket_id + 1, &readFrom, NULL, NULL, &timeout); if (bytesRecv != 0){ if (FD_ISSET(tcp_socket_id, &readFrom)){ if ((stream_socket_id = listenAndConnect(tcp_socket_id)) < 0){ perror("error: listening for response"); exit(2); } else{ do{ flag_for_replay = 0; //start streaming movie if (playMovie(stream_socket_id, query_buffer) < 0){ perror("error: playing movie"); exit(2); } /* send half of a handshake */ if (sprintf(handshake_buffer, "%d", stream_socket_id) < 0){ perror("error: convert stream sock to handshake"); exit(2); } usleep(ACCEPT_WAIT); //wait for accept if (write(stream_socket_id, handshake_buffer, 80) == -1){ perror("error: failed to write"); exit(2); } incorrect_response_counter = 0; do{ FD_ZERO(&readFrom); if (FD_SET(stream_socket_id, &readFrom) < 0){ perror("error: adding tcp_socket_id to readset"); exit(2); } //code from http://web.cs.wpi.edu/~cs4513/d14/samples/select.c was adapted //for the select function bytesRecv = select(stream_socket_id + 1, &readFrom, NULL, NULL, &timeout_replay); if (bytesRecv > 0){ if (FD_ISSET(stream_socket_id, &readFrom)){ if (read(stream_socket_id, handshake_reponse_buffer, 120) < 0){ perror("error: receiving handshake response"); exit(2); } /* look for handshake_buffer for each reply */ if (strncmp(handshake_buffer, handshake_reponse_buffer, strlen(handshake_buffer) + 1) == 0){ flag_for_replay++; break; } else{ //incorrect response incorrect_response_counter++; } } } else if (bytesRecv == 0){ break; } } while ((bytesRecv != 0) || (incorrect_response_counter < 10)); } while (flag_for_replay > 0); if (close(stream_socket_id) < 0){ perror("error: closing stream socket"); exit(2); } } } } } } /* close the query socket */ if (msockdestroy(query_socket_id) < 0){ perror("error: query_buffer socket destruction"); exit(2); } /* close the response socket */ if (msockdestroy(reply_socket_id) < 0){ perror("error: response socket destruction"); exit(2); } /* close the TCP socket*/ if (close(tcp_socket_id) < 0){ perror("error: closing tcp socket"); exit(2); } return 0; }
int main(int argc) { char message[MESSAGE_LEN]; char movie_title[MOVIE_TITLE_LEN]; int len, check_sock, found_sock, cnt; int stream_port; char *stream_addr; int stream_sock, n; unsigned int length; struct sockaddr_in server, from; struct hostent *hp; char buffer[1440]; char *stream_hostname; printf("Nutella Client Started\n"); while(1) { printf("Enter Movie Name: "); scanf("%s", movie_title); printf("Sending search request for the movie %s\n", movie_title); /* set up socket */ if ((check_sock=msockcreate(SEND, CHECK_ADDR, CHECK_PORT)) < 0) { perror("msockcreate"); exit(1); } /* multicast message */ cnt = msend(check_sock, movie_title, strlen(movie_title)+1); if (cnt < 0) { perror("msend"); exit(1); } printf("Waiting for response\n"); /* set up socket */ if ((found_sock=msockcreate(RECV, FOUND_ADDR, FOUND_PORT)) < 0) { perror("msockcreate"); exit(1); } char found_message[FOUND_MSG_LEN]; /* get multicasted message */ cnt = mrecv(found_sock, found_message, FOUND_MSG_LEN); if (cnt < 0) { perror("mrecv"); exit(1); } else if (cnt==0) { return 0; } stream_port = atoi(strtok(found_message," ")); stream_addr = strtok (NULL, " "); stream_hostname = strtok (NULL, " "); printf("Received port(%d), IP(%s) and hostname(%s) from server\n", stream_port, stream_addr, stream_hostname); stream_sock = socket(AF_INET, SOCK_DGRAM, 0); if (stream_sock < 0) printf("socket error"); server.sin_family = AF_INET; /* bcopy((char *)stream_addr, (char *)&server.sin_addr, strlen(stream_addr)); */ //hp = gethostbyaddr(stream_addr); /* if (inet_aton(stream_addr, &server.sin_addr) == 0) { printf("inet_aton\n"); exit(EXIT_FAILURE); } /* hp = gethostbyaddr( (char *)&server.sin_addr.s_addr, sizeof(server.sin_addr.s_addr),AF_INET); if (hp==0) error("Unknown host"); */ hp = gethostbyname(stream_hostname); bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length); server.sin_port = htons(stream_port); length=sizeof(struct sockaddr_in); bzero(buffer,1440); sprintf(buffer, "stream_request:%s", movie_title); n=sendto(stream_sock,buffer, strlen(buffer),0,(const struct sockaddr *)&server,length); if (n < 0) printf("Sendto error"); while (1) { n = recvfrom(stream_sock,buffer,1440,0,(struct sockaddr *)&from, &length); if (n < 0) { printf("recvfrom error"); break; } if (strcmp(buffer, "End Movie") == 0) { printf("\033[2J"); printf("\033[0;0f"); break; } printf("\033[2J"); printf("\033[0;0f"); printf("%s", buffer); memset(&buffer[0], 0, sizeof(buffer)); //write(1,"Got an ack: ",12); //write(1,buffer,n); } msockdestroy(check_sock); msockdestroy(found_sock); close(stream_sock); } }
int player(int id) { char message[512]; char input[512]; int sock; struct sockaddr_in info; char address[INET_ADDRSTRLEN]; char *token; char *port; int i; char *name; int bytes; int sender_id; while(1){ fgets(input, sizeof input, stdin); if(strcmp(input, "exit\n") == 0){ msockdestroy(sock); exit(0); } if ((sock = msockcreate(SEND, MULTICAST_ADDR, MULTICAST_PORT)) < 0) { perror("msockcreate"); exit(1); } sprintf(message, "%d\t%s\t%d", REQUEST_KVK, input, id); if(msend(sock, message, strlen(message) + 1) < 0){ perror("msend"); exit(1); } i = 0; while(1){ printf("waiting for response from peers %d\n", i); if((bytes = mrecv(sock, &info, message, sizeof message)) < 0){ perror("mrecv"); } printf("received response:\n%s\n", message); inet_ntop(AF_INET, &(info.sin_addr), address, INET_ADDRSTRLEN); printf("\nsender address: %s\n", address); token = strtok(message, "\t"); if(atoi(token) == RESPONSE_KVK){ printf("response:\n"); token = strtok(NULL, "\t\n"); name = token; token = strtok(NULL, "\t"); port = token; token = strtok(NULL, "\t"); sender_id = atoi(token); if(atoi(token) == id){ break; } printf("NAME: %s\nPORT: %s\nINPUT: %s\nID: %d\n", name, port, input, sender_id); if(strcmp(name, strtok(input, "\n")) == 0){ watch_movie(address, port); break; } } if(i++ > 2){ break; } sleep(1); } } return 0; }