Good evening all. I am working on a poker program, using a TCP client/server. I am having the client do only input/output work, while the server handles all of the operations.
I have run into a problem where I have a card structure that is populated with 5 cards from a deck. I put those 5 cards into 2 arrays, one for the face of the card, the other for the suit. I am them trying to send these arrays to the client to print the hand out for the user. The array gets filled with no problem, but when I send the array to the client, and print it, it prints nothing. There is a lot of code, most of which is messy, so for the time being I will post the code in question, unless you guys need more.
Server:
The definitions used in here are:
1 const char *face[] = { "Ace", "Two", "Three", "Four",
2 "Five", "Six", "Seven", "Eight",
3 "Nine", "Ten", "Jack", "Queen", "King"};
4 const char *suit[] = {"Diamonds", "Hearts", "Clubs", "Spades" };
5
6 const char *handFace[5];
7 const char *handSuit[5];
And the code:
01 n = write(newsockfd,"Here is your hand. Good Luck!",255);
02
03 for(v=0;v<5;v++) {
04 handFace[v] = myDeck.myCards[v].face;
05 handSuit[v] = myDeck.myCards[v].suit;
06 }
07
08 for(y=0;y<5;y++) {
09 n = write(newsockfd,&handFace[y],sizeof(handFace[y]));
10 }
11
12 for (r=0;r<5;r++) {
13 n = write(newsockfd,&handSuit[r],sizeof(handSuit[r]));
14 }
15
16 for (a=0;a<5;a++) {
17 printf("%s of %s\n", handFace[a],handSuit[a]);
18 }
For the client, the definitions are:
1 char *handFace[5];
2 char *handSuit[5];
And the code is:
01 n = read(sockfd,buffer,255);
02
03 printf("%s\n",buffer);
04
05 for(v=0;v<5;v++) {
06 n = read(sockfd,&handFace[v],sizeof(handFace[v]));
07 }
08
09 for(y=0;y<5;y++) {
10 n = read(sockfd,&handSuit[y],sizeof(handSuit[y]));
11 }
12
13 for(i=0;i<5;i++) {
14 printf("%s of %s\n", handFace[i],handSuit[i]);
15 }
It compiles and runs. However in the client, it only prints out " of " as if the array is empty. I am assuming a read or write is wrong, I just don't know where or how.
Ok, I am getting closer. I have my write statements set to
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]));
}
and my read set to
for(v=0;v<5;v++) {
n = read(sockfd,buffer1,sizeof(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]);
}
It works for the first buffer read. For example if the first face that the server sends in 4, the print loops in the client will only print
4 of
4 of
4 of
4 of
4 of
Can the buffer not be overwritten? - drewd12 3 hrs ago