It is suppose to be able to only reserve a seat if it is empty, but for some reason it doesn't work on the same connection.
I have to reserve a seat, close the client, reopen the client, and then it will give me the correct message (I cannot reserve the seat).
I am running it on Ubuntu Linux.
Compile Server - gcc -o server -lpthread server.c
Compile Client - gcc -o client -lpthread client.c
here is my code
server.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
//This struct will hold seat and passenger information for each seat
typedef struct
{ char name[32];
int row;
char col;
}Reserve;
Reserve seats[120];
sem_t *sem; //Pointer to semaphore
void *receive(void *arg);
void reserve(int sock, Reserve client);
void list(Reserve x);
void intialize();
void cleanUp(int sig); //Signal handler func
int main(int argc, char *argv[])
{ int sock, clientSock;
int result;
struct sockaddr_in addr;
struct sockaddr_in cAddr;
int val = 1;
key_t semKey = 5678; //Semaphore key
pthread_t thread;
//Register signal Handler
signal(SIGINT, cleanUp);
//Make sure there is no semaphore with name "/sem"
sem_close(sem);
sem_unlink("/sem");
//Create and initialize semaphore
sem = sem_open("/sem", O_CREAT | O_EXCL, 0666, 1);
//Initialize seats
intialize();
//create socket
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock == -1)
{ perror("Could not create socket\n");
exit(1);
}
//set socket option
result = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
if(result < 0)
{ perror("ListenSock option not set\n");
return 0;
}//End of if -- sockopt
//Bind socket to port
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(55000);
result = bind(sock, (struct sockaddr*)&addr, sizeof(addr));
if(result == -1)
{ perror("Could not bind socket\n");
close(sock);
exit(1);
}
//Put socket into listening mode
result = listen(sock, 5);
if(result == -1)
{ perror("Could not put socket in listening mode\n");
close(sock);
exit(1);
}
while(1)
{ //Accpet connections
int clientL = sizeof(cAddr);
clientSock = accept(sock, (struct sockaddr*)&cAddr, &clientL);
if(clientSock == -1)
{ perror("Could not accept connection from client\n");
close(clientSock);
exit(1);
}
//Create a thread for each client request
result = pthread_create(&thread, NULL, receive, (void*) clientSock);
if(result < 0)
{ printf("Could not create a new thread\n");
close(clientSock);
exit(1);
}
//Allow new threads to execute
pthread_detach(thread);
sched_yield();
}//End of while
close(sock);
return 0;
}//End of main
//This function will initialize all Reserve struct data (120 seats)
void intialize()
{ int i, j; //i - indiviual seat indexes, j - for loop counter
char k; //for loop char counter
//initialize i
i=0;
//Initialize the array of structs
for(j=1; j<=20; j++) //Rows
for(k='A'; k<='F'; k++) //Columns
{strcpy(seats[i].name, "empty"); //Initialize all seat names to "empty"
seats[i].row = j; //Initialize rows (1-20)
seats[i].col = k; //Initialize cols (A-F)
//list(seats[i]);
i++;
}//End of for
}//End of intialize
void *receive(void *arg)
{ Reserve client; //Stores a clients reservation data
char message[43]; //Store data sent form client
char seat[4]; //Temporarily stores seatx
char col; //Temporarliy store seat column
char r[3];
int row; //Temporarily stores seat row
char name[32]; //Temporarily stores the name
int result;
//Cast socket id back to int
int sock = (int)arg;
//Accept Client data
result = recv(sock, message , sizeof(message), MSG_PEEK);
if(result == -1)
{ perror("Could not receive\n");
close(sock);
exit(1);
}
else
printf("%s\n", message);
//Pares message
if(result == 42)
{ printf("lenght is 2\n");
r[0] = message[0];
col = message[1];
strncpy(name, message+3, 32);
}
else
{ printf("length is 3\n");
r[0] = message[0];
r[1] = message[1];
col = message[2];
strncpy(name, message+4, 32);
}
//Put data into Reserve struct
client.row = atoi(r);
printf("%d\n",client.row);
client.col = col;
printf("%c\n", client.col);
strcpy(client.name, name);
printf("%s\n", client.name);
printf("%d\n", result);
//Send message back
if(result > 0)
reserve(sock, client);
}//End of receive
void reserve(int sock, Reserve client)
{ int i; //for loop counter
char res[] = "Reservation has been made.\n";
char nRes[] = "Cannot reserve seat.\n";
int result;
//Find seat and reserve it if it is open
for(i=0; i<120; i++)
{ if((seats[i].row == client.row) && (seats[i].col == client.col))
if(strcmp(seats[i].name, "empty") == 0)
{ sem_wait(sem);
strcpy(seats[i].name, client.name); //Assign a name to the reserved seat
sem_post(sem);
//Create message to send to client
/* sprintf(row, "%d", seats[i].row);
col[0] = seats[i].col;
col[1] = '\0';
strcat(res, row);
strcat(res, col);
strcat(res, " ");
strcat(res, seats[i].name);*/
send(sock, res, sizeof(res), 0);
//Send open seats to all clients
// openSeats();
}
else //Seat is already reserved
{ //Create and send message
/* sprintf(row, "%d", seats[i].row);
col[0] = seats[i].col;
col[1] = '\0';
strcat(nRes, row);
strcat(nRes, col);
strcat(nRes, " ");
strcat(nRes, seats[i].name);*/
send(sock, nRes, sizeof(nRes), 0);
}
}//End of for
printf("\n");
for(i=0; i<120; i++)
list(seats[i]);
}//End of reserve
//This function outputs a seats information
void list(Reserve x)
{ //Print seat information
printf("%d%c%s%s\n", x.row, x.col," - ", x.name);
}//End of list
void cleanUp(int sig)
{ //Close semaphore
sem_close(sem);
//Remove semaphore
sem_unlink("/sem");
exit(sig);
}//End of CleanUp
client.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
//This struct will hold seat and passenger information for each seat
typedef struct
{ char name[32];
int row;
char col;
}Reserve;
Reserve seats[120];
void display(int sock);
void makeRes(int sock);
int main(int argc, char *argv[])
{ int sock;
struct sockaddr_in addr;
int result;
//Set up address
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(55000);
//Create socket
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock == -1)
{ perror("Could not create socket\n");
exit(1);
}
//Try and connect
result = connect(sock, (struct sockaddr*)&addr, sizeof(addr));
if(result == -1)
{ perror("Cannot connect\n");
close(sock);
exit(1);
}
display(sock);
close(sock);
return 0;
}//End of main
void display(int sock)
{ int choice; //Store choice
//Didplay a menu
printf("Make another Reservation: 1\n");
printf("Exit: 2\n\n");
printf("Please enter your choice\n");
scanf("%d", &choice);
while(choice != 2)
{ switch(choice)
{ case 1:
makeRes(sock); break;
default:
printf("Invalid Choice, please try again\n");
break;
}//End of switch
//Display a menu
printf("Make another Reservation: 1\n");
printf("Exit: 2\n\n");
printf("Please enter your choice\n");
scanf("%d", &choice);
}//End of while
}//End of display
void makeRes(int sock)
{ char message[43];
char command[7];
char seat[4];
char fName[16];
char lName[16];
char response[30]; //Response to reservation
//Ask the user make a reservation
printf("The reservation should look like this\n'Reserve 1A Steve Monico'\n");
printf("Please make a reservation\n");
scanf("%s", command);
scanf("%s", seat);
scanf("%s", fName);
scanf("%s", lName);
//Create a message
// strcpy(message, command);
//strcat(message, " ");
strcpy(message, seat);
strcat(message, " ");
strcat(message, fName);
strcat(message, " ");
strcat(message, lName);
printf("%s\n", message);
//Send reservation to the server
if(strlen(seat) == 2)
send(sock, message, 42, 0);
else
send(sock, message, 43, 0);
//Recevie the response to the request
recv(sock, response, sizeof(response), MSG_PEEK);
//Output thr resposne
printf("%s\n", response);
}//End of make Res
This post has been edited by monicojr84: 08 May 2011 - 03:19 PM

New Topic/Question
Reply




MultiQuote





|