Skip to content

Commit eaf1543

Browse files
committed
set TCP_NODELAY
1 parent c3ed6ed commit eaf1543

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

examples/rpc/rpc-server.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <sys/types.h>
1313
#include <sys/socket.h>
1414
#include <netinet/in.h>
15+
#include <netinet/tcp.h>
1516
#include <arpa/inet.h>
1617
#include <stdio.h>
1718
#include <stdlib.h>
@@ -92,6 +93,14 @@ int main(int argc, char * argv[])
9293
fprintf(stderr, "Failed to accept client connection\n");
9394
return 1;
9495
}
96+
// set TCP_NODELAY to disable Nagle's algorithm
97+
int flag = 1;
98+
int ret = setsockopt(client_socket, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
99+
if (ret < 0) {
100+
fprintf(stderr, "Failed to set TCP_NODELAY\n");
101+
close(client_socket);
102+
continue;
103+
}
95104
printf("Accepted client connection\n");
96105
rpc_serve_client(backend, client_socket);
97106
printf("Client connection closed\n");

ggml-rpc.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <sys/socket.h>
1111
#include <sys/types.h>
1212
#include <netinet/in.h>
13+
#include <netinet/tcp.h>
1314
#include <netdb.h>
1415
#include <string.h>
1516
#include <unistd.h>
@@ -57,6 +58,12 @@ static int socket_connect(const char * host, int port) {
5758
if (sock < 0) {
5859
return -1;
5960
}
61+
// set TCP_NODELAY to disable Nagle's algorithm
62+
int flag = 1;
63+
int ret = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
64+
if (ret < 0) {
65+
return -1;
66+
}
6067
addr.sin_family = AF_INET;
6168
addr.sin_port = htons(port);
6269
struct hostent * server = gethostbyname(host);

0 commit comments

Comments
 (0)