Domanda

I want to get away from the sequential and slow way of reading from a socket, in which we use:

struct PACKET_STRUCT{
   int PacketType;
   char buffer[50];
};

char buffer[sizeof(PACKET_STRUCT)];
struct sockaddr_storage addr;
socklen_t fromlen = sizeof(addr);
int iByteCount = recvfrom(CProperties->m_iSocket, buffer, sizeof (buffer), MSG_PEEK, (struct sockaddr*)&addr, &fromlen);

That means, if the client sends me PACKET_STRUCT (Packet #1) and another PACKET_STRUCT (Packet #2) -- I would have to read Packet #1 before I am able to read from Packet #2.

Is there a way where I can offset in the recvfrom, starting at the sizeof(PACKET_STRUCT) in which I would be able to read Packet #2 without reading Packet #1?

And so on sizeof(PACKET_STRUCT)*2 to read Packet #3.

I understand there is a pread() that allows for reading a file descriptor at a certain offset, but I would like to keep the MSG_PEEK flag.

There is also an lseek() function that sets the position of the file descriptor, but I will be having several worker threads reading on that file descriptor (and I would prefer not to use a mutex as that is also sequential.)

So my question is, is there a recvmsg-similar function with an offset and MSG_PEEK flags?

È stato utile?

Soluzione

There's no concept of seeking or skipping data on a socket, so you can't do that. (lseek/pread can't be used on sockets)

Some platforms lets you receive many datagrams in one call though, using the recvmmsg, if you don't care about the first message - just receive and ignore it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top