Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
/* Variable Declarations */
int sockfd, portno, n;
char choice;
int bet;
int bank;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
char buffer1[255];
char buffer2[255];
char buffer3[255];
char buffer4[255];
char *handFace[5];
char *handSuit[5];
int i, v, y;
/* Checks count of arguments for starting the client. */
/* Usage: ./clientname localhost <port#> */
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
/* Handles all client server functionality */
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
/* Prints main user interface menu for the user to interact with. */
printf(" \n");
printf("*****************************************\n");
printf("Welcome to the World Famous Davis' Poker!\n");
printf("*****************************************\n");
printf(" \n");
/* Ensures menu will loop allowing user to do multiple things in one run. */
do
{
printf("Please select an action:\n");
printf("1 - Play Poker\n");
printf("2 - View Statistics\n");
printf("3 - Quit\n");
printf("\n");
printf("Selection: ");
scanf( "%s", &choice ); /* Accepts users input */
/* Passes user choice to server for all work to be done. */
/* Client waits for server to do work, and send back information */
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,&choice,5);
if (choice == '1')
{
printf("How much do you wish to bet? ");
scanf("%d", &bet);
printf("You have bet %d credits\n", bet);
n = write(sockfd,&bet,10);
n = read(sockfd,buffer,255);
printf("%s\n",buffer);
for(v=0;v<5;v++) {
n = read(sockfd,buffer1,sizeof(buffer1));
printf("%s\n", buffer1);
n = read(sockfd,buffer2,sizeof(buffer2));
handFace[v] = buffer1;
handSuit[v] = buffer2;
}
for(i=0;i<5;i++) {
printf("%s of %s\n", handFace[i],handSuit[i]);
}
}
if (choice == '2')
{
n = read(sockfd,&bank,10);
printf("Your current bank is %d credits.\n",bank);
printf("\n");
}
/* if (choice < '3')
{
printf("In here\n");
printf("the choice you entered is: %c\n", choice);
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,&choice,1);
if (n < 0)
error("ERROR reading from socket");
} */
if (choice == '3')
{
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR writing to socket");
printf("%s\n",buffer);
return 0;
}
} while (buffer != NULL);
return 0;
}
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <time.h>
typedef struct card {
const char *face;
const char *suit;
}Card;
/* typedef struct card Card; */
// A more sophisticated implementation of Deck might use
// Card * myCards and memory allocation to create a deck of
// any size, instead of just 52
typedef struct {
Card myCards[52];
unsigned int topCard;
} Deck;
void fillDeck( Card * const, const char*[], const char*[] );
void shuffle( Card * const );
void initDeck (Deck * const initMe, const char *face[], const char * suit[]);
void Deal(Deck * const dealMe);
// These are very simple, they just save typing really.
char const * getFace (Deck const * const getMyFace);
char const * getSuit (Deck const * const getMySuit);
// Card * topCard (Deck * getMyTop);
int play();
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
/* Variable Declarataions */
int sockfd, newsockfd, portno, clilen;
char buffer[256];
char c;
int bet;
int bank = 100;
char choice;
struct sockaddr_in serv_addr, cli_addr;
int n;
const char *face[] = { "Ace", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"};
const char *suit[] = {"Diamonds", "Hearts", "Clubs", "Spades" };
int v;
int a;
int y;
int r;
const char *handFace[5];
const char *handSuit[5];
/* Checks count of arguments for starting the server */
/* Usage: ./servername <port#> */
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
/* Handles all client server functionality. */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
// do {
/* Reads message sent from client into choice variable */
// bzero(buffer,256);
n = read(newsockfd,&choice,5);
if (n < 0) error("ERROR reading from socket");
/* Variable checking. Prints variable to ensure message is sent correctly */
printf("Here is the message: %c\n",choice);
/* Performs main switch statement with sent input from client */
/* Sends program to appropriate function to perform the task the user requested */
switch (choice) {
case '1':
printf("You are now in play poker function\n"); /* Play poker */
Deck myDeck;
srand(time(NULL));
initDeck (&myDeck, face, suit);
Deal(&myDeck);
/* Print hand to screen. */
n = write(newsockfd,"Here is your hand. Good Luck!",255);
for(v=0;v<5;v++) {
handFace[v] = myDeck.myCards[v].face;
handSuit[v] = myDeck.myCards[v].suit;
}
for(y=0;y<5;y++) {
n = write(newsockfd,handFace[y],sizeof(handFace[y]));
n = write(newsockfd,handSuit[y],sizeof(handSuit[y]));
}
for (a=0;a<5;a++) {
printf("%s of %s\n", handFace[a],handSuit[a]);
}
// if (myDeck.myCards[0].suit == "Diamonds")
// printf("Diamond!");
// else
// printf("Not Diamonds!");
write(newsockfd,&choice,1);
break;
case '2':
printf("You are now in the view stats function\n"); /* View stats */
printf("\n");
n = write(newsockfd,&bank,10);
printf("I get past the write statement!\n");
break;
case '3':
printf("I'm in case 3!");
n = write(newsockfd,"Thanks for playing! Come back soon!",255); /* Quit program */
return 0;
break;
default:
printf( "Bad input, quitting!\n" ); /* Default statement. Switch input checking */
break;
}
// getchar();
// } while(buffer != NULL);
/* Writes choice variable back to client to test writing back to client */
n = write(newsockfd,&choice,1);
if (n < 0) error("ERROR writing to socket");
return 0;
}
/* int play()
{
const char *face[] = { "Ace", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"};
const char *suit[] = {"Diamonds", "Hearts", "Clubs", "Spades" };
int c=0;
int a=0;
const char *handFace[5];
const char *handSuit[5];
Deck myDeck;
srand(time(NULL ) );
initDeck (&myDeck, face, suit);
Deal(&myDeck);
do {
// printf("Maybe Face? %s\n", myDeck.myCards[0].face);
// printf("Maybe Suit? %s\n", myDeck.myCards[0].suit);
handFace[c] = myDeck.myCards[c].face;
handSuit[c] = myDeck.myCards[c].suit;
// n = write(newsockfd,&myDeck.myCards[c].face,sizeof(myDeck.myCards[c].face));
// n = write(newsockfd,&myDeck.myCards[c].suit,sizeof(myDeck.myCards[c].suit));
printf("%s of %s\n", myDeck.myCards[c].face, myDeck.myCards[c].suit);
c++;
} while(c<5);
for (a=0;a<5;a++) {
printf(" Array reads: %s\n", handFace[a]);
printf(" Array reads: %s\n", handSuit[a]);
}
// if (myDeck.myCards[0].suit == "Diamonds")
// printf("Diamond!");
// else
// printf("Not Diamonds!");
return ;
} */
void initDeck (Deck * const initMe, const char * face[], const char * suit[])
{
// Whether you start at 0 and count up, or start at 311 and count
// down is arbitrary.
initMe -> topCard = 0;
fillDeck (initMe -> myCards, face, suit);
shuffle (initMe -> myCards);
return;
}
void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[])
{
int i;
for( i = 0; i <= 51; i++) {
wDeck[ i ].face = wFace[ i % 13 ];
wDeck[ i ].suit = wSuit[ i / 13 ];
}
}
void shuffle( Card * const wDeck )
{
int i, j;
Card temp;
for ( i = 0; i<= 51; i++ ) {
j = rand() % 52;
temp =wDeck[ i ];
wDeck[ i ] = wDeck[ j ];
wDeck[ j ] = temp;
}
}
// Takes the top card off the deck and prints it
void Deal(Deck * const dealMe)
{
int i = 0;
int n;
do {
// n = write(newsockfd,myDeck.myCards[i].face,15);
//printf ("%5s of %-8s\n", getFace (dealMe), getSuit (dealMe));
dealMe -> topCard++;
i++;
} while (i < 5);
return;
}
// These could be implemented as macros
char const * getFace (Deck const * const getMyFace)
{
return getMyFace->myCards[getMyFace->topCard].face;
}
char const * getSuit (Deck const * const getMySuit)
{
return getMySuit->myCards[getMySuit->topCard].suit;
}