-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FileDescriptorCast: Allow casting AF_INET/AF_INET6 socket file descriptors to Socket API classes #151
Comments
Thanks for reporting, @xueqiwang0v0! I think it's due to the fact that it's likely an AF_INET or AF_INET6 socket (you mention TCP), which junixsocket currently doesn't provide specific support for. In that sense, junixsocket "works as expected", but obviously it's not working for you. I see three possible solutions:
I can see the utility for ensuring compatibility with apps expecting a Socket instance, so option 3 may happen eventually. A workaround that works right now would be to "cast" the Any other ideas? |
Thanks for your reply, @kohlschuetter ! Sorry for my misusage about junixsocket library. It offers great help to my project! So many thanks again! I'm currently working on option 1. I achieved rebuilding Socket using reflection, while ServerSocket rebuild is still unsolved. For option 3, will there be a rough plan? |
There's no plan yet for option 3, but it may eventually arrive. I will keep this ticket open for now to be reminded of it. Contributions are of course welcome! |
When passing socket file descriptors from other processes, provide a fallback to allow communicating with otherwise unsupported socket types. This allows using FileDescriptorCast to Socket. Previously, only ByteChannel/InputStream/OutputStream was supported. Note that the socket addresses returned are not guaranteed to be compatible across operating systems. #151
@xueqiwang0v0 Make sure the following section is in your POM:
Then run If you use Gradle, please specify the following section in your
Then run |
@kohlschuetter Thanks for your help!
|
@xueqiwang0v0 Regarding case 2, I think you found another bug that we should fix. I think that calling configureBlocking(true) is currently not working as expected since there is some Java Socket code that ignores that call if the Java base class "thinks" it's blocking already. Please try adding
right after casting the socket, and before calling |
@kohlschuetter It works! Thanks a lot! |
Previously, casting a FileDescriptor to Socket would not reset its (cached) blocking state to "blocking" if it was configured non-blocking, which may lead to unexpected behavior. Check the "blocking" state upon casting and set it accordingly when a Socket/DatagramSocket/ServerSocket is required (since they are expected to be blocking by default). When casting to a SocketChannel/DatagramChannel/ServerSocketChannel, adjust the Java-cached "blocking" state to the actual blocking state of the native socket. Moreover, add support to declare "we don't know the state" (like in Windows, which is currently not supported for casting, though), and ensure we get the cached state and the native state back in sync. #151
@xueqiwang0v0 Please re-test with the latest snapshot ( |
@gamlerhart FYI, this could be relevant for you too, given your previous experiments. |
@kohlschuetter The new version works well. I removed |
@kohlschuetter Thanks for the notification. I've added a update note to the old blog post, for future visitor. |
Fixed in 2.9.0, closing. |
Describe the bug
More tutorials on how to cast fd to socket. I got ClassCastException when casting. Are there any other restrictions?
To Reproduce
Steps to reproduce the behavior:
platform: Mac Arm MacOS 13.5.1
env: Java11
lib:
I‘m trying to transfer a tcp connection between two processes. To do this, I create a tcp socket in process1 and send the file descriptor of the tcp socket to process2. Then I try to rebuild the tcp socket in process2. I used
FileDescriptorCast.using(fd).as(Socket.class)
and it throws ClassCastException.I notice that there are only few global types are available for this fd, am I missing something? I follow the instruction here: https://kohlschutter.github.io/junixsocket/filedescriptors.html
The available types I get: [class java.lang.Number, class java.io.OutputStream, interface java.lang.Comparable, class java.io.FileInputStream, interface java.nio.channels.Channel, interface java.nio.channels.InterruptibleChannel, interface java.io.Serializable, interface java.io.Closeable, interface java.io.Flushable, interface java.nio.channels.ScatteringByteChannel, class java.lang.Integer, class java.io.FileDescriptor, interface java.lang.AutoCloseable, class java.lang.ProcessBuilder$Redirect, interface java.nio.channels.GatheringByteChannel, interface java.nio.channels.ReadableByteChannel, class java.nio.channels.spi.AbstractInterruptibleChannel, class java.lang.Object, class java.nio.channels.FileChannel, interface java.nio.channels.ByteChannel, class java.io.InputStream, interface java.nio.channels.WritableByteChannel, class java.io.FileOutputStream, interface java.nio.channels.SeekableByteChannel]
Expected behavior
FileDescriptorCast successfully cast the fd to socket.
Output/Screenshots
When I called the
FileDescriptorCast.using(fd).as(Socket.class);
, I gotAny help would be appreciated!
The text was updated successfully, but these errors were encountered: