LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Temporarily release socket FD while keeping connection alive... (https://www.linuxquestions.org/questions/linux-networking-3/temporarily-release-socket-fd-while-keeping-connection-alive-916740/)

_gg 12-02-2011 05:51 AM

Temporarily release socket FD while keeping connection alive...
 
Hi,

I have TCP/IPv4 listen server.
A client connects to it, sends some data and then connection is inactive, but stays alive.
From now, client sends no more data.
After some time, by some third-party notification, server replies to the client and that connection is closed.

`accept()` creates file descriptor and while that connection is alive it occupies that fd.
My server should be capable to hold big amount of such parallel connections, even much more than file descriptor limit.

The solution can be: temporarily release socket file descriptor for that connection after receiving data and manually assign fd to that existing connection before reply and then close.

How can we solve that?

---
1) If we might somehow change `socket_type` for open socket file descriptor, we could do:
Code:

listen(server, 20);
s = accept(...);
recv(s, request, sizeof(request));
change_socket_type(s, SOCK_DGRAM);
close(s); // this won't send TCP FIN, just release `s` fd
...
s = socket(AF_INET, SOCK_DGRAM, 0);
bind(...); // set local endpoint
connect(...); // set remote endpoint
change_socket_type(s, SOCK_STREAM); // restore fd for existing
  // TCP connection without sending SYN, ACK
send(s, ...); // reply to client
close(s); // send TCP FIN and close connection

How to do this, if possible?

2) Emulate all TCP layer by using RAW sockets. Thus we can perform low-level TCP actions. The only problem is to make listen server, so kernel doesn't manage listening port and replying "Connection refused". How about TCP listening with `backlog=0` and not accepting the incoming connections?
Code:

listen(server, 0); // minimal queue not to occupy memory
...
recv(raw_server, ...); // sniff for TCP connection request
...
sendto(raw_server, ...); // manage TCP via RAW

Won't kernel interfere our sniffer, while client is infinitely waiting for accept?
Maybe you have some other idea how to do this?

Taras

(thread is copied from Linux - Server)

TimothyEBaldwin 12-03-2011 04:39 AM

Increase the file descriptor limit and/or modify the protocol not to leave connections idle for so long. In Linux the file descriptor limit can be increased to 2,147,483,648.


All times are GMT -5. The time now is 09:33 AM.