Commit 80abbcad authored by Ryan Pasculano's avatar Ryan Pasculano

I thinkit is fixed

parent aebcd6ac
/*
* CSCI 315 Operating Systems Design
* Author: L. Felipe Perrone
* Date: 2010-02-16
* Copyright (c) 2011 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 <sys/uio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wrappers.c"
#define BUFFER_SIZE 512 // length of message buffer
#define QLEN 6 // length of request queue
#define FOREVER 1
#define TRUE 1
#define FALSE 0
int num_requests = 0; // tally of client requests
/*------------------------------------------------------------------------
* Program: echod - a server for echo requests
*
* Purpose: repeatedly execute the following:
* (0) wait for a connection request from client
* (1) wait for a null-terminated string from client
* (2) send back the same string to client
* (3) close the connection
* (4) go back to step (0)
*
* Usage: echod [ port ]
*
* port - a port number in user space
*
*------------------------------------------------------------------------
*/
int
main (int argc, char* argv[]) {
struct sockaddr_in sad; // structure to hold server's address
struct sockaddr_in cad; // structure to hold client's address
int sd, sd2; // socket descriptors
int port; // protocol port number
socklen_t alen; // length of address
char in_msg[BUFFER_SIZE];
char read_msg1[BUFFER_SIZE];
// buffer for incoming message
// prepare address data structure
// The memset call is ESSENTIAL!
// if you don't do this every time you create a sockaddr struct, you will
// see some pretty strange behaviour
memset((char *)&sad,0,sizeof(sad)); // zero out sockaddr structure
sad.sin_family = AF_INET; // set family to Internet
sad.sin_addr.s_addr = INADDR_ANY; // set the local IP address
// verify usage
if (argc > 1) {
port = atoi(argv[1]);
}
else {
printf("Usage: %s [ port ]\n", argv[0]);
exit(-1);
}
if (port > 0)
// test for illegal value
sad.sin_port = htons((u_short)port);
else {
// print error message and exit
fprintf(stderr,"ECHOD: bad port number %s\n", argv[1]);
exit(-1);
}
// create socket
sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sd < 0) {
perror("ECHOD: socket creation failed");
exit(-1);
}
// assign IP/port number to socket where connections come in
if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
perror("ECHOD: bind failed");
exit(-1);
}
// set up socket to receive incomming connections
if (listen(sd, QLEN) < 0) {
perror("ECHOD: listen failed");
exit(-1);
}
// main server loop - accept and handle requests
while (FOREVER) {
alen = sizeof(cad);
if ( (sd2 = accept(sd, (struct sockaddr *)&cad, &alen)) < 0) {
perror("ECHOD: accept failed\n");
exit(-1);
}
num_requests++;
// receive the string sent by client
//char write_msg[BUFFER_SIZE];
recv(sd2,&in_msg, BUFFER_SIZE, 0);
printf("done");
printf("Received:%s\n", in_msg);
// Read(parent_to_child[READ_END], &read_msg, BUFFER_SIZE);i
strcpy(read_msg1, "");
char* str1 = " ";
char* str2 = strtok(in_msg, str1);
while(str2!= NULL){
printf("Words in String:%s\n", str2);
strcat(read_msg1, str1);
strcat(read_msg1, str2);
str2 = strtok(NULL, str1);
}
send(sd2, &read_msg1, BUFFER_SIZE, 0);
Close(sd2);
}
}
/*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);
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