0

Currently in my C++ application I authenticate a client by creating a simple POST request then sending it to my webserver (all data is encrypted during transfer) through the C++ socket then receiving and processing the response. The flow of how I make connection and send/receive response to/from my server is like so:

(error handling and other code has been removed, this is all important code relevant to question)

createSocket = socket(AF_INET, SOCK_STREAM, 0);
connect(createSocket, (struct sockaddr *)&sock_t, sizeof(sock_t));
send(createSocket, POSTRequestSend, strlen(POSTRequestSend), 0);
recv(createSocket, responseBuffer, 6000, 0);

So this works just fine, but my question is should I apply any socket options via setsockopt() to my socket? Being that I am only receiving and sending small pieces of data I was wondering if there are any socket options that could help improve performance or if there are any socket options I in general should be using?

I.e. I've seen some examples of people creating/sending similar requests to mine apply these socket options:

int on = 1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&on, sizeof(int));

So if I were to add this to my socket code it would look like:

int on = 1;
createSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
setsockopt(createSocket, IPPROTO_TCP, TCP_NODELAY, (const char *)&on, sizeof(int));
connect(createSocket, (struct sockaddr *)&sock_t, sizeof(sock_t));
send(createSocket, POSTRequestSend, strlen(POSTRequestSend), 0);
recv(createSocket, responseBuffer, 6000, 0);

Would there be any benifit to setting these socket options? Or are there any other socket options that I should be using based on me only sending POST request with small amounts of data sent/received.

Power5
  • 69
  • 1
  • 5
  • If it’s authentication I assume it’s a one-off process? So if you could even in theory shave some milliseconds off, would it matter? Or are there some other data sending requests going on? In general anyway these won’t matter. – Sami Kuhmonen Sep 22 '18 at 06:07
  • Don't just blindly do something because you see something else doing it. Read up on it instead and figure out if it makes sense to do. (In the case of `TCP_NODELAY`, it usually doesn't) – Shawn Sep 22 '18 at 07:51

0 Answers0