java rawsocket,做RAW socket编程涉及Java最好的方法

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);

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.

I have observed the following:

If I write a C program and call that from Java using Runtime the forked code may open the socket.

If I run this native code from Eclipse it also runs nicely. I guess this comes from the fact that Eclipse has been started from eclipse.exe, and not from java.exe.

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?

Edit:

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

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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值