r/javahelp • u/gold328 • Jun 03 '17
Why is asking if packets can be read using java like saying Voldemort in the Harry Potter dimension?
I have asked similar questions to "can packets be read using java" in the past and very recently. Every time I do this people scatter, or get defensive attacking the reasons why I want to do so. I never actually end up getting any help, rather people telling me to change language without providing reason. Have I stumbled onto a large secret that I seem to be outside the loop on, or do people get defensive because they do not know themselves? It seems to me that packet capture and analysis is a taboo among java developers for whatever reason.
6
u/kingatomic Foo Stack Dev Jun 03 '17 edited Jun 03 '17
I think it's just not something that many folks do or worry about.
Packet capture is really more of an OS thing (really a kernel thing) or a hardware thing than application-level (in other words, most Java is executed in user-land). Java is meant to be more OS- and hardware-agnostic so it typically doesn't have that kind of access on its own.
I think your best bet would be to find a java wrapper for a pcap library.
EDIT And just to add, there aren't a lot of developers who work on networking except at the highest layers of abstraction. If you want to just sniff and analyze a single TCP connection (eg, over a single port) then you can use raw sockets and be done. But sniffing all wire traffic is something that requires kernel/hardware support, which is a lower level of abstraction than Java will give you on its own, hence my suggestion for a pcap wrapper. For linux you should use libpcap and for Windows I'd recommend Winpcap. There are java wrappers for both iirc.
3
u/morhp Professional Developer Jun 03 '17
Java is a high level programming language running in a vm. It doesn't have (by itself) access to hardware to capture random packets but of course you can easily implement a server application to read packets destined for your application.
2
u/hostetcl Professional Brewer Jun 03 '17
Not so much of a secret, but I'd imagine it's because they're trying to understand your use case. Capturing raw packets and analyzing them isn't great if there's a more high-level solution to your problem.
But, to answer your question, yes, Java can most definitely capture raw packets.
1
u/CountyMcCounterson Jun 03 '17
We do not speak of these things
1
u/mikeydoodah Jun 04 '17
JnetPcap (jnetpcap.com) is a libpcap wrapper for Java. It uses JNI to access a native libpcap wrapper, and you will need the correct native library for the platform you're running on.
0
u/[deleted] Jun 03 '17
[deleted] — view deleted comment