Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-02-2011, 02:01 AM   #1
LQ Newbie
Registered: Dec 2011
Posts: 2

Rep: Reputation: Disabled
Question Temporarily release socket FD while keeping connection alive


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 restore fd before reply and then close.

How can we achieve that?

1) If we might somehow change `socket_type` for open socket file descriptor, we could do:
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?
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?

Old 12-03-2011, 12:32 PM   #2
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,353
Blog Entries: 1

Rep: Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656
You'd better increment the fd-limit (ulimit -n)
Or rethink the protocol.
Or using UDP.

Last edited by NevemTeve; 12-03-2011 at 12:33 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Trinity Project keeping 3.5 alive LXer Syndicated Linux News 0 11-09-2011 12:12 AM
linux programing: checking of socket connection is alive tilman1 Linux - Networking 1 09-11-2011 08:51 PM
keeping OpenSuSe 10.2 alive SolarisZen SUSE / openSUSE 10 12-22-2009 12:40 PM
Keeping the connection alive? NonSumPisces Linux - Networking 2 07-08-2004 05:12 PM
Keeping dialup alive kobe Linux - General 3 08-18-2002 01:46 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:43 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration