我已經閱讀過早期的問題和對我的問題的解答,但大多數人似乎使用shmget而不是mmap。使用mmap在C中構建共享內存
我正在使用mmap作爲共享內存,它一切正常,直到我從使用靜態變量改爲結構。目前,我發現了以下錯誤:
server.c: In function ‘interfaceComms’:
server.c:129:18: error: ‘interfaceConnection’ undeclared (first use in this function)
n = write(sock, interfaceConnection->processingResults, 47);
^
server.c:129:18: note: each undeclared identifier is reported only once for each function it appears in
server.c:139:12: error: ‘imageDetails’ undeclared (first use in this function)
if(strcmp(imageDetails->prevImage, "") != 0) {
^
server.c: In function ‘robotComms’:
server.c:162:2: error: ‘imageDetails’ undeclared (first use in this function)
imageDetails->prevImage = imageDetails->currImage;
^
server.c:213:9: error: ‘interfaceConnection’ undeclared (first use in this function)
strcpy(interfaceConnection->processingResults, msg);
我理解錯誤,但我不知道如何使用它結構,因爲這是我想共享內存做的方式來解決。我是分叉的,所以這些結構正在用於兒童流程。
我的代碼:
typedef struct Images {
char prevImage;
char currImage;
} image;
typedef struct Robot {
char coordinates;
} robot;
typedef struct Interface {
char processingResults;
} interface;
int main(int argc, char *argv[]){
int sockfd, newsockfd, portno, clilen, memid;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n, pid;
interface *interfaceConnection;
image *imageDetails;
// First call to socket() function
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){
perror("Error opening socket");
exit(1);
}
// Initialise socket structure
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5001;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
//Bind the socket
if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
perror("Error binding socket");
exit(1);
}
//Listen for client connections
listen(sockfd,5);
clilen = sizeof(cli_addr);
//Share memory before forking
interfaceConnection = mmap(NULL, sizeof(interface), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
imageDetails = mmap(NULL, sizeof(image), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
函數 'interfaceComms' 和 'robotComms' 被稱爲後來叉發生後。但我猜我的問題在那裏。
按照要求,intefaceComms和robotComms功能:
void checkMessage(int sock){
int n;
char message[256];
n = read(sock, message, 255);
int response;
if(n < 0){
perror("Error reading the socket");
exit(1);
}
//see if the message is from the robot or from the interface
if(strstr(message,"robot") != NULL) {
//The robot wants the co-ordinates of the component
robotComms(sock);
} else {
//Someone has accessed the main page
interfaceComms(sock);
}
}
void interfaceComms(int sock) {
int n;
n = write(sock, interfaceConnection->processingResults, 47);
if (n < 0){
perror("Error writing to the socket");
exit(1);
} else {
printf("Results sent to interface\n");
}
}
void robotComms(int sock) {
int n;
srand (time(NULL)); //reset rand
//printf("Here is the message: %s\n", message);
printf("Getting image...\n");
//Change currImage to prevImage
imageDetails->prevImage = imageDetails->currImage;
在此先感謝。
「我猜我的問題在某處」。請不要猜測(你在這裏正是因爲你不知道錯誤在哪裏)。只是告訴我們所有的信息。特別是至少要顯示那些編譯錯誤來自的代碼行是合乎邏輯的。也就是說,至少要顯示'interfaceComms'函數。但最好提供[最小的完整和可驗證的示例](http://stackoverflow.com/help/mcve)。 – kaylum
已更新。我的代碼有些部分無法顯示,但我認爲添加的代碼應該涵蓋您要求的內容 – mfisher91