-
-
Save beefy/1d1c372b7bdd65c9e08fd074538d0975 to your computer and use it in GitHub Desktop.
simple network in C from CS283
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 "csapp.h" | |
/* usage: ./echoclient host port */ | |
int main(int argc, char **argv) | |
{ | |
int clientfd, port; | |
char *host, buf[MAXLINE]; | |
rio_t rio; | |
host = argv[1]; | |
port = atoi(argv[2]); | |
clientfd = Open_clientfd(host, port); | |
Rio_readinitb(&rio, clientfd); | |
printf("Enter message:"); fflush(stdout); | |
while (Fgets(buf, MAXLINE, stdin) != | |
NULL) { | |
Rio_writen(clientfd, buf, strlen(buf)); | |
Rio_readlineb(&rio, buf, MAXLINE); | |
printf("Echo:"); | |
Fputs(buf, stdout); | |
printf("Enter message:"); | |
fflush(stdout); | |
} | |
Close(clientfd); | |
exit(0); | |
} |
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