Bin文件如何通过共享内存方式从Linux端CPU0发送到裸机CPU1完整代码
时间: 2024-02-19 14:02:38 浏览: 154
以下是一个简单的示例代码,用于演示如何通过共享内存方式从Linux端CPU0发送Bin文件到裸机CPU1:
Linux端CPU0代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SHM_KEY 0x1234
#define SHM_SIZE 0x10000
#define PORT 12345
int main(int argc, char *argv[]) {
int shmid;
char *shmaddr;
struct sockaddr_in serv_addr;
int sockfd, connfd;
int bin_size;
char *bin_data;
int ret;
// 读取Bin文件内容
FILE *fp = fopen("test.bin", "rb");
if (fp == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
return -1;
}
fseek(fp, 0, SEEK_END);
bin_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
bin_data = malloc(bin_size);
fread(bin_data, 1, bin_size, fp);
fclose(fp);
// 创建共享内存区域
shmid = shmget(SHM_KEY, SHM_SIZE, IPC_CREAT | 0666);
if (shmid == -1) {
fprintf(stderr, "Failed to create shared memory: %s\n", strerror(errno));
return -1;
}
shmaddr = shmat(shmid, NULL, 0);
if (shmaddr == (void *) -1) {
fprintf(stderr, "Failed to attach shared memory: %s\n", strerror(errno));
return -1;
}
// 将Bin文件内容写入共享内存区域
memcpy(shmaddr, bin_data, bin_size);
// 发送共享内存信息给裸机CPU1
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
return -1;
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("192.168.1.100"); // 裸机CPU1的IP地址
serv_addr.sin_port = htons(PORT);
ret = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (ret == -1) {
fprintf(stderr, "Failed to connect to server: %s\n", strerror(errno));
return -1;
}
struct {
int shmid;
int size;
void *addr;
} shm_info;
shm_info.shmid = shmid;
shm_info.size = bin_size;
shm_info.addr = shmaddr;
ret = send(sockfd, &shm_info, sizeof(shm_info), 0);
if (ret == -1) {
fprintf(stderr, "Failed to send shared memory info: %s\n", strerror(errno));
return -1;
}
// 关闭socket和共享内存
close(sockfd);
shmdt(shmaddr);
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
```
裸机CPU1代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 12345
int main(int argc, char *argv[]) {
int shmid;
char *shmaddr;
struct sockaddr_in serv_addr, cli_addr;
int sockfd, connfd;
socklen_t clilen;
int ret;
// 创建socket并监听
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
return -1;
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(PORT);
ret = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (ret == -1) {
fprintf(stderr, "Failed to bind socket: %s\n", strerror(errno));
return -1;
}
listen(sockfd, 1);
// 接收共享内存信息
clilen = sizeof(cli_addr);
connfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (connfd == -1) {
fprintf(stderr, "Failed to accept connection: %s\n", strerror(errno));
return -1;
}
struct {
int shmid;
int size;
void *addr;
} shm_info;
ret = recv(connfd, &shm_info, sizeof(shm_info), 0);
if (ret == -1) {
fprintf(stderr, "Failed to receive shared memory info: %s\n", strerror(errno));
return -1;
}
// 关联共享内存区域并读取Bin文件内容
shmid = shm_info.shmid;
shmaddr = shmat(shmid, NULL, 0);
if (shmaddr == (void *) -1) {
fprintf(stderr, "Failed to attach shared memory: %s\n", strerror(errno));
return -1;
}
char *bin_data = malloc(shm_info.size);
memcpy(bin_data, shmaddr, shm_info.size);
// 打印Bin文件内容
printf("Bin file content:\n");
for (int i = 0; i < shm_info.size; i++) {
printf("%02X ", bin_data[i]);
if ((i + 1) % 16 == 0) {
printf("\n");
}
}
printf("\n");
// 关闭socket和共享内存
close(connfd);
close(sockfd);
shmdt(shmaddr);
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
```
注意,以上代码仅用于演示如何通过共享内存方式发送Bin文件,实际应用中需要根据具体情况进行修改和优化。同时,需要注意安全性和可靠性问题,例如应对异常情况和数据不一致问题。
阅读全文