如何在Java中实例化一个Socket类?

发布于 2024-08-23 02:14:04 字数 324 浏览 9 评论 0原文

我知道 Socket 类可以通过这种方式实例化(作为示例):

new Socket("taranis", 7);

其中“taranis”是本地网络中服务器的名称。对此我有两个问题:


1.如果我的计算机没有形成本地网络,但连接到 Internet,我可以使用计算机的 IP 地址来实例化套接字吗?


2。我的计算机如何获取本地网络中的名称?创建网络时是否为计算机命名?

PS我认为计算机可以自己建立网络(使用zeroconf)。然后谁给计算机命名,我如何提前知道这些名称(我需要它们来编写我的代码)。

I know that the Socket class can be instantiated in this way (as an example):

new Socket("taranis", 7);

where "taranis" is the name of the server in a local network. In this respect I have two questions:

1. If my computers do not form a local network but are connected to the Internet, can I use the IP addresses of computers to instantiate a socket?

2. How my computers get names in the local network? Do I give names to the computers when I create the network.

P.S. I herd that computers can establish a network by themself (using zeroconf). Who then gives names to the computers and how can I know these names in advance (I need them to write my code).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花坊 2024-08-30 02:14:04
  1. 是的,您可以使用IP地址创建一个套接字,您可以这样做: new Socket("192.168.1.00",8888)

  2. 当您在计算机上安装操作系统时,通常会执行以下步骤之一:操作系统让你经历的是给你的机器一个名字。每个操作系统还可以在安装后更改这些名称。因此,您的每台计算机可能都有一个名称。然而,棘手的部分是让一台机器知道另一台机器的名称。这可以通过几种方式来完成。一种是使用 DNS 服务器。这有点像一个中间人,它将名称(即 Computer1)转换为其 IP 地址(192.1.168.1.100)。您还可以将这些映射保留在本地,您可以将其放入 hosts 文件中。这是名称和 IP 地址之间的映射,如果使用此方法,则需要确保这些主机文件在计算机上对齐。

  3. Zeroconf 是一个有趣的协议。它的工作方式是一台计算机创建一个命名服务,第二台计算机只需按名称查找该服务,一旦找到该服务就可以连接到该服务。当服务被发现时,连接客户端可以查询要连接的 IP 和端口。

  1. Yes, you can create a socket using the ip address, you can do so like this: new Socket("192.168.1.00",8888)

  2. When you install an OS on your computer, usually one of the steps that the OS makes you go through is giving your machine a name. Each OS also has a way of changing these names after installation. So, everyone of your computers probably does have a name. However, the tricky part is getting one machine to know the name of the other. This can be done in a few ways. One is by using a DNS server. This kind of like a middle man which will translate the name (i.e Computer1) to its IP address (192.1.168.1.100). You can also keep these mapping locally, you can put this in the hosts file. This is a mapping between names and ip addresses, and if you use this method, you need to make sure that these hosts files line up across the machines.

  3. Zeroconf is an interesting protocol. The way it works is one computer creates a named service and the second computer just looks up the service by name, and once it finds the service it can connect to it. When the service is discovered, the connecting client and can query for the ip and port to connect to.

妄断弥空 2024-08-30 02:14:04

在进行套接字编程之前,您需要一些网络背景知识。不幸的是,您提出的问题并不容易回答,因为这取决于您的具体网络配置。以下是一些简短的答案,但由于依赖于特定配置,可能不正确。您最好阅读此处有关 TCP/IP 的内容。

  1. 是的。但我怀疑您的计算机没有形成本地网络(LAN)。如果他们这样做,您可以使用他们的 LAN IP 地址。要查找 IP 地址,您可以在 Windows 上使用“命令提示符”上的“ipconfig”命令,在 unix 上使用“ifconfig”。输出是计算机中每个网络接口的配置。是

  2. 是的,您可以在计算机上配置每台计算机的名称。

对于编程,通常您会使用 ipaddress(当名称可以动态分配给其中一台计算机时使用名称(使用动态 DNS))。 IP 地址也可以(并且很常见)使用 DHCP 服务器动态分配。

Before socket programming, you need some background on networking. Unluckily the questions you ask are not simple to answer as it depends on the specific network configuration you have. Here are some short answers, but may be incorrect due to the dependency on the particular configuration. You will do better to read up on TCP/IP for example here.

  1. Yes. But I doubt that that your computers do not form a local network (LAN). In case they do, you can use their LAN IP address. To find the ipaddress, you can use "ipconfig" command on the "Command Prompt" on Windows, and "ifconfig" on unix. The output is the configuration of each network interface in the computer.

  2. Yes, you can configure the name of each computer on the computer.

For programming, usually you would use ipaddress (use name when the name can be dynamically assigned to one of the computers (using a Dynamic DNS)). IP Address may also be (and is quite commonly) dynamically assigned using a DHCP server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文