我試圖在Java中與Linux tun驅動程序接口,因爲它在這裏解釋。帶有JNI的ioctl():破損的文件描述符
How to interface with the Linux tun driver
但因爲你不能調用的ioctl()用java,我現在用的是Java本地接口。只要我不在相同的文件中讀寫,它工作的很好。
如果我這樣做,我得到這個例外,我會通過翻譯「的FileDescriptor處於斷開狀態」:
java.io.IOException: Le descripteur du fichier est dans un mauvais état
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:326)
at WriterThread.main(WriterThread.java:54)
這裏是Java代碼:
public static void main(String[] arg){
File tunFile = new File("/dev/net/tun");
FileOutputStream outStream;
FileInputStream inStream;
try {
inStream = new FileInputStream(tunFile);
outStream = new FileOutputStream(tunFile);
FileDescriptor fd = inStream.getFD();
//getting the file descriptor
Field f = fd.getClass().getDeclaredField("fd");
f.setAccessible(true);
int descriptor = f.getInt(fd);
//use of Java Native Interface
new TestOuvertureFichier().ioctl(descriptor);
while(true){
System.out.println("reading");
byte[] bytes = new byte[500];
int l = 0;
l = inStream.read(bytes);
//the problem seems to come from here
outStream.write(bytes,0,l);
}
} catch (Exception e) {
e.printStackTrace();
}
}
這裏C代碼:
JNIEXPORT void JNICALL Java_TestOuvertureFichier_ioctl(JNIEnv *env,jobject obj, jint descriptor){
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN;
strncpy(ifr.ifr_name, "tun0", IFNAMSIZ);
int err;
if ((err = ioctl(descriptor, TUNSETIFF, (void *) &ifr)) == -1) {
perror("ioctl TUNSETIFF");exit(1);
}
return;
}
'新FileOutputStream(...)'一定會嘗試創建一個新文件。嘗試使用* one *'RandomAccessFile'而不是兩個文件流。 – EJP