set TCP_NODELAY

This commit is contained in:
Radoslav Gerganov 2024-04-29 14:38:45 +03:00
parent bfdd4f4045
commit e539b45e5a
2 changed files with 16 additions and 0 deletions

View file

@ -12,6 +12,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
@ -92,6 +93,14 @@ int main(int argc, char * argv[])
fprintf(stderr, "Failed to accept client connection\n");
return 1;
}
// set TCP_NODELAY to disable Nagle's algorithm
int flag = 1;
int ret = setsockopt(client_socket, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
if (ret < 0) {
fprintf(stderr, "Failed to set TCP_NODELAY\n");
close(client_socket);
continue;
}
printf("Accepted client connection\n");
rpc_serve_client(backend, client_socket);
printf("Client connection closed\n");

View file

@ -10,6 +10,7 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
@ -57,6 +58,12 @@ static int socket_connect(const char * host, int port) {
if (sock < 0) {
return -1;
}
// set TCP_NODELAY to disable Nagle's algorithm
int flag = 1;
int ret = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
if (ret < 0) {
return -1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
struct hostent * server = gethostbyname(host);