Can I get a Java Socket from a file descriptor number?
Asked Answered
S

2

0

When a program is started via FastCGI, it is exec'd with a socket already open to talk to the web server. The socket's file descriptor number is handed to the program, but how can that be converted to something useful in Java, such as a Socket instance?

My hosting service uses mod_fastcgi for Apache httpd. They won't allow me to configure an external server where I provide a host:port to forward requests to via FastCgiExternalServer.

Seabee answered 7/8, 2009 at 8:14 Comment(0)
S
1

You can't do this legally. However, you can do a hack like this (Don't try this at home). You can read from is and write to os.

    Class<FileDescriptor> clazz = FileDescriptor.class;

    Constructor<FileDescriptor> c;
    try {
        c = clazz.getDeclaredConstructor(new Class[] { Integer.TYPE });
    } catch (SecurityException e) {
        e.printStackTrace();
        return;
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
        return;
    }

    c.setAccessible(true);
    FileDescriptor fd;
    try {
        fd = c.newInstance(new Integer(socket));
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return;
    } catch (InstantiationException e) {
        e.printStackTrace();
        return;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return;
    } catch (InvocationTargetException e) {
        e.printStackTrace();
        return;
    }

    FileOutputStream os = new FileOutputStream(fd);
            FileInputStream is = new FileInputStream(fd);
Saddlecloth answered 7/8, 2009 at 12:30 Comment(1)
It's going to take me a little while to hook this up to the FCGI dev kit's message parsing library, but this looks like the way to go. Thanks.Seabee
S
1

What would you want a Socket for? The only useful methods in a socket are getInputStream and getOutputStream but if you’re running as a CGI you already have those: they’re called System.in and System.out. :)

Sic answered 7/8, 2009 at 8:25 Comment(4)
Because that isn't how FastCGI works, which the OP knew, which is why he asked the question he did.Kent
Any documentation I can find tells me that communication is done with System.in and System.out so my original question still stands.Sic
(I’m talking about the documentation at fastcgi.com/devkit/doc/fcgi-java.htm which looks quite “official” to me.)Sic
@Sic - good idea, but that relies on starting up the Java program as an "external host" and connecting via host:port, which my hosting provider doesn't support.Seabee
S
1

You can't do this legally. However, you can do a hack like this (Don't try this at home). You can read from is and write to os.

    Class<FileDescriptor> clazz = FileDescriptor.class;

    Constructor<FileDescriptor> c;
    try {
        c = clazz.getDeclaredConstructor(new Class[] { Integer.TYPE });
    } catch (SecurityException e) {
        e.printStackTrace();
        return;
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
        return;
    }

    c.setAccessible(true);
    FileDescriptor fd;
    try {
        fd = c.newInstance(new Integer(socket));
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return;
    } catch (InstantiationException e) {
        e.printStackTrace();
        return;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return;
    } catch (InvocationTargetException e) {
        e.printStackTrace();
        return;
    }

    FileOutputStream os = new FileOutputStream(fd);
            FileInputStream is = new FileInputStream(fd);
Saddlecloth answered 7/8, 2009 at 12:30 Comment(1)
It's going to take me a little while to hook this up to the FCGI dev kit's message parsing library, but this looks like the way to go. Thanks.Seabee

© 2022 - 2024 — McMap. All rights reserved.