Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

c - Best way to do RAW socket programming involving 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

See: http://www.savarese.com/software/rocksaw/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...