Difference b/w self.packetFlow.ReadBytes vs socket read

假装没事ソ 提交于 2020-06-29 04:59:13

问题


I am trying to read data from packet tunnel NEPacketTunnelProvider. Right now what i am doing is trying to read the data using

 self.packetFlow.readPackets { [weak self] (packets: [Data], protocols: [NSNumber]) in }

function. It seems to be working fine. But now i want to read the packets by using the network handle of self.packetFlow function like this

 let tunFd = self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int32

and use this file descriptor to read the packet data from the tunnel. like this

    char *buffer1 = malloc(1024);
    ssize_t length = read(tunFd, buffer1, 1024);

But now when i read the data from socket like this. It seems like data is not similar to what i am receiving earlier using the self.packetFlow.

Does anybody else know why it is happening. and what should i do get the similar data as in self.packetFlow.readPackets..

Any pointer of help will be appreciated.


回答1:


I found the answer or my own query. There are 4 bytes of extra protocol information added to the data while we use file descriptor. We need to handle it as well.



来源:https://stackoverflow.com/questions/53914735/difference-b-w-self-packetflow-readbytes-vs-socket-read

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!