diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-09-20 04:04:22 +0000 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-09-20 04:04:22 +0000 |
commit | 1263cbdbb6cf3ebbb157286b2bb2e488e4b931c8 (patch) | |
tree | 0538cd930e5afd840f0edef42f448d1933b03b59 | |
parent | 5eb24f6063515e86e3e87e6fb91e6672dc14de34 (diff) |
fix handshake protocol
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | broadcast.c | 67 | ||||
-rw-r--r-- | client.c | 27 | ||||
-rw-r--r-- | htable.c | 78 | ||||
-rw-r--r-- | htable.h | 45 | ||||
-rw-r--r-- | list.h | 137 | ||||
-rw-r--r-- | server.c | 153 | ||||
-rwxr-xr-x | snowcast_control | bin | 35309 -> 21440 bytes | |||
-rw-r--r-- | snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control | bin | 13886 -> 13854 bytes | |||
-rwxr-xr-x | snowcast_listener | bin | 34654 -> 19128 bytes | |||
-rwxr-xr-x | snowcast_server | bin | 55212 -> 37128 bytes | |||
-rw-r--r-- | snowcast_server.c | 234 | ||||
-rw-r--r-- | snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server | bin | 23632 -> 23723 bytes | |||
-rw-r--r-- | snowcast_server_concurrent.c | 57 | ||||
-rw-r--r-- | talker.c | 67 | ||||
-rwxr-xr-x | test | bin | 52753 -> 0 bytes |
16 files changed, 49 insertions, 820 deletions
@@ -4,10 +4,10 @@ CFLAGS = -g -I. -std=gnu99 -Wall -pthread all: server client -server: server.c +server: $(CC) $(CFLAGS) -o snowcast_server snowcast_server_concurrent.c -client: client.c +client: $(CC) $(CFLAGS) -o snowcast_control client.c $(CC) $(CFLAGS) -o snowcast_listener listener.c diff --git a/broadcast.c b/broadcast.c deleted file mode 100644 index b10e633..0000000 --- a/broadcast.c +++ /dev/null @@ -1,67 +0,0 @@ -/* -** broadcaster.c -- a datagram "client" like talker.c, except -** this one can broadcast -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <errno.h> -#include <string.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <netdb.h> - -#define SERVERPORT 4950 // the port users will be connecting to - -int main(int argc, char *argv[]) -{ - int sockfd; - struct sockaddr_in their_addr; // connector's address information - struct hostent *he; - int numbytes; - int broadcast = 1; - //char broadcast = '1'; // if that doesn't work, try this - - if (argc != 3) { - fprintf(stderr,"usage: broadcaster hostname message\n"); - exit(1); - } - - if ((he=gethostbyname(argv[1])) == NULL) { // get the host info - perror("gethostbyname"); - exit(1); - } - - if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - perror("socket"); - exit(1); - } - - // this call is what allows broadcast packets to be sent: - if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast, - sizeof broadcast) == -1) { - perror("setsockopt (SO_BROADCAST)"); - exit(1); - } - - their_addr.sin_family = AF_INET; // host byte order - their_addr.sin_port = htons(SERVERPORT); // short, network byte order - their_addr.sin_addr = *((struct in_addr *)he->h_addr); - memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero); - - if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0, - (struct sockaddr *)&their_addr, sizeof their_addr)) == -1) { - perror("sendto"); - exit(1); - } - - printf("sent %d bytes to %s\n", numbytes, - inet_ntoa(their_addr.sin_addr)); - - close(sockfd); - - return 0; -}
\ No newline at end of file @@ -88,15 +88,11 @@ int main(int argc, char *argv[]) freeaddrinfo(servinfo); // all done with this structure - struct Welcome msg; - // recv the message, check for errors too - if ((recvbytes = recv(sockfd, (char*)&msg, sizeof(struct Welcome), 0)) == -1) { - perror("recv"); - exit(1); - } - msg.numStations = ntohs(msg.numStations); - printf("Welcome to Snowcast! The server has %d stations.\n", msg.numStations); - + pthread_t reply_thread; + pthread_create(&reply_thread, NULL, reply_thread_routine, (void*)sockfd); + + usleep(1000); + struct Hello hello; hello.commandType = 0; // convert updPort to an int @@ -106,11 +102,6 @@ int main(int argc, char *argv[]) perror("send"); exit(1); } - // print the amount of bytes sent - printf("client: sent %d bytes on a hello call \n", numbytessent); - - pthread_t reply_thread; - pthread_create(&reply_thread, NULL, reply_thread_routine, (void*)sockfd); char input[LINE_MAX]; printf("Enter a number to change to it's station. Click q to end stream.\n"); @@ -166,6 +157,14 @@ void *reply_thread_routine(void* args) { // print out the fields of reply on one line printf("\nclient: replyType: %d, stringSize: %d\n", reply.replyType, reply.stringSize); + if (reply.replyType == 2) { + struct Welcome msg; + // recv the message, check for errors too + memcpy(&msg, buf, sizeof(struct Welcome)); + msg.numStations = ntohs(msg.numStations); + printf("Welcome to Snowcast! The server has %d stations.\n", msg.numStations); + } + // print the size of reply if (reply.replyType == 3) { printf("client: received an announce message\n"); diff --git a/htable.c b/htable.c deleted file mode 100644 index a39c3c6..0000000 --- a/htable.c +++ /dev/null @@ -1,78 +0,0 @@ -#include <stdio.h> -#include <pthread.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "htable.h" - -static htable_node_t *__htable_lookup( htable_t *ht, unsigned int id ); - -void htable_init( htable_t *ht, unsigned int cap ) { - unsigned int i; - - ht->ht_hash = (list_t*) malloc( sizeof( list_t ) * cap ); - ht->ht_cap = cap; - ht->ht_size = 0; - - for( i = 0; i < cap; i++ ) - list_init( &ht->ht_hash[ i ] ); -} - -void htable_destroy( htable_t *ht ) { - unsigned int i; - htable_node_t *hn; - - for( i = 0; i < ht->ht_cap; i++ ) { - list_iterate_begin( &ht->ht_hash[ i ], hn, htable_node_t, hn_link ) { - free( hn ); - } list_iterate_end(); - } - - free( ht->ht_hash ); -} - -void *htable_get( htable_t *ht, unsigned int id ) { - htable_node_t *hn; - - if( ( hn = __htable_lookup( ht, id ) ) ) return hn->hn_data; - else return NULL; -} - -void *htable_put( htable_t *ht, unsigned int id, void *data ) { - htable_node_t *hn; - void *old = NULL; - - if( !( hn = __htable_lookup( ht, id ) ) ) { - hn = (htable_node_t*) malloc( sizeof( htable_node_t ) ); - hn->hn_id = id; - list_insert_head( &ht->ht_hash[ id % ht->ht_cap ], &hn->hn_link ); - ht->ht_size++; - } else old = hn->hn_data; - - hn->hn_data = data; - - return old; -} - -void *htable_remove( htable_t *ht, unsigned int id ) { - htable_node_t *hn; - - if( ( hn = __htable_lookup( ht, id ) ) ) { - void *data = hn->hn_data; - list_remove( &hn->hn_link ); - free( hn ); - ht->ht_size--; - return data; - } else return NULL; -} - -htable_node_t *__htable_lookup( htable_t *ht, unsigned int id ) { - htable_node_t *hn; - - list_iterate_begin( &ht->ht_hash[ id % ht->ht_cap ], hn, htable_node_t, hn_link ) { - if( hn->hn_id == id ) return hn; - } list_iterate_end(); - - return NULL; -}
\ No newline at end of file diff --git a/htable.h b/htable.h deleted file mode 100644 index 65eac58..0000000 --- a/htable.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef __HASHTABLE_H__ -#define __HASHTABLE_H__ - -#include <pthread.h> - -#include "list.h" - -/* FIXME make this a doubly-hashed, dynamically groweable hashtable */ - -typedef struct htable { - list_t *ht_hash; /* table entries */ - unsigned int ht_size; /* table size */ - unsigned int ht_cap; /* table capacity */ -} htable_t; - -typedef struct htable_node { - list_t hn_link; /* link */ - unsigned int hn_id; /* hash id */ - void *hn_data; /* data */ -} htable_node_t; - -void htable_init( htable_t *ht, unsigned int cap ); -void htable_destroy( htable_t *ht ); -void *htable_get( htable_t *ht, unsigned int id ); -void *htable_put( htable_t *ht, unsigned int id, void *data ); -void *htable_remove( htable_t *ht, unsigned int id ); - -#define htable_iterate_begin( ht, key, var, type ) \ -do { \ - unsigned int ___i; \ - htable_t *__ht = (ht); \ - htable_node_t *__hnode; \ - for(___i = 0;___i < __ht->ht_cap;___i++ ) { \ - list_iterate_begin( &__ht->ht_hash[___i ], __hnode, htable_node_t, hn_link ) { \ - (var) = (type*) __hnode->hn_data; \ - (key) = __hnode->hn_id; \ - do - -#define htable_iterate_end() \ - while( 0 ); \ - } list_iterate_end(); \ - } \ -} while( 0 ) - -#endif /* __HASHTABLE_H__ */
\ No newline at end of file @@ -1,137 +0,0 @@ -#ifndef __LIST_H__ -#define __LIST_H__ - -#include <stddef.h> - -/* -** Generic circular doubly linked list implementation. -** -** list_t is the head of the list. -** list_link_t should be included in structures which want to be -** linked on a list_t. -** -** All of the list functions take pointers to list_t and list_link_t -** types, unless otherwise specified. -** -** list_init(list) initializes a list_t to an empty list. -** -** list_empty(list) returns 1 iff the list is empty. -** -** Insertion functions. -** list_insert_head(list, link) inserts at the front of the list. -** list_insert_tail(list, link) inserts at the end of the list. -** list_insert_before(olink, nlink) inserts nlink before olink in list. -** -** Removal functions. -** Head is list->l_next. Tail is list->l_prev. -** The following functions should only be called on non-empty lists. -** list_remove(link) removes a specific element from the list. -** list_remove_head(list) removes the first element. -** list_remove_tail(list) removes the last element. -** -** Item accessors. -** list_item(link, type, member) given a list_link_t* and the name -** of the type of structure which contains the list_link_t and -** the name of the member corresponding to the list_link_t, -** returns a pointer (of type "type*") to the item. -** -** To iterate over a list, -** -** list_link_t *link; -** for (link = list->l_next; -** link != list; link = link->l_next) -** ... -** -** Or, use the macros, which will work even if you list_remove() the -** current link: -** -** type iterator; -** list_iterate_begin(list, iterator, type, member) { -** ... use iterator ... -** } list_iterate_end; -*/ - -typedef struct llist { - struct llist *l_next; - struct llist *l_prev; -} list_t, list_link_t; - -#define list_init(list) \ - (list)->l_next = (list)->l_prev = (list); - -#define list_link_init(link) \ - (link)->l_next = (link)->l_prev = NULL; - -#define list_empty(list) \ - ((list)->l_next == (list)) - -#define list_insert_before(old, new) \ - do { \ - list_link_t *prev = (new); \ - list_link_t *next = (old); \ - prev->l_next = next; \ - prev->l_prev = next->l_prev; \ - next->l_prev->l_next = prev; \ - next->l_prev = prev; \ - } while(0) - -#define list_insert_head(list, link) \ - list_insert_before((list)->l_next, link) - -#define list_insert_tail(list, link) \ - list_insert_before(list, link) - -#define list_remove(link) \ - do { \ - list_link_t *ll = (link); \ - list_link_t *prev = ll->l_prev; \ - list_link_t *next = ll->l_next; \ - prev->l_next = next; \ - next->l_prev = prev; \ - ll->l_next = ll->l_prev = 0; \ - } while(0) - -#define list_remove_head(list) \ - list_remove((list)->l_next) - -#define list_remove_tail(list) \ - list_remove((list)->l_prev) - -#define list_item(link, type, member) \ - (type*)((char*)(link) - offsetof(type, member)) - -#define list_head(list, type, member) \ - list_item((list)->l_next, type, member) - -#define list_tail(list, type, member) \ - list_item((list)->l_prev, type, member) - -#define list_iterate_begin(list, var, type, member) \ - do { \ - list_link_t *__link; \ - list_link_t *__next; \ - for (__link = (list)->l_next; \ - __link != (list); \ - __link = __next) { \ - var = list_item(__link, type, member); \ - __next = __link->l_next; - -#define list_iterate_end() \ - } \ - } while(0) - -#define list_iterate_reverse_begin(list, var, type, member) \ - do { \ - list_link_t *__link; \ - list_link_t *__prev; \ - for (__link = (list)->l_prev; \ - __link != (list); \ - __link = __prev) { \ - var = list_item(__link, type, member); \ - __prev = __link->l_prev; - -#define list_iterate_reverse_end() \ - } \ - } while(0) - -#endif /* __LIST_H__ */
\ No newline at end of file diff --git a/server.c b/server.c deleted file mode 100644 index be9f603..0000000 --- a/server.c +++ /dev/null @@ -1,153 +0,0 @@ -/* -** server.c -- a stream socket server demo -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <stdint.h> -#include <unistd.h> -#include <errno.h> -#include <string.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <netdb.h> -#include <arpa/inet.h> -#include <sys/wait.h> -#include <signal.h> - -#include "protocol.h" - -#define BACKLOG 10 // how many pending connections queue will hold - -#define MAXDATASIZE 100 // max number of bytes we can get at once - - -void sigchld_handler(int s) -{ - // waitpid() might overwrite errno, so we save and restore it: - int saved_errno = errno; - - while(waitpid(-1, NULL, WNOHANG) > 0); - - errno = saved_errno; -} - - -// get sockaddr, IPv4 or IPv6: -void *get_in_addr(struct sockaddr *sa) -{ - if (sa->sa_family == AF_INET) { - return &(((struct sockaddr_in*)sa)->sin_addr); - } - - return &(((struct sockaddr_in6*)sa)->sin6_addr); -} - -int main(int argc, char *argv[]) -{ - int sockfd, new_fd, numbytes, b; // listen on sock_fd, new connection on new_fd - char buf[MAXDATASIZE]; - struct addrinfo hints, *servinfo, *p; - struct sockaddr_storage their_addr; // connector's address information - socklen_t sin_size; - struct sigaction sa; - int yes=1; - char s[INET6_ADDRSTRLEN]; - int rv; - - if (argc < 3) { - fprintf(stderr,"usage: <listen port> <file0> [file 1] [file 2] ... \n"); - exit(1); - } - - memset(&hints, 0, sizeof hints); - hints.ai_family = AF_INET; // only IPv4 - hints.ai_socktype = SOCK_STREAM; // TCP connection - hints.ai_flags = AI_PASSIVE; // use my IP - - const char* port = argv[1]; // the port users will be connecting to - - if ((rv = getaddrinfo(NULL, port, &hints, &servinfo)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); - return 1; - } - - // loop through all the results and bind to the first we can - for(p = servinfo; p != NULL; p = p->ai_next) { - if ((sockfd = socket(p->ai_family, p->ai_socktype, - p->ai_protocol)) == -1) { - perror("server: socket"); - continue; - } - - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, - sizeof(int)) == -1) { - perror("setsockopt"); - exit(1); - } - - if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { - close(sockfd); - perror("server: bind"); - continue; - } - - break; - } - - freeaddrinfo(servinfo); // all done with this structure - - if (p == NULL) { - fprintf(stderr, "server: failed to bind\n"); - exit(1); - } - - if (listen(sockfd, BACKLOG) == -1) { - perror("listen"); - exit(1); - } - - sa.sa_handler = sigchld_handler; // reap all dead processes - sigemptyset(&sa.sa_mask); - sa.sa_flags = SA_RESTART; - if (sigaction(SIGCHLD, &sa, NULL) == -1) { - perror("sigaction"); - exit(1); - } - - printf("server: waiting for connections...\n"); - - - - while(1) { // main accept() loop - sin_size = sizeof their_addr; - new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); - if (new_fd == -1) { - perror("accept"); - continue; - } - - inet_ntop(their_addr.ss_family, - get_in_addr((struct sockaddr *)&their_addr), - s, sizeof s); - printf("server: got connection from %s\n", s); - - if (!fork()) { // this is the child process - - close(sockfd); // child doesn't need the listener - - // make a struct for the message, number is the number of stations - struct Welcome welcome; - welcome.replyType = 2; - welcome.numStations = htons(argc - 2); - if ((send(new_fd, &welcome, sizeof(struct Welcome), 0)) == -1) - perror("send"); - close(new_fd); - exit(0); - } - // close(new_fd); // parent doesn't need this - } - - return 0; -}
\ No newline at end of file diff --git a/snowcast_control b/snowcast_control Binary files differindex 56af544..711e770 100755 --- a/snowcast_control +++ b/snowcast_control diff --git a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control Binary files differindex 7406e42..4cd75ec 100644 --- a/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control +++ b/snowcast_control.dSYM/Contents/Resources/DWARF/snowcast_control diff --git a/snowcast_listener b/snowcast_listener Binary files differindex de66f23..9f75571 100755 --- a/snowcast_listener +++ b/snowcast_listener diff --git a/snowcast_server b/snowcast_server Binary files differindex 78be793..8ef32a4 100755 --- a/snowcast_server +++ b/snowcast_server diff --git a/snowcast_server.c b/snowcast_server.c deleted file mode 100644 index 6043157..0000000 --- a/snowcast_server.c +++ /dev/null @@ -1,234 +0,0 @@ -/* -** server.c -- a stream socket server demo -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <stdint.h> -#include <unistd.h> -#include <errno.h> -#include <string.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <netdb.h> -#include <arpa/inet.h> -#include <sys/wait.h> -#include <signal.h> - -#include "protocol.h" - -// get sockaddr, IPv4 or IPv6: -void *get_in_addr(struct sockaddr *sa) -{ - if (sa->sa_family == AF_INET) { - return &(((struct sockaddr_in*)sa)->sin_addr); - } - - return &(((struct sockaddr_in6*)sa)->sin6_addr); -} - -int main(int argc, char *argv[]) -{ - fd_set master; // master file descriptor list - fd_set read_fds; // temp file descriptor list for select() - int fdmax; // maximum file descriptor number - - int listener; // listening socket descriptor - int newfd; // newly accept()ed socket descriptor - struct sockaddr_storage remoteaddr; // client address - socklen_t addrlen; - - char buf[256]; // buffer for client data - int nbytes; - - char remoteIP[INET6_ADDRSTRLEN]; - - int yes=1; // for setsockopt() SO_REUSEADDR, below - int i, j, rv; - - struct addrinfo hints, *ai, *p; - - // check and assign arguments - if (argc < 3) { - fprintf(stderr,"usage: <listen port> <file0> [file 1] [file 2] ... \n"); - exit(1); - } - - const char* port = argv[1]; - - FD_ZERO(&master); // clear the master and temp sets - FD_ZERO(&read_fds); - - // LISTENER: get us a socket and bind it - memset(&hints, 0, sizeof hints); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_PASSIVE; - if ((rv = getaddrinfo(NULL, port, &hints, &ai)) != 0) { - fprintf(stderr, "snowcast_server: %s\n", gai_strerror(rv)); - exit(1); - } - - for(p = ai; p != NULL; p = p->ai_next) { - listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol); - if (listener < 0) { - continue; - } - - // lose the pesky "address already in use" error message - setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); - - if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) { - close(listener); - continue; - } - - break; - } - - // if we got here, it means we didn't get bound - if (p == NULL) { - fprintf(stderr, "snowcast_server: failed to bind\n"); - exit(2); - } - - freeaddrinfo(ai); // all done with this - - // listen - if (listen(listener, 10) == -1) { - perror("listen"); - exit(3); - } - - // add the listener to the master set - FD_SET(listener, &master); - - // keep track of the biggest file descriptor - fdmax = listener; // so far, it's this one - - // main loop - while(1==1) { - read_fds = master; // copy it - if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) { - perror("select"); - exit(4); - } - - // run through the existing connections looking for data to read - for(i = 0; i <= fdmax; i++) { - if (FD_ISSET(i, &read_fds)) { // we got one!! - if (i == listener) { - // handle new connections - addrlen = sizeof remoteaddr; - newfd = accept(listener, - (struct sockaddr *)&remoteaddr, - &addrlen); - - if (newfd == -1) { - perror("accept"); - } else { - FD_SET(newfd, &master); // add to master set - if (newfd > fdmax) { // keep track of the max - fdmax = newfd; - } - printf("selectserver: new connection from %s on " - "socket %d\n.", - inet_ntop(remoteaddr.ss_family, - get_in_addr((struct sockaddr*)&remoteaddr), - remoteIP, INET6_ADDRSTRLEN), - newfd); - - // send the welcome message to client - struct Welcome welcome; - welcome.replyType = 2; - welcome.numStations = htons(argc - 2); - if ((send(newfd, &welcome, sizeof(struct Welcome), 0)) == -1) - perror("send"); - } - } else { - // handle data from a client - struct Command command; - if ((nbytes = recv(i, (char*)&command, sizeof(struct Command), 0)) <= 0) { - // got error or connection closed by client - if (nbytes == 0) { - // connection closed - printf("selectserver: socket %d hung up\n", i); - } else { - perror("recv"); - } - close(i); // bye! - FD_CLR(i, &master); // remove from master set - } else { - // we got some data from a client - if (command.commandType == 0) { - // hello message with udpPort - printf("udpPort (from Hello) for new connection is %d.\n", ntohs(command.number)); - - // TALKER: get us a udp socket and bind it - struct addrinfo hintsUdp, *servinfoUdp, *pUdp; - int rvUdp, sockfdUdp, numbytesUdp; - memset(&hintsUdp, 0, sizeof hintsUdp); - hintsUdp.ai_family = AF_INET; // IPv4 - hintsUdp.ai_socktype = SOCK_DGRAM; // UDP - if ((rvUdp = getaddrinfo(argv[1], command.number, &hints, &servinfoUdp)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rvUdp)); - return 1; - } - - // loop through all the results and make a socket - for(p = servinfoUdp; p != NULL; p = p->ai_next) { - if ((sockfdUdp = socket(p->ai_family, p->ai_socktype, - p->ai_protocol)) == -1) { - perror("talker: socket"); - continue; - } - break; - } - - if (p == NULL) { - fprintf(stderr, "talker: failed to create socket\n"); - return 2; - } - - - if ((numbytesUdp = sendto(sockfdUdp, "test", strlen("test"), 0, - p->ai_addr, p->ai_addrlen)) == -1) { - perror("talker: sendto"); - exit(1); - } - - freeaddrinfo(servinfoUdp); - - printf("talker: sent %d bytes to %d\n", numbytesUdp, sockfdUdp); - // close(sockfdUdp); - } - if (command.commandType == 1) { - // setStation command for the user - printf("TODO: set station to %d\n", ntohs(command.number)); - } - else { - // send back in invalid command - struct InvalidCommand invalid; - invalid.replyType = 4; - invalid.replyStringSize = 21; - // make a string with the command.commmandType type in it - invalid.replyString = "Invalid command type"; - if ((send(i, &invalid, sizeof(struct InvalidCommand), 0)) == -1) - perror("send"); - - // drop connection upon invalid command - close(i); - FD_CLR(i, &master); - } - } - } // END handle data from client - } // END got new incoming connection - } // END looping through file descriptors - - // broadcast the new files over the udp socket list for each use - - } // END for(;;)--and you thought it would never end! - - return 0; -}
\ No newline at end of file diff --git a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server Binary files differindex d27ad67..a3f5493 100644 --- a/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server +++ b/snowcast_server.dSYM/Contents/Resources/DWARF/snowcast_server diff --git a/snowcast_server_concurrent.c b/snowcast_server_concurrent.c index d0e6aa5..d855bab 100644 --- a/snowcast_server_concurrent.c +++ b/snowcast_server_concurrent.c @@ -114,7 +114,7 @@ int main(int argc, char *argv[]) // start syncchronization thread to broadcast stations pthread_t sync_thread; -pthread_create(&sync_thread, NULL, synchronization_thread, NULL); + pthread_create(&sync_thread, NULL, synchronization_thread, NULL); // command line interface char input[LINE_MAX]; @@ -307,6 +307,7 @@ void *synchronization_thread(void *arg) { FILE* fp = fopen(station_data[i].filePath, "r"); fseek(fp, 0L, SEEK_END); size_t size = ftell(fp); + fclose(fp); if (size == -1) { perror("ftell"); return (NULL); @@ -315,10 +316,25 @@ void *synchronization_thread(void *arg) { // if the seek index is greater than the size of the file, reset it if (station_data[i].seekIndex >= size) { - // printf("resetting seek index for station %d\n", i); station_data[i].seekIndex = 0; + + + // send the announce messages + for (int i = 0; i < max_active_users; i++) + { + // if (user_data[i].streamThread == NULL) { + // break; + // } + if (user_data[i].sockfd == -1) + continue; + // print_user_data(i); + // update the station of each user + if (user_data[i].stationNum == i) + { + send_announce_message(user_data[i].sockfd, i); + } + } } - fclose(fp); } @@ -424,27 +440,14 @@ void *select_thread(void *arg) { if (newfd > fdmax) { // keep track of the max fdmax = newfd; } - printf("selectserver: new connection from %s on " - "socket %d\n.", - inet_ntop(remoteaddr.ss_family, - get_in_addr((struct sockaddr*)&remoteaddr), - remoteIP, INET6_ADDRSTRLEN), - newfd); + // printf("selectserver: new connection from %s on " + // "socket %d\n.", + // inet_ntop(remoteaddr.ss_family, + // get_in_addr((struct sockaddr*)&remoteaddr), + // remoteIP, INET6_ADDRSTRLEN), + // newfd); // init user with this newfd init_user(newfd); - - // send the welcome message to client - struct Welcome welcome; - welcome.replyType = 2; - welcome.numStations = htons(num_stations); - int numbytes; - if ((numbytes=send(newfd, &welcome, sizeof(struct Welcome), 0)) == -1) - perror("send"); - - //print the num bytes - // print the size of the struct welcome - printf("size of welcome struct: %lu\n", sizeof(struct Welcome)); - printf("sent %d bytes\n", numbytes); } } else { // handle data from a client @@ -468,6 +471,14 @@ void *select_thread(void *arg) { printf("udpPort (from Hello) for new connection is %d.\n", ntohs(command.number)); // update udp port of user update_user_udpPort(i, ntohs(command.number)); + + // send the welcome message to client + struct Welcome welcome; + welcome.replyType = 2; + welcome.numStations = htons(num_stations); + int numbytes; + if ((numbytes=send(i, &welcome, sizeof(struct Welcome), 0)) == -1) + perror("send"); } else if (command.commandType == 1) { int station_num = ntohs(command.number); @@ -490,6 +501,7 @@ void *select_thread(void *arg) { // drop connection upon invalid command close(i); FD_CLR(i, &master); + destroy_user(i); } } } // END handle data from client @@ -594,5 +606,4 @@ void send_announce_message(int fd, int station_num) { printf("sent %d bytes\n", bytessent); free(send_buffer); - }
\ No newline at end of file diff --git a/talker.c b/talker.c deleted file mode 100644 index bb801e5..0000000 --- a/talker.c +++ /dev/null @@ -1,67 +0,0 @@ -/* -** talker.c -- a datagram "client" demo -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <errno.h> -#include <string.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <netdb.h> - -#define SERVERPORT "4950" // the port users will be connecting to - -int main(int argc, char *argv[]) -{ - int sockfd; - struct addrinfo hints, *servinfo, *p; - int rv; - int numbytes; - - if (argc != 3) { - fprintf(stderr,"usage: talker hostname message\n"); - exit(1); - } - - memset(&hints, 0, sizeof hints); - hints.ai_family = AF_INET6; // set to AF_INET to use IPv4 - hints.ai_socktype = SOCK_DGRAM; - - if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); - return 1; - } - - // loop through all the results and make a socket - for(p = servinfo; p != NULL; p = p->ai_next) { - if ((sockfd = socket(p->ai_family, p->ai_socktype, - p->ai_protocol)) == -1) { - perror("talker: socket"); - continue; - } - - break; - } - - if (p == NULL) { - fprintf(stderr, "talker: failed to create socket\n"); - return 2; - } - - if ((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, - p->ai_addr, p->ai_addrlen)) == -1) { - perror("talker: sendto"); - exit(1); - } - - freeaddrinfo(servinfo); - - printf("talker: sent %d bytes to %s\n", numbytes, argv[1]); - close(sockfd); - - return 0; -}
\ No newline at end of file Binary files differ |