一些java Datagram Socket问题 [英] Some java Datagram Socket questions

查看:182
本文介绍了一些java Datagram Socket问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近已经厌倦了使用java进行套接字编程,我有一些常见的问题。

I have recently nose dived into socket programming using java, and I have a few general sort of questions.

有一个bind()方法,以及一个connect()和disconnect()。没有unbind()。这是因为在断开套接字时是未绑定的吗?一旦程序退出,垃圾收集是否会处理这个问题?或者这甚至不是一个有效的问题?

There is a bind() method, as well as a connect() and disconnect(). There is no unbind(). Is this because upon disconnecting the socket is unbound? Does garbage collection take care of this once the program exits? Or is this not even a valid question?

此外,在创建DatagramSocket时,如果我只提供端口和地址,它有何不同?我正在创建一个程序来从网络上收集数据,因为数据会浮动并记录下来。我应该只使用本地地址吗?我创建套接字时无法使用地址导致我无法收集数据包?

Also, upon creating a DatagramSocket, how is it different if I only provide the port and the address? I am creating a program to collect data off a network, as the data floats around and log it. Should I just use the local address? Could not using the address when I create the socket cause me to not be able to collect packets?

我只是想更深入地了解内部工作原理这些东西。

I am just trying to get a stronger understanding on the inner-workings of these things.

推荐答案

那里有大约15个独立的问题,但我会尽力解决这些问题:

There are about 15 independent questions in there, but I'll do my best to address them:


有一个bind()方法,以及一个connect()和disconnect()。没有unbind()。这是因为在断开套接字时是未绑定的吗?

There is a bind() method, as well as a connect() and disconnect(). There is no unbind(). Is this because upon disconnecting the socket is unbound?

bind()是与 connect() disconnect()分开。 Bind用于将套接字绑定到特定端口 - 有效地监听连接,而 connect()用于打开已经侦听的套接字的连接在特定的港口。相当于 unbind() close()

bind() is separate from connect() and disconnect(). Bind is used to bind a socket to a particular port -- effectively to "listen" for connections whereas connect() is used to open a connection to a socket that is already listening on a particular port. The equivalent of unbind() is close()


一旦程序退出,垃圾收集是否会处理这个问题?或者这甚至不是一个有效的问题?

Does garbage collection take care of this once the program exits? Or is this not even a valid question?

这是一个完全有效的问题,尽管垃圾收集是一种用于内存管理的技术,不是socket / OS资源管理。如果您没有释放特定端口,它将与您的应用程序保持关联,直到您的应用程序终止,然后由操作系统回收它。这是操作系统级功能,而不是JVM功能等。

This is a totally valid question, although garbage collection is a technology used for memory management, not socket/OS resource management. If you don't release a particular port, it will remain associated with your application until your application terminates and it will then be reclaimed by the OS. This is OS-level functionality, not JVM functionality, etc.


此外,在创建DatagramSocket时,如果我只提供它,它有何不同端口或提供端口和地址?

Also, upon creating a DatagramSocket, how is it different if I only provide the port or provide the port and the address?

在某些时候,你必须提供互联网地址和端口或套接字你希望连接或绑定到。没有办法绕过它。

At some point, you have to provide the internet address and port or the socket you wish to connect to or to bind to. There's no way around it.


我正在创建一个程序来从网络上收集数据,因为数据会浮动并记录下来。我应该只使用本地地址吗?我创建套接字时无法使用该地址导致我无法收集数据包?

I am creating a program to collect data off a network, as the data floats around and log it. Should I just use the local address? Could not using the address when I create the socket cause me to not be able to collect packets?

我不确定你是什么在这里问,你是在谈论在网络上记录所有数据包,又称嗅探器?这将需要的不仅仅是简单的数据报编程。实际上,您必须在网络适配器级别注入自己,以便在数据包读取时拦截数据包。您所谈论的内容只允许您接收发送到您正在收听的特定端口的数据包。

I'm not sure what you're asking here, are you talking about logging all packets on the network, aka a sniffer? That's going to require more than simple datagram programming. You actually have to inject yourself at the network-adapter level to intercept packets as they are read off the line. What you're talking about will only allow you to receive packets that are sent to the specific port you're listening to.

这篇关于一些java Datagram Socket问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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