LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: Disabled
Question 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 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:
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
 
Old 12-03-2011, 12:32 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:52 AM.

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