Code:
/* Include all libraries for Berkeley functions */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <strings.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
/* a default port and the maximum buffer length */
#define DEFAULT_PORT 6565
#define BUFLEN 256
int x;
int main(int argc, char **argv)
{
/* variable declartion */
int n, sd, new_sd, port, client_len, ip, pid, items;
char buffer[BUFLEN], *bp;
struct sockaddr_in server, client;
struct hostent *the_client;
/* switch case for command line arguments
case 1 - the default port # is assigned to the socket (port 6565)
case 2 - the user can assign a port # instead of default */
switch(argc)
{
case 1:
port = DEFAULT_PORT;
break;
case 2:
port = atoi(argv[1]);
break;
default:
fprintf(stderr, "Usage %s [port]\n", argv[0]);
fprintf(stderr, "Exiting...\n");
exit(0);
}
sd = socket(AF_INET, SOCK_STREAM, 0); //create the socket to use socket stream
/* check to see if sd returned a value < 0 - checking if socket was created */
if (sd < 0)
{
fprintf(stderr, "ERROR: opening socket\n");
fprintf(stderr, "Exiting...\n");
exit(0);
}
/* setup server attributess*/
bzero((char *) &server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
/* binding the socking */
if (bind(sd, (struct sockaddr *) &server, sizeof(server)) < 0)
{
fprintf(stderr, "ERROR: binding socket\n");
fprintf(stderr, "Exiting...\n");
exit(0);
}
/* queue up to 5 requests */
listen(sd, 5);
client_len = sizeof(client);
char *item;
/* create random price for clients to bid on item*/
srand( (unsigned)time( NULL ) );
items = randomStartingPrice(1,4);
switch (items)
{
case 1: item = "Music CD"; break;
case 2: item = "DVD Player"; break;
case 3: item = "Sports Card"; break;
case 4: item = "House"; break;
default: break;
}
printf("Bidding price starts at for %s: $%d.00\n", item, items);
printf("\n**** Customer Bids Below ****\n");
int PRICE = 0;
/* infinite loop to keep socket tcp connection open*/
while(1)
{ printf("price is %d\n", x);
/* accept - */
new_sd = accept(sd, (struct sockaddr *) &client, &client_len);
if (new_sd < 0)
{
fprintf(stderr, "ERROR: on accepting\n");
fprintf(stderr, "Exiting...\n");
exit(0);
}
//close(sd);
pid = fork();
if (pid == 0)
{ printf("forking...\n");
close(sd);
}
if (the_client < 0)
{
fprintf(stderr, "ERROR: on get IP\n");
fprintf(stderr, "Exiting...\n");
exit(0);
}
//sprintf(buffer, "CURRENT PRICE: $%d.00\n", PRICE);
//n = write(new_sd, buffer, strlen(buffer)); //send the price to the clients
//if (n < 0)
//{
// fprintf(stderr, "ERROR: socket write\n");
///// fprintf(stderr, "Exiting...\n");
// exit(0);
//}
/* receive the bid from the client */
bzero(buffer, BUFLEN);
n = read(new_sd, buffer, BUFLEN);
if (n < 0)
{
fprintf(stderr, "ERROR: socket read\n");
fprintf(stderr, "Exiting...\n");
exit(0);
}
/* extract the price from the clients bid */
int new_price = atoi(buffer);
if (new_price > PRICE)
{
PRICE = new_price;
/* receieve bid from client */
int peerlen = sizeof(struct sockaddr);
getpeername(new_sd, (struct sockaddr *)&client, &client_len);
printf("Client IP: %s - Bids : $ %d.00 dollars\n", inet_ntoa(client.sin_addr), PRICE);
/*send the price of the bid to the client */
bzero(buffer, BUFLEN);
sprintf(buffer, "Your Bid was: $%i.00\n", PRICE);
n = write(new_sd, buffer, strlen(buffer)); //send the price to the clients
/* pass new price to function below*/
savePrice(PRICE);
}
else
{
bzero(buffer, BUFLEN);
sprintf(buffer, "Bid is too small!\n");
n = write(new_sd,buffer, strlen(buffer));
}
x = PRICE;
//else
//{
close(new_sd);
//}//
}
/* close all sockets */
//close(new_sd);
//close(sd);
return(0);
}
/*Fuction to return the random value */
int randomStartingPrice(int min, int max)
{
return ( rand() % (max - min) + min );
}
int savePrice(int p)
{
int newp;
newp = p;
return newp;
}