Can I do low level network programming in Java?

前端 未结 3 686
半阙折子戏
半阙折子戏 2021-02-09 20:32

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

3条回答
  •  遥遥无期
    2021-02-09 21:21

    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.

提交回复
热议问题