简体   繁体   中英

Increasing winsock performance using datagrams or raw sockets

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM