0

我对服务器和客户端定义它们之间的通信标准的方式有疑问。作为参考,我使用的是 Java 和这里引用的代码。

  1. 客户端需要服务器的 IP 才能连接到它。如果服务器使用该方法InetAddress.getLocalHost().getHostAddress(),则返回的 IP 地址将是本地的:如果客户端与服务器在同一台机器上,则很有用,否则就不太有用了。如何获取客户端需要的服务器 IP?是否可以在不使用 ping 您的服务器并返回您的公共 IP 的外部服务或站点的情况下获取此信息?
  2. 假设我,客户端,已经Socket与服务器建立了通信。现在我需要打开输入和输出流,它必须与服务器端的流兼容。我怎样才能提前知道正确的种类?例如,让我们考虑time.nist.gov端口 13 的情况。我希望与该服务器的通信输出是String封装当前日期。String服务器是通过一个ObjectOutputStream还是通过一个来传输所说的OutputStreamWriter?我怎么知道?
4

1 回答 1

2

The client needs the server's IP to connect to it... How can I get the server IP that the client needs? Is it possible to acquire this information without using external services or sites that ping your server and return your public IP?

The client either must know either the IP address or the host name of the server. If the host name is known, the IP address can be figured out on the client's side using InetAddress.getByName( "java.sun.com" ), using java.sun.com as an example.

I need to open the input and output stream, which must be of a kind compatible with the stream on the server side. How can I know in advance the correct kind?

Since the messaging varies from protocol to protocol, the client has to know what the server supports and write its input and output streams accordingly. Usually, client-server systems communicate in bytes-based input and output. That is, plain bytes data will be exchanged between the client and the server. They then convert these bytes from/to characters where needed based on the agreed upon character encoding, like UTF-8.

Does the server transmit said String through an ObjectOutputStream, or maybe an OutputStreamWriter?

Here is where the idea of a protocol needs understanding. You may already know, however, I am writing it just to explain clearly. A protocol is an agreement or contract of the messaging style - how many bytes to send, when to send, how to encode, etc. Eg, in SMTP the client has to first say HELO or EHLO, upon receiving which the server returns a specific response. The client must know what to expect at this stage and how to move further based on the protocol.

Usage of ObjectInputStream and ObjectOutputStream

Now, ObjectInputStream and ObjectOutputStream is a Java-specific wrapper over IO streams to read and write byte buffers in the "shape" of Java objects. That is, these classes serve only Java-object-specific purposes. This is fine if it known that the server is communicate using serialized/deserialized Java objects. However, the server the client is connecting may not be using Java at all! Or even if it is, it may be communicating in pure characters or bytes and not in Java objects.

I am not sure what protocol time.nist.gov:13 is using. You will need to understand that and code your client accordingly. However, since you have mentioned that it returns a string:

  1. Simply use socket.getInputStream() on the client, without wrapping it with ObjectInputStream
  2. Read the bytes from this stream till EOF
  3. Convert these bytes into a String considering the character encoding published by the site, say, UTF-8. new String( bytes, StandardCharsets.UTF_8 ).
于 2020-01-30T12:41:06.737 回答