我可以用Java进行底层网络编程吗? [英] Can I do low level network programming in Java?

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

问题描述

应用程序级别的消息以一系列数据包的形式通过网络发送,这些数据包在接收方组装并传递到应用程序级别.
Java是否可以在这些单个数据包的级别上进行网络编程?
还是在Java中我们只能看到应用程序"级别的数据包? IE.所有这些网络数据包组装而成的大数据包"?
我曾尝试在Google上对此事进行研究,但结果确实令人困惑.
造成这种混乱的原因是,一些与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?

纯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)

如果是,请问哪个程序包?

If yes, which package does this?

您可以使用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

并非没有任何其他库.

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?

正确.

不行吗?

Doesn't it work?

你为什么这么说?

我试图查看我需要做的事情是否可以用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进行底层网络编程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆