Hi everybody, I'm doing a simple client server communication system.
The client have to send in a TCP socket a couple of strings and the server receive them, then have to search in a buffer if a category is similar to even just one of the strings sent by the client, and put them in a similar buffer.
The buffer of the server is done in this way:
#define SIZE 10
struct content {
char idCategory[50];
int timestamp;
};
struct buffers {
struct content* locations;
int size;
int i_get;
int i_put;
};
struct buffers buffer;
//function that inizialize an empty buffer
void create_empty_buffer() {
buffer.size = SIZE;
buffer.locations = malloc (sizeof(struct content)*buffer.size);
buffer.i_get=0;
buffer.i_put=0;
int i;
for (i=0;i<buffer.size;i++) {
strcpy(buffer.locations[i].idCategory,"void");
buffer.locations[i].timestamp=0;
}
}
The buffer where the server copy the content to give it to the client is:
struct buffersClient {
int size;
struct content* locations;
};
Then in the server I have the function that make the copy and return it as type struct buffersClient b.
char c[5];
struct buffersClient reply;
reply=getContent(c);
//then in the function getContent
struct buffersClient getContent (char* c) {
struct buffersClient b;
b.locations=malloc(sizeof(struct bando)*SIZE);
int j;
int k;
k=0;
for (j=0; j<buffer.size;j++){
if (strcmp(c,buffer.locations[j].idCategory)==0) {
b.locations[k]=buffer.locations[j];
k++;
}
}
return b;
}
// then in the server:
write(sockmsg,&reply,sizeof(reply));
// in the client
struct buffersClient result;
read(sock,&result, sizeof(result));
This works in the server side (I can read reply) but in the client give me segmentation fault if I try to read the result content such as: result.locations[0].idCategory.
If I make the malloc in both client and server the same: segmentation fault. If I make the malloc only in the client: segmentation fault for server..
How can I do?
Thank you in advance.