进行涉及 Java 的 RAW 套接字编程的最佳方法 [英] Best way to do RAW socket programming involving Java

查看:40
本文介绍了进行涉及 Java 的 RAW 套接字编程的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些现有的 C 代码使用 ICMP 原始套接字执行 Ping,我需要在 Java 中使用此代码,因为 Java 不支持 ICMP 包.当我将代码包装在JNI中并调用它时,C代码在执行时无法获取套接字:

I have some existing C code that uses ICMP raw sockets to do Ping and I need to use this code in Java as Java does not support ICMP packages. When I wrap the code in JNI and call it, the C code fails to obtain the socket when doing:

socket(AF_INET, SOCK_RAW, 1);

socket(AF_INET, SOCK_RAW, 1);

我猜 Java 已经放弃了一些特权,这些特权会禁止在 Java 进程中使用原始套接字.这个我必须找到解决办法.

I guess that Java has dropped some privileges that disables the use of raw socket from the Java process. This I must find a solution to.

我观察到以下情况:

  • 如果我编写一个 C 程序并使用运行时从 Java 调用它,则分叉的代码可能会打开套接字.
  • 如果我从 Eclipse 运行此本机代码,它也运行良好.我猜这是因为 Eclipse 是从 eclipse.exe 启动的,而不是从 java.exe 启动的.

这意味着我可以通过选择这两种策略之一来解决我的问题,但我都不喜欢.有没有其他方法可以让 Java 接受 JNI 代码正在打开这个套接字?

This means that I could solve my problem by choosing one of these two strategies, but I like neither. Are there any other ways that I could get Java to accept that the JNI code is opening this socket?

我对这个问题的研究越多,我认为它一定是与 Java 启动方式有关的 Windows 7 相关问题.

The more I look into this problem I figure that it must be a windows 7 related issue with how Java is being started.

如果您让 Windows 运行,或者如果您在 Windows 以外的其他平台上,则似乎也可以使用 InetAddress.isReachable() 方法.

It also appears that if you get windows to behave or if you are on some other platform than Windows the method InetAddress.isReachable() could be used as well.

推荐答案

也许使用现有的 Java 原生套接字库?这样您就不必担心编写 JNI 库了.

Maybe use an existing Java native socket lib? Then you don't have to worry about coding the JNI lib.

参见:http://www.savarese.com/software/rocksaw/

这篇关于进行涉及 Java 的 RAW 套接字编程的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆