I’m writing a custom tunnel (first there’s a custom hello and then the connection becomes a tunnel), but it’s pretty slow.
I’m wondering if there is anything I can do to increase speed.
One way to increase speed for connections that use short messages for instance would be to disable the nagle algorithm (TCP_NODELAY).
What would you recommend for tunneling?
I am tunneling RTSP and HTTP if that helps.
EDIT: The code is as simple as it could possibly be:
int remote_fd;
int local_fd;
int fdmax;
char buf[1 << 10];
fdset master_set, read_set;
FD_ZERO(&master_set);
FD_ZERO(&read_set);
FD_SET(remote_fd, &master_set);
FD_SET(local_fd, &master_set);
fdmax = remote_fd > local_fd ? remote_fd : local_fd;
//Connect both remote_fd and local_fd
...
while (1) {
read_set = master_set;
select(fdmax + 1, &read_set, NULL, NULL, NULL);
if (FD_ISSET(local_fd, &read_set)) {
int n = recv(local_fd, buf, sizeof(buf), 0);
send(remote_fd, buf, n, 0);
}
if (FD_ISSET(remote_fd, &read_set)) {
int n = recv(remote_fd, buf, sizeof(buf), 0);
send(local_fd, buf, n, 0);
}
}
I have omitted error handling and the code connecting the sockets to make it more readable.
The problem may be in your code rathar than the socket options. TCP_NODELAY may or may not help. Large socket and and receive huffers may help. Your code may be introducing latency. Show us some code.