Hi everybody!
In the file "server.c" i created a shared memory segment of the size of a 1024 byte string and then i attached a string to the segment, just like this:
int key = 12345;
char *message = "hello_world";
if((shmid = shmget(key, sizeof(char)*1024, IPC_CREAT | 0666)) < 0){
perror("Error in shmget()!");
exit(1);
}
if((message = shmat(shmid, NULL, 0)) ==(char *) -1){
perror("Error in shmat!");
exit(1);
}
Then in the file "client.c" i try to get the message sent by the server.c trough the shared memory:
int key = 12345;
char *msg, *message;
if((shmid = shmget(key, sizeof(char)*1024, 0666)) < 0){
perror("Error in shmget!");
exit(1);
}
if ((msg = shmat(shmid, NULL, 0)) == (char*) -1) {
perror("Error in shmat!");
exit(1);
}
...
//after waiting for the server to put the message into the segment, it does:
message = msg;
printf("MESSAGE: %s\n", message);
fflush(stdout);
I don´t get any compiler errors, nor runtime errors (no segmentation faults), but the printing result is always empty, void. I really don´t understand the reason why that happens! I´ve tried a bunch of stuff like mallocs, but nothing worked. I´d be most thankfull for any help
Best regards