代码如下 m_target_addr=(sockaddr_un*)malloc(sizeof (sockaddr_un)); memset(m_target_addr,0,sizeof(sockaddr_un)); m_target_addr->sun_family = AF_UNIX; sprintf(m_target_addr->sun_path,"/tmp/video_un_player_socket_%s","101010101"); int n=sendto(m_socket,buffer,send_len,0, (struct sockaddr*)&m_target_addr,(socklen_t)sizeof(m_target_addr)); if(n==-1){ logi("send socket %d,buffer %d,len %d,addrsize %d,to %s",m_socket,buffer,send_len,sizeof(m_target_addr),strerror(errno)); }
时间: 2024-03-20 21:39:25 浏览: 112
您的问题出在第四个参数 `(struct sockaddr*)&m_target_addr` 上。实际上,m_target_addr已经是一个指向sockaddr_un的结构体指针,因此在sendto函数中不需要再取一次地址。修改代码如下即可:
```
int n = sendto(m_socket, buffer, send_len, 0, (struct sockaddr*)m_target_addr, sizeof(sockaddr_un));
if (n == -1) {
logi("send socket %d, buffer %d, len %d, addrsize %d, to %s", m_socket, buffer, send_len, sizeof(*m_target_addr), strerror(errno));
}
```
注意,第四个参数应该是 `m_target_addr` 而不是 `&m_target_addr`,第五个参数应该是 `sizeof(sockaddr_un)` 而不是 `sizeof(m_target_addr)`。
相关问题
void SendFile6678(char * msg, SOCKET Udp, int sourceID, int destinateID, int destinatePort, int realPort,bool isPhone, bool filetext) { struct sockaddr_in s; memset(&s, 0, sizeof(struct sockaddr_in)); s.sin_family = AF_INET; s.sin_port = htons(realPort); s.sin_addr.s_addr = inet_addr(DESTINATEID); //内容 FileMsg6678 newMsg; newMsg.destinateID = BSWAP_32(destinateID); newMsg.sourceID = BSWAP_32(sourceID); newMsg.destinatePort = BSWAP_32(destinatePort); int isphone = 1; if (isPhone) { isphone = 0; } newMsg.isPhone = BSWAP_32(isphone); memcpy(newMsg.data, msg, sizeof(DataPackage)); int ret=0; ret = sendto(Udp, (const char*)&newMsg, sizeof(FileMsg6678), 0, (struct sockaddr*)&s, sizeof(s));}报错msg无法读取内存,但是data数据却存在
根据你提供的代码,出现 "msg 无法读取内存" 的错误可能是因为 `msg` 参数指向的内存地址无效或者已经被释放。这种错误通常发生在访问非法指针或者已经释放的内存时。
首先,确保 `msg` 参数指向一个有效的字符数组,并且数组长度足够容纳 `sizeof(DataPackage)` 字节的数据。如果 `msg` 的长度小于 `sizeof(DataPackage)`,那么使用 `memcpy` 复制数据时可能会造成访问非法内存的错误。
其次,如果你在调用 `SendFile6678` 函数之前对 `msg` 进行了动态内存分配(例如使用 `malloc` 或者 `new`),请确保在调用 `sendto` 函数之后释放了该内存。
最后,检查调用 `SendFile6678` 函数时传递的参数是否正确,并且确保 `Udp` 是一个已经初始化并绑定了正确地址和端口的套接字。
如果问题仍然存在,请仔细检查代码其他部分,确保没有其他地方对 `msg` 或者 `newMsg.data` 进行了无效的操作。
如果你能提供更多关于代码上下文和错误信息的信息,我可以给出更具体的建议来帮助你解决问题。
在dalsa相机提供的GigE-V-Framework_aarch64_2.20.0.0181.tar.gz的sdk中,我想把相机数据取出通过udo协议发送出去怎么做,代码
以下是一个示例代码,它使用GigE-V-Framework SDK将图像数据从相机中提取出来,并通过UDP协议发送出去。请注意,这只是一个示例代码,您需要根据您的具体情况进行修改和适配。
```C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include <Dalsa/GevApi.h>
#include <Dalsa/GigEVision.h>
#define MAX_PACKET_SIZE 1472 // Max UDP packet size
#define PORT 8888 // Port number for UDP communication
int main(int argc, char *argv[]) {
// Initialize GigE-V Framework
GevApiInitialize();
// Open camera
GEV_DEVICE_INTERFACE device;
GEV_CAMERA_HANDLE handle;
GEV_DEVICE_INFO device_info;
memset(&device, 0, sizeof(device));
memset(&handle, 0, sizeof(handle));
memset(&device_info, 0, sizeof(device_info));
device.macLow = 0x10;
device.macHigh = 0x00;
handle = GevOpenCamera(&device, GevExclusiveMode);
if (handle == NULL) {
printf("Failed to open camera.\n");
return -1;
}
// Get camera information
GevGetCameraInfo(handle, &device_info);
// Configure camera
GevSetFeatureValue(handle, "AcquisitionMode", "SingleFrame");
GevSetFeatureValue(handle, "TriggerMode", "Off");
GevSetFeatureValue(handle, "PixelFormat", "Mono8");
// Get image size
int width = 0;
int height = 0;
GevGetFeatureValue(handle, "Width", &width);
GevGetFeatureValue(handle, "Height", &height);
// Create UDP socket
int sockfd;
struct sockaddr_in servaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // IP address of the receiver
servaddr.sin_port = htons(PORT);
// Allocate memory for image buffer
int image_size = width * height;
uint8_t *image_buffer = (uint8_t *) malloc(image_size);
// Start image acquisition
GevStartTransfer(handle, -1);
// Loop for image acquisition and UDP transmission
while (1) {
int status = GevWaitForNextImage(handle, &image_buffer, -1);
if (status == GEVLIB_OK) {
// Send image via UDP
int num_packets = (image_size + MAX_PACKET_SIZE - 1) / MAX_PACKET_SIZE; // Calculate the number of packets needed to transmit the image
int last_packet_size = image_size - (num_packets - 1) * MAX_PACKET_SIZE; // Calculate the size of the last packet
uint8_t *packet_buffer = (uint8_t *) malloc(MAX_PACKET_SIZE);
for (int i = 0; i < num_packets; i++) {
int packet_size = (i == num_packets - 1) ? last_packet_size : MAX_PACKET_SIZE;
memcpy(packet_buffer, image_buffer + i * MAX_PACKET_SIZE, packet_size);
int bytes_sent = sendto(sockfd, packet_buffer, packet_size, 0, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (bytes_sent != packet_size) {
printf("Error sending UDP packet.\n");
}
}
free(packet_buffer);
} else {
printf("Error getting image.\n");
}
}
// Stop image acquisition
GevStopTransfer(handle);
// Free memory
GevFreeImageBuffer(&image_buffer);
GevCloseCamera(&handle);
GevApiUninitialize();
return 0;
}
```
阅读全文