我认为这是一个非常直接的问题,但我仍然无法弄清楚。
我有通过网络发送流的功能。自然,这需要 const void * 作为参数:
void network_send(const void* data, long data_length)
我正在尝试在通过套接字发送之前以 char* 的形式在其前面添加一个特定的标头:
long sent_size = strlen(header)+data_length;
data_to_send = malloc(sent_size);
memcpy(data_to_send,header,strlen(header)); /*first copy the header*/
memcpy((char*)data_to_send+strlen(header),data,dat_length); /*now copy the actual data*/
只要数据实际上是 char* ,它就可以正常工作。但如果它更改为其他一些数据类型,那么这将停止工作。
接收时,我需要在处理之前从数据中删除标题。所以它是这样做的:
void network_data_received(const void* data, long data_length)
{
........
memmove(data_from_network,(char*)data_from_network + strlen(header),data_length); /*move the data to the beginning of the array*/
ProcessFurther(data_from_network ,data_length - strlen(header)) /*data_length - strlen(header) causes the function ProcessFurther to read only certain part of the array*/
}
如果数据是 char 类型,这又可以正常工作。但如果它是任何不同的类型就会崩溃。
谁能建议如何正确实施?
问候,汗