-
-
Save evanradcliffe/c8d5c44aeacefad9f3330ee4076727b3 to your computer and use it in GitHub Desktop.
server file from class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "csapp.c" | |
void echo(int connfd) | |
{ | |
size_t n; | |
char buf[MAXLINE]; | |
rio_t rio; | |
Rio_readinitb(&rio, connfd); | |
while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { | |
//upper_case(buf); | |
Rio_writen(connfd, buf, n); | |
printf("server received bytes\n"); | |
} | |
} | |
int main(int argc, char **argv) { | |
int listenfd, connfd, port, clientlen; | |
struct sockaddr_in clientaddr; | |
struct hostent *hp; | |
char *haddrp; | |
port = atoi(argv[1]); /* the server listens on a port passed | |
on the command line */ | |
listenfd = open_listenfd(port); | |
while (1) { | |
clientlen = sizeof(clientaddr); | |
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); | |
hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, | |
sizeof(clientaddr.sin_addr.s_addr), AF_INET); | |
haddrp = inet_ntoa(clientaddr.sin_addr); | |
printf("server connected to %s (%s)\n", hp->h_name, haddrp); | |
echo(connfd); | |
Close(connfd); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment