Commit 9699a4b4 authored by cdf009's avatar cdf009

Lab 4 finally done

parent 3a45f2c7
......@@ -9,4 +9,6 @@ wrappers.o: wrappers.h wrappers.c
echoreq: echoreq.c wrappers.o
$(CC) $(CLAGS) echoreq.c -o echoreq
echod: echod.c wrappers.o
$(CC) $(CFLAGS) echod.c -o echod
$(CC) $(CFLAGS) echod.c -o echod
echoreq2: echoreq2.c wrappers.o
$(CC) $(CLAGS) echoreq2.c -o echoreq2
/*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>
#include "wrappers.c"
#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);
struct addrinfo hints;
struct addrinfo *result, *rp;
int sfd, s;
struct sockaddr_storage peer_addr;
socklen_t peer_addr_len;
ssize_t nread;
char buf[BUFFER_SIZE];
if (argc != 2) {
fprintf(stderr, "Usage: %s port\n", argv[0]);
exit(EXIT_FAILURE);
}
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
s = getaddrinfo(NULL, argv[1], &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
/* getaddrinfo() returns a list of address structures.
* Try each address until we successfully bind(2).
* If socket(2) (or bind(2)) fails, we (close the socket
* and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sfd == -1)
continue;
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
break; /* Success */
close(sfd);
}
if (rp == NULL) { /* No address succeeded */
fprintf(stderr, "Could not bind\n");
exit(EXIT_FAILURE);
}
freeaddrinfo(result); /* No longer needed */
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
char write_msg[BUFFER_SIZE];
strcpy(write_msg, argv[3]);
send(sd, &write_msg, BUFFER_SIZE, 0);
printf("sent");
// send the received string back to client
printf("\n");
// //send(sd, buf, len flags);
strcpy(in_msg, "");
recv(sd, in_msg, BUFFER_SIZE, 0);
//
// 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);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment