echoreq.c 4.1 KB
Newer Older
cdf009's avatar
help  
cdf009 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/*CSCI 315 Operating Systems Design
 * Author: L. Felipe Perrone
 * Date: 2014-09-21
 * Copyright (c) 2014 Bucknell University
 *
 * Permission is hereby granted, free of charge, to any individual
 * or institution obtaining a copy of this software and associated
 * documentation files (the "Software"), to use, copy, modify, and
 * distribute without restriction, provided that this copyright
 * and permission notice is maintained, intact, in all copies and
 * supporting documentation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL BUCKNELL UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE 512
#define TRUE 1
#define FALSE 0

/*------------------------------------------------------------------------
 * Program:   echoreq
 *
 * Purpose:  allocate a socket, connect to a server, transfer requested
 *            file to local host, and print file contents to stdout
 *
 * Usage:    echoreq [ host ] [ port ] [ string ] 
 *
 *               host  - name of a computer on which server is executing
 *               port  - protocol port number server is using
 *     string - a string in double quotes
 *     
 *------------------------------------------------------------------------
 */

int
main(int argc, char* argv[]) {

        struct  hostent  *ptrh;  // pointer to a host table entry       
        struct  sockaddr_in sad; // structure to hold an IP address     

        int     sd;                              // socket descriptor                   
        int     port;                          // protocol port number          
        char *host;                // pointer to host name              
        char  in_msg[BUFFER_SIZE]; // buffer for incoming message

        int ret_val;
  	sad.sin_family = AF_INET;                 // set family to Internet     

        // verify usage

        if (argc < 4) {
                printf("Usage: %s [ host ] [ port ] [ string ]\n", argv[0]);
                exit(-1);
        }

        host = argv[1];
        port = atoi(argv[2]);

        if (port > 0)
                // test for legal value         
                sad.sin_port = htons((u_short)port);
        else {
                // print error message and exit 
                printf("ECHOREQ: bad port number %s\n", argv[2]);
                exit(-1);
        }

        // convert host name to equivalent IP address and copy to sad 

        ptrh = gethostbyname(host);

        if ( ((char *)ptrh) == NULL ) {
                printf("ECHOREQ: invalid host: %s\n", host);
                exit(-1);
        }

        memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);

        // create socket 

        sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (sd < 0) {
                printf("ECHOREQ: socket creation failed\n");
                exit(-1);
        }

        // connect the socket to the specified server 

        if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
                perror("ECHOREQ: connect failed");
                exit(-1);
        }

        // send message to server
cdf009's avatar
help2  
cdf009 committed
110
        send(sd, &in_msg, BUFFER_SIZE, 0);
cdf009's avatar
help  
cdf009 committed
111 112 113 114 115
	printf("sent");
           // send the received string back to client
			printf("\n"); 
                //                 //send(sd, buf, len flags);
        strcpy(in_msg, "");
cdf009's avatar
help2  
cdf009 committed
116
	recv(sd, &in_msg, BUFFER_SIZE, 0);
cdf009's avatar
help  
cdf009 committed
117 118 119 120 121 122 123 124 125 126 127 128 129

                //
        // receive message echoed back by server

        printf("ECHOREQ: from server= %s\n", in_msg);

        // close the socket   
        close(sd);

        // terminate the client program gracefully 
        return(0);
}