?
Solved

Sharing a socket?

Posted on 2003-03-05
8
Medium Priority
?
328 Views
Last Modified: 2010-04-15
I have Client A and Server B communicating via tcp/ip sockets. Most requests from A are answered immediately and the socket closed thereafter. Certain requests require a longer lifetime of the socket connection. For those requests I want Server B to pass responsibility to Server C (running on the same machine) without Client A noticing. Can I do a dup on Server B's socket and somehow pass it to Server C or are sockets non-shareable between processes? Just wondering if it's theoretically doable before I run off an write lots of code.
0
Comment
Question by:docfab
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 150 total points
ID: 8076193

Everything is theoretically doable, but sometimes there's an easier way.  :)


One of the traits of socket programing is that the task listening to the socket rarely performs the actual work.  If the volume of work is very small, it can make sense to merge the listener and application code, but the norm is to separate the functionality by having the listener spawn off another task.

Here's how it works.  Within the listener, when a socket connection is established, perform the following (pseudo-code):

Inside the listener loop:

if (spawn())  
  continue;
else
  HandleSocketConnection ();


The spawn() function literally replicates the running task into another task.  spawn() returns the child task ID to the task that initiated the spawn() and returns 0 to the child.  This is how the two tasks determine whether they are the original task or the new task.

Now the listener is free to watch for new connection requests and the child task is free to manage the connection.  It can send/receive as much data as it requires, update databases, connect to other servers, etc without impacting the programs ability to catch other connect requests.

To answer your specific question, the child task that has just been created is now free to create its own socket connection to Server C.


Kdo
0
 

Author Comment

by:docfab
ID: 8076292
Thank you Kdo, more on the specific question, server B already behaves as you describe and I know you can share connections between forked processes. My question is probably not clear enough but anyway if I understand you correctly you suggest that server B (or rather forked child of server B) create a socket connection to Server C and act as a bridge between Client A and Server C. I want to free Server B (or any of his child processes) completely from the loop by simply passing the socket to Server C which is actually running alongside Server B (not spawned). I was thinking along the lines of:

Server B:
   helllo special request on socket1
   socket2 = dup(socket1)
   pass socket2 to Server C
   close socket1
   goodbye special request

Server C:
   receive socket2
   do heavy stuff
   close socket2




0
 

Author Comment

by:docfab
ID: 8076330
I don't want to take up any of Server B's resources with the special requests, just recognize them and pass them along. Reason being that the special requests are not guaranteed to return immediately and Server B has enough on his hands already with frequent normal requests.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:zebada
ID: 8078568
It looks like what you are asking for is an n-tier client server implementation that provides connection-pooling and load balancing.
You've just asked for an "application server".
If you can spare the cash go and buy one - believe me it is *much* easier in the long run.

If you want to roll your own be prepared to spend many, many hours building it - that is if you want it to be bullet proof.

If you are running on Unix/Linux look into using message queues to send data between processes on the same machine instead of sockets. Message queues can be shared so that more than one process can send data into the same message queue.

I'm not sure of the equivalent functionality of message queues on Windows.

i.e.
- have both B anc C servers waiting for requests via message queue - with no socket connections at all.
- have a socket listener process that spawns handler processes like Kdo says.
- The listener immediately resumes listening after handing off the connection to the spawned process.
- The spawned process (the handler) is then responsible for reading the message from the client via the socket connection that was nailed up by the listener and sending the message to B's message queue (or it could send it directly to C's message queue).
- If B gets the message but it wants to hand it off to C then just have B on-send the message to C message queue.

Regards
Paul
0
 

Author Comment

by:docfab
ID: 8087761
I am obviously not being clear enough. I don't have the option of changing server B, it already is an 'application server' of some sorts, this is however a new type of request from client A and I don't want to encumber server B with it. So still my question goes unanswered, is it possible to pass responsibility for a socket to another unrelated process, nb. not a child process?

I guess I'll just go ahead and write some code and try it out.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8088980

I'm unaware of any solution that will work without changing the code to Server B.


Having the Server A task open a socket to Server B and acting as a "passthrough" has approximately the same overhead as performing a socket handoff.  The only additional overhead to Server A is the dependent upon the amount of communications traffic that you'll generate.


You've already got a handle on socket programming.  You should be able to write and test this scenario in a very short time.


Kdo
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10089316
Nothing has happened on this question in more than 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer Kdo [grade B] (asker doesn't seem sufficiently satisfied).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question