We help IT Professionals succeed at work.

Question about sockets

zionz
zionz asked
on
Medium Priority
244 Views
Last Modified: 2010-04-22
Hello i have a program that uses a tcp socket to connect to a server, i want to send packets using that socket but from another program, i've tryed opening the socket file descriptor listed in /proc/<pid>/fd but that didn't worked for sockets (it worked on normal files). Is there a way to do this?
Comment
Watch Question

Commented:
look in <sys/socket.h> for the send function
it just might do the trick for ya
man 2 send
Top Expert 2005

Commented:
Sorry - there's no way to take over file descriptor of another process(open file or socket).
opening /proc/PID/fd/number opens new instance of file in fact (it's symlink).
OK - there is possibility to transfer opened file descriptor from one process to another, but they have to cooperate(process have to be willing to give it's opened socket to another) - can they in Your example?

Commented:
Hi

A simple way is to fork your process and as the child-process you can communicate with parent-process over sockets.

regards,
a2h0mi
Software Developer
CERTIFIED EXPERT
Commented:
The /proc symbolic link for a socket looks like:
   lrwx------  1 dunc users 64 2006-10-21 10:31 10 -> socket:[1225]
When you try to open it, it fails:
   10:54:37$ cat 10
   cat: 10: No such device or address
It really is the open() call that fails (from strace):
   open("10", O_RDONLY|O_LARGEFILE)        = -1 ENXIO (No such device or address)
If the fd is a symlink to a real file then of course it works.

You need to modify the program that has the socket with the server. Make sure the socket fd has close_on_exec flag *not* set. Now any program you fork and exec has access to the socket. Any recieved data item can only be recieved by one process - the other misses out. So there needs to be co-operation in that regard.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2005

Commented:
since no input from author: delete/no refund.
Duncan RoeSoftware Developer
CERTIFIED EXPERT

Commented:
Is that the rule?
Duncan RoeSoftware Developer
CERTIFIED EXPERT

Commented:
I mean, if it was the rule then Venabili  could just have recommened it straight off
Top Expert 2005

Commented:
duncan_roe: what You want? It's just my suggestion. If You have another, bring it here.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.