aboutsummaryrefslogtreecommitdiff
path: root/snowcast_server.c
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-09-15 01:56:35 -0400
committersotech117 <michael_foiani@brown.edu>2023-09-15 01:56:35 -0400
commit9e0994d5a997a5b8c298992f05c94ba1b22a692c (patch)
treec560424d75a2c3d0171c320df14f32f5dabc1131 /snowcast_server.c
parent35f464738c990752ffdb65ef3adcb2812925c07d (diff)
decents stopping point. developed some udp code
Diffstat (limited to 'snowcast_server.c')
-rw-r--r--snowcast_server.c47
1 files changed, 44 insertions, 3 deletions
diff --git a/snowcast_server.c b/snowcast_server.c
index b1d7d8a..6043157 100644
--- a/snowcast_server.c
+++ b/snowcast_server.c
@@ -60,13 +60,13 @@ int main(int argc, char *argv[])
FD_ZERO(&master); // clear the master and temp sets
FD_ZERO(&read_fds);
- // get us a socket and bind it
+ // 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, "selectserver: %s\n", gai_strerror(rv));
+ fprintf(stderr, "snowcast_server: %s\n", gai_strerror(rv));
exit(1);
}
@@ -133,7 +133,7 @@ int main(int argc, char *argv[])
fdmax = newfd;
}
printf("selectserver: new connection from %s on "
- "socket %d\n",
+ "socket %d\n.",
inet_ntop(remoteaddr.ss_family,
get_in_addr((struct sockaddr*)&remoteaddr),
remoteIP, INET6_ADDRSTRLEN),
@@ -164,6 +164,44 @@ int main(int argc, char *argv[])
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
@@ -187,6 +225,9 @@ int main(int argc, char *argv[])
} // 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;