0

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.

Edited by samp4ever: n/a

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by samp4ever
0

You can't send pointers through a socket connection. Pointer addresses are only valid within its own address space.

0

Thank you for your reply gerard4143.

It is an exercise so I'm trying it in the same machine, client in a terminal and server with another terminal shell.

If I've understood what you mean, I can't send the buffersClient reply because it contains a content* locations inside.

So is not possible through socket sending a data set all togheter? I need to send more object struct content all togheter, how can I do?

Thank you again.
--

EDIT: thank you, solved with struct content locations;

Edited by samp4ever: solved

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.