0
votes

I'm having a problem with the following task, I need to receive some data from the server, until I encounter a specific set of rules (they are checked by using c++ regular expression). Currently I'm using a simple winsock socket

ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
do {

    bytes = recv(ClientSocket, buffer, LENGTH, 0);
    if ( bytes > 0 ) {
        //regex checks
         .....
        /* if regex checks are passed i need to close the connection: */
        closesocket(ClientSocket);
        break;
    }

} while( bytes > 0 );

The problem is, because the socket is a stream socket, as I understand, that might be, that additional data will arrive and will be stored in some low-level buffer by windows core tcp/ip driver, that I don't need to process, and that I don't need to be handling. And my goal is to receive data, by packets, check that data for the matching rules by regex, and then close the connection, to avoid additional low-level data receiving by windows driver. Basically I want to get a higher performance, by just dropping the connection when I achieved what i need in the incoming data. So I want to use a kind of datagram packets.So the question is will it increase performance? And is the only option for this would be to use raw socket with tcp implementation? Or maybe i could use datagran with tcp socket in winsock?

EDIT: What i mean in short words:

does SOCK_STREAM makes windows tcp/ip driver to receive data from server in internal buffer, even if i do not request it with recv, and if it is so would the SOCK_RAW will be the choice?

1
What do you expect the driver to do with data which arrives before you have finished processing a packet? The kernel must keep a buffer. But nothing says you have to read the buffer. Close the socket, and the buffer will be recycled with no extra work on your part. - rici
@rici i know that there is this MTU thing anyway, so the kernel mod driver should receive by blocks anyway, i wonder if i could make it so, using raw sockets, that if the connection is opened, kernel mod-driver would hang, and get the mtu size packets from the server, only when i call recv with the mtu size (1500 bytes?) - Vlad Savelyev
Yes, and datagram sockets also do that, and raw sockets also do that. What's the alternative - you want the driver to just ignore packets your computer receives? - user253751
@immibis i wonder if could make the driver 'hang' the connection and receive mtu size blocks from server only when i ask it from client, im not an expert in low-level programming and drivers, i do not know for sure how tcp/ip driver works, is it just constantly reads the mtu sized chunks in internal buffer? can it somehow hang the connection until it is asked to receive ? - Vlad Savelyev
@VladSavelyev TCP does indeed work that way - if you don't call recv for a while then send on the other side will wait for you to call recv. The buffer isn't MTU-sized though. - user253751

1 Answers

0
votes

SOCK_STREAM is a stream based communication just like a file. the upper application need to know how to make those stream as structured data.

Maybe you can try the SOCK_DGRAM if possible, which is based on packet, so if you receive a packet, that include all the data information as a structure, and you just process that packet as whole.