简体   繁体   English

如何在Java中实例化Socket类?

[英]How to instantiate a Socket class in Java?

I know that the Socket class can be instantiated in this way (as an example): 我知道可以以这种方式实例化Socket类(例如):

new Socket("taranis", 7);

where "taranis" is the name of the server in a local network. 其中“ taranis”是本地网络中服务器的名称。 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? 1.如果我的计算机不是局域网,而是连接到Internet,我可以使用计算机的IP地址来实例化套接字吗?


2. How my computers get names in the local network? 2.我的计算机如何在本地网络中获取名称? Do I give names to the computers when I create the network. 创建网络时,是否给计算机命名?

PS I herd that computers can establish a network by themself (using zeroconf). PS我认为计算机可以自己建立网络(使用zeroconf)。 Who then gives names to the computers and how can I know these names in advance (I need them to write my code). 谁给计算机命名,以及我如何事先知道这些名称(我需要它们来编写代码)。

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 . 你会做的更好,以TCP / IP上读到例如这里

  1. Yes. 是。 But I doubt that that your computers do not form a local network (LAN). 但是我怀疑您的计算机没有形成局域网(LAN)。 In case they do, you can use their LAN IP address. 如果出现这种情况,您可以使用其LAN IP地址。 To find the ipaddress, you can use "ipconfig" command on the "Command Prompt" on Windows, and "ifconfig" on unix. 要找到ipaddress,可以在Windows的“命令提示符”上使用“ ipconfig”命令,在Unix上使用“ ifconfig”。 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)). 对于编程,通常将使用ipaddress(当名称可以动态分配给其中一台计算机(使用动态DNS)时,请使用name)。 IP Address may also be (and is quite commonly) dynamically assigned using a DHCP server. IP地址也可以(通常是)使用DHCP服务器动态分配。

  1. Yes, you can create a socket using the ip address, you can do so like this: new Socket("192.168.1.00",8888) 是的,您可以使用IP地址创建一个套接字,您可以像这样: 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 . 一种是使用DNS服务器 This kind of like a middle man which will translate the name (ie Computer1) to its IP address (192.1.168.1.100). 这就像一个中间人,它将名称(即Computer1)转换为其IP地址(192.1.168.1.100)。 You can also keep these mapping locally, you can put this in the hosts file. 您也可以将这些映射保留在本地,也可以将其放在hosts文件中。 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. 这是名称和ip地址之间的映射,如果使用此方法,则需要确保这些主机文件在计算机之间对齐。

  3. Zeroconf is an interesting protocol. Zeroconf是一个有趣的协议。 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. 发现服务后,连接的客户端可以查询要连接的IP和端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM