Click here to Skip to main content
15,886,074 members

Passing an array through read/write in C

drewd12 asked:

Open original thread
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:

C#
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:
C#
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:

C#
1   char *handFace[5];
2   char *handSuit[5];


And the code is:
C#
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
Tags: C, Sockets

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900