我可以用 Java 进行低级网络编程吗?

Can I do low level network programming in Java?(我可以用 Java 进行低级网络编程吗?)

本文介绍了我可以用 Java 进行低级网络编程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序级别的消息通过网络以一系列数据包的形式发送,这些数据包在接收端组装并传递到应用程序级别.
Java 是否可以在这些单独的数据包级别进行网络编程?
还是在Java中我们只能看到应用"级别的数据包?IE.由所有这些网络数据包组装而成的大数据包"?
我试图在谷歌上研究这个问题,但结果真的很混乱.
混淆是由于一些关于 UDP 的资源似乎表明该操作是在数据包上进行的,而其他人则说 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?

推荐答案

在 Java 中是否可以在这些单独的数据包级别进行网络编程?

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.

或者在Java中我们只能看到应用程序"级别的数据包?

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

Pure Java 只能看到 TCP 流和与数据包一对一映射的 UDP 数据报,但您无法访问 UDP 标头.

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)

如果是,这是哪个包?

您可以使用 JPcap 查看单个数据包,但是,除非您需要每个数据包的准确时间戳或需要跟踪丢弃的数据包,否则这很少有用.

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.

这通过 JNI 使用 winpcap (Windows) 或 libpcap (linux).

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.

从我的角度来看,提到 JNI 的答案意味着 Java 不支持它(因为您必须实际使用另一种语言进行编码才能满足您的需要)

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)

套接字、文件、GUI 组件最终都使用 JNI.根据这个定义,你不能做任何在 Java 中使用系统调用的事情,因为操作系统不是用 Java 编写的.

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.

我不认为这是对您在 Java 中可以做什么的有用定义.

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

1) 纯 Java 只能看到 TCP 流.那么UDP呢?

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

如果没有 libPCap,您将无法使用 Java 中的任何协议访问数据包的标头.

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) 在我见过的大多数情况下,这需要大量工作吗?为什么.

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?

正确.

它不工作吗?

为什么这么说?

我正在尝试看看我需要做的事情是否可以用 Java 完成,或者应该研究其他语言.

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.

我在 jpcap 文档中读到它不能重塑流量,例如丢弃数据包等.为什么不能这样做?

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.

这篇关于我可以用 Java 进行低级网络编程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:我可以用 Java 进行低级网络编程吗?

基础教程推荐