Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I do low level network programming in Java?

An application level message is send over the network in a series of packets that are assembled in the receiving side and passed to the application level.
Is it possible in Java to do network programming in the level of these individual packets?
Or in Java we can only see the "application" level packet? I.e. the "big-packet" that is assembled by all these network packets?
I tried to research on google for this matter but the results where really confusing.
The confusion is due to the fact that some resources that are about UDP seem to indicate that the operation is on packets, while others say that Java can not work in raw sockets which implies that it works on a higher level of abstraction.I could not find an answer to exactly what I am looking for. If yes, which package does this?

like image 780
Jim Avatar asked Jul 19 '13 20:07

Jim


People also ask

Can java be used for network programming?

Java certainly fills well over 90% of most network programmers' needs. Once a program has connected to a server, the local program must understand the protocol the remote server speaks and properly interpret the data the server sends back.

Why is java good for network programming?

Java programs are flexible because Java is a fully general programming language, unlike HTML. Java programs see network connections as streams of data, which can be interpreted and responded to in any way that's necessary. Web browsers see only certain kinds of data streams and can interpret them only in certain ways.

Which java package is used for networking?

As stated earlier, the java.net package of the Java programming language includes various classes and interfaces that provide an easy-to-use means to access network resources. Other than classes and interfaces, the java.net package also provides support for the two well-known network protocols.

What is networking basics in java?

Java Networking is a notion of connecting two or more computing devices together to share the resources. Java program communicates over the network at the application layer. java.net package is useful for all the Java networking classes and interfaces. The java.net package provides support for two protocols.


1 Answers

Is it possible in Java to do network programming in the level of these individual packets?

Yes, but it's very unlikely you would want individual packets.

Or in Java we can only see the "application" level packet?

Pure Java can only see TCP streams, and UDP datagram which have a one-to-one mapping with packets, but you have no access to the UDP header.

I.e. the "big-packet" that is assembled by all these network packets?

You don't get packets at all big or small. You read data and the data available is read (up to the size of your buffer)

If yes, which package does this?

You can use JPcap to see individual packets, however, this is rarely useful unless you need accurate time stamping of each packet or you need to trace dropped packets.

This uses winpcap (Windows) or libpcap (linux) via JNI.

In most of these cases where I have seen this used it was a lot of work for little gain.

from my point of view an answer mentioning JNI means that Java does not support it (since you have to actually code in another language for what you need)

Sockets, Files, GUI components all use JNI in the end. By this definition you can't do anything which uses a system call in Java, because the OS is not written in Java.

I don't think this is a useful definition of what you can do in Java.

1) Pure Java can only see TCP streams. What about UDP?

You don't have access to the packet's header with any protocol in Java without libPCap.

I assume this point means no packet access

Not without any additional libraries.

2) In most of these cases where I have seen this used it was a lot of work ? Why.

Because it is very low level and lots of details you don't normally have to worry about are exposed to you. Note: you might not get a packet as they can be dropped while attempting to record them and you won't be able to ask for them again so you miss them.

It is just a library right?

Correct.

Doesn't it work?

Why do you say that?

I am trying to see if what I need to do can be done in Java or should look into other languages.

IMHO, You won't find it any easier in another language.

I read in the jpcap docs that it can not reshape traffic e.g. drop packets etc. Why can't it do that?

You can't force a network to drop a packet and you can't trick the kernel in to drop one either. If you think about what a dropped packet is, the answer is fairly obvious.

like image 157
Peter Lawrey Avatar answered Sep 18 '22 09:09

Peter Lawrey