Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: socketpair equivalent?



Thanks, Greg, this was helpful. My previous experience with porting BSD-socket C code to Java was that there seemed to be a simple wrapper for everything I needed, and I figured I was just being dense and not seeing the wrapper for socketpair. Clearly this is not the case.

The thing I'm looking at connecting to is Helix Player (aka RealPlayer). I'd like to be portable across Mac / Win / Linux, and the lack of a Linux version of Quicktime has me looking at alternative players. (JVLC is another alternative, but that isn't ported to Mac yet. I haven't investigated mplayer's embed-ability yet.)

For Helix, you launch a process and give it a FD (just a plain integer) on the command line, which they recommend creating using socketpair. Obviously, I can write a little C program to do this, but I was hoping I could skip that step.

-Joshua

On Apr 2, 2007, at 10:46 PM, Greg Guerin wrote:

Joshua Smith wrote:

I'm looking at integrating with a package which expects to run in its
own process, and communicate with callers via sockets.  Their C-
oriented documentation recommends creating the communications
endpoints using socketpair.    These are two-way communications
channels: I send messages down my side of the pair, and then read
from that same file descriptor to get the return code.  (I pass the
FD of their end as an integer on the command line.)

Unlike most BSD socket functions, there appears to be no socketpair
in the java.net.Socket interface.  I found something called a Pipe in
java.nio, which I think creates a one-way socket (like a unix pipe).

I'm sure the socketpair function must be wrapped in some Java API,
right?  Can one of you geniuses point me at it?

The features you want aren't directly exposed by anything in a typical Java
installation, AFAIK.


I strongly doubt that Java's Pipe class will do anything for you, mainly
because it's not inter-process.


I'm not sure about this, but if you have to pass an FD as an arg, maybe you
want the java.io.FileDescriptor class. You'd still have to set up the
connection using ServerSocket and Socket, then dig into the FileDescriptor
object from the Socket (i.e. endpoint) you want to pass to the other
process. You'd have to use reflection, since the mebers are private.


Even then, I'm not sure this will work, because the fd's may be closed when
you Runtime.exec() the other process (Runtime.exec() is technically a
fork()/execve() in Unix terms). Java is not conducive to passing
already-open fd's to child processes. It wasn't intended to be, because
the internals of sockets are OS-specific.


If the other process is expecting a connection on a Unix-domain socket,
there isn't any simple or easy way to make that connection. If you have to
use Unix-domain sockets, all I know of is the Jtux library, mentioned here:
<http://lists.apple.com/archives/java-dev/2006/Dec/msg00167.html>


I'm not sure Jtux would work, either, because of Runtime.exec() constraints.

It might help if we knew which package you're using that expects to run in
its own process with a passed-in fd arg. You might have to write a native
wrapper app of some kind, but I'm just guessing.


  -- GG


_______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


_______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: socketpair equivalent? (From: Greg Guerin <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.