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.
I do not think that disabling the nagle algorithm would help much. You provide too little information to give any more specific help. Hence the rest of the answers are only guesses. You need to provide which platform/OS each end of the tunnels is on and which programming language you are using.
For instance. HTTP tunneling would benefit greatly if you compress the requests/responses. But that would use a lot more battery if you are on a handheld device.
RTSP would benefit if you used UDP instead of TCP since it's a real time protocol that doesn't really care if everything arrives properly.
精彩评论