diff options
Diffstat (limited to 'server.c')
-rw-r--r-- | server.c | 37 |
1 files changed, 23 insertions, 14 deletions
@@ -4,6 +4,7 @@ #include <stdio.h> #include <stdlib.h> +#include <stdint.h> #include <unistd.h> #include <errno.h> #include <string.h> @@ -15,7 +16,7 @@ #include <sys/wait.h> #include <signal.h> -#define PORT "3490" // the port users will be connecting to +#include "protocol.h" #define BACKLOG 10 // how many pending connections queue will hold @@ -43,9 +44,9 @@ void *get_in_addr(struct sockaddr *sa) return &(((struct sockaddr_in6*)sa)->sin6_addr); } -int main(void) +int main(int argc, char *argv[]) { - int sockfd, new_fd, numbytes; // listen on sock_fd, new connection on new_fd + 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 @@ -55,12 +56,19 @@ int main(void) 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; + hints.ai_socktype = SOCK_STREAM; // TCP connection hints.ai_flags = AI_PASSIVE; // use my IP - if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) { + 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; } @@ -126,18 +134,19 @@ int main(void) if (!fork()) { // this is the child process close(sockfd); // child doesn't need the listener - if (send(new_fd, "Hello, world!", 13, 0) == -1) + + // make a struct for the message, number is thennubmer of stations + struct Welcome welcome; + welcome.replyType = 2; + welcome.numStations = argc - 2; + if ((b = send(new_fd, &welcome, sizeof(struct Welcome), 0)) == -1) perror("send"); - // close(new_fd); - if ((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1) { - perror("recv"); - exit(1); - } - buf[numbytes] = '\0'; - printf("server: received '%s'\n",buf); + close(new_fd); + // print the num bytes sent + // printf("server: sent '%d'\n",b); exit(0); } - close(new_fd); // parent doesn't need this + // close(new_fd); // parent doesn't need this } return 0; |