· 7 years ago · Apr 19, 2018, 02:00 AM
1/*------------------------------------------
2 Project: CS 270 Project 5: A Small Server
3 SubName: Small Server
4 Authors: Brad Stephenson & Colton Thompson
5 Date: 4/12/18
6 Version: 1.0
7 ------------------------------------------
8*/
9
10/* Requirements:
11 -Listen for incoming connections using IP address INADDR_ANY
12 and a port number specified as a command-line parameter
13 -Only respond to requests that include SecretKey
14 -Run server with something like "smalld 5678 987654"
15 where 5678 is the port and 987654 is the Secret Key
16 -Should accept incoming requests from clients
17 each request is a new connection to the server
18 -Server should only handle one request per connection
19 -Must verify the SecretKey on every transaction
20 */
21
22/* Procedure:
23 -Print out the following for every valid request
24 -Secret key = SecretKey
25 -Request type = type
26 -type is one of set, get, digest, or run
27 -Completion = status
28 -detail is a specific to the particular type
29 -If the secret key is wrong, print out the SecretKey line and
30 close connection
31 */
32
33/*
34 * echoserveri.c - An iterative echo server
35 */
36/* $begin echoserverimain */
37//#include <stdio.h>
38#include "csapp.h"
39
40void echo(int connfd){
41 size_t n;
42 char buf[MAXLINE];
43 rio_t rio;
44// printf("ehhh");
45 Rio_readinitb(&rio, connfd);
46 // printf("oooooo");
47 n = Rio_readn(connfd, buf, MAXLINE);
48// printf("kjkjkjk");
49 fprintf(stdout,"SERVER BUFFER: %c %c %c\n", buf[0], buf[1], buf[2]);
50 fprintf(stdout,"server received %d bytes\n", (int)n);
51
52 char BUF[3];
53 BUF[0] = '4'; BUF[1] = '5'; BUF[2] = '6';
54
55 Rio_writen(connfd, BUF, 3);
56
57
58}
59int EchoServer(int argc, char **argv)
60{
61 int listenfd, connfd, port; //listenfd, connfd - file descriptors
62 socklen_t clientlen;
63 struct sockaddr_in clientaddr; /*EDIT REQUIRED: ADDR Should be "INADDR_ANY" */
64 struct hostent *hp;
65 char *haddrp;
66 if (argc != 2) {
67 fprintf(stderr, "usage: %s <port>\n", argv[0]); //wrong num cmd line args
68 exit(0);
69 }
70 port = atoi(argv[1]); //takes port num from cmd line
71
72 listenfd = Open_listenfd(port); //listens for connections
73
74 while (1) {
75 clientlen = sizeof(clientaddr);
76 connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); //accept connection
77
78 /* Determine the domain name and IP address of the client */
79 hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
80 sizeof(clientaddr.sin_addr.s_addr), AF_INET);
81 haddrp = inet_ntoa(clientaddr.sin_addr);
82 fprintf(stdout, "server connected to %s (%s)\n", hp->h_name, haddrp);
83// fprintf(stdout, "ahh");
84 echo(connfd);
85 Close(connfd);
86 }
87 exit(0);
88}
89/* $end echoserverimain */
90
91
92int main(int argc, char **argv){
93
94 EchoServer(argc, argv);
95
96 return 0;
97}