User Tools

Site Tools


cs444kim:ctvkime18

FORTUNE SERVER

/* 
 * fortuneserver.c - An iterative echo server 
 */ 
#include <ctype.h>
#include "csapp.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

char arr[500][150];          //max word length 50

/* Destructively modify string to be upper case */
void upper_case(char *s)
{
  while (*s) {
    *s = toupper(*s);
    s++;
  }
}

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) {
        srand(time(NULL));   // should only be called once
        int r = rand()%349;
    	printf("\nserver received %d bytes\n", (int) n);
        sprintf(buf, "%s\n", arr[r]);

    	Rio_writen(connfd, buf, strlen(buf));
        printf("\nserver sent %d bytes\n", (int) strlen(arr[r]));
    }
}

int main(int argc, char **argv) 
{
    FILE* inp;
    inp = fopen("fortunes.txt","r");        //filename of your data file
    
    int i = 0;
    while(1){
        char r = (char)fgetc(inp);
        int k = 0;
        while(r!=10 && !feof(inp)){    //read till , or EOF
            arr[i][k++] = r;            //store in array
            r = (char)fgetc(inp);
        }
        arr[i][k]=0;        //make last character of string null 
        if(feof(inp)){      //check again for EOF
            break;
        }
        i++;
    }
    fclose(inp);



    int listenfd, connfd, port;
    socklen_t clientlen;
    struct sockaddr_in clientaddr;
    struct hostent *hp;
    char *haddrp;
    unsigned short client_port;
    if (argc != 2) {
	fprintf(stderr, "usage: %s <port>\n", argv[0]);
	exit(0);
    }
    port = atoi(argv[1]);

    listenfd = Open_listenfd(port);
    while (1) {
	clientlen = sizeof(clientaddr);
	connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);

	/* determine the domain name and IP address of the client */
	hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, 
			   sizeof(clientaddr.sin_addr.s_addr), AF_INET);
	haddrp = inet_ntoa(clientaddr.sin_addr);
	client_port = ntohs(clientaddr.sin_port);
	printf("server connected to %s (%s), port %u\n",
	       hp->h_name, haddrp, client_port);
	echo(connfd);
	printf("Connection closed\n");
	Close(connfd);
    }
    exit(0);
}
/* $end fortuneserverimain */

FORTUNE CLIENT

/*
 * fortuneclient.c - An echo client
 */
/* $begin fortuneclientmain */
#include "csapp.h"
 #include <stdlib.h>
 #include <stdio.h>

int main(int argc, char **argv) 
{
    int clientfd, port;
    char *host, buf[MAXLINE];
    rio_t rio;
    char c = 'y';

    if (argc != 3) {
    	fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
    	exit(0);
    }
    host = argv[1];
    port = atoi(argv[2]);

    clientfd = Open_clientfd(host, port);
    Rio_readinitb(&rio, clientfd);

    printf("\nWould you like to know today's fortune? (y or n) : "); fflush(stdout);
    while (Fgets(buf, MAXLINE, stdin) != NULL && c=='y') {
    	Rio_writen(clientfd, buf, strlen(buf));
    	Rio_readlineb(&rio, buf, MAXLINE);
    	printf("Today's fortune: ");
    	Fputs(buf, stdout);
    	printf("\nWould you like to know today's fortune? (y or n) : "); c = getchar();	
    }
    Close(clientfd);
    exit(0);
}
/* $end fortuneclientmain */
cs444kim/ctvkime18.txt · Last modified: 2018/05/01 13:42 by kimj0