简体   繁体   English

服务器和客户端之间的套接字 object 不同

[英]Socket object is not same between server and client

Are there any ways to check if the same Socket is used between the server-side and client-side?有什么方法可以检查服务器端和客户端之间是否使用了相同的 Socket?

  • What I did.我做了什么。
  1. Create DateServer.java and DateClient.java创建 DateServer.java 和 DateClient.java
  2. Run DateServer.java运行 DateServer.java
  3. Run DateClient.java运行 DateClient.java
  4. Check both hashCode on the console.检查控制台上的两个 hashCode。 The hashCode is different. hashCode 不同。 I thought it was the same.我以为是一样的。
  • DateServer.java日期服务器.java
package net;
    
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class DateServer {
    
    public static final int LISTENING_PORT = 32007;
    
    public static void main(String[] args) {
        ServerSocket listener;
        Socket connection;
        try {
            listener = new ServerSocket(LISTENING_PORT);
    
            while (true) {
                connection = listener.accept();
                System.out.println(connection.hashCode());
                
                sendDate(connection);
            }
        } catch (Exception e) {
            return;
        }
    }
    
    private static void sendDate(Socket client) {
        try {
            Date now = new Date();
            PrintWriter outgoing = new PrintWriter(client.getOutputStream());
            outgoing.println(now.toString());
            outgoing.flush();
            client.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
  • DateClient.java DateClient.java
package net;
    
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
    
public class DateClient {
    
    public static final int LISTENING_PORT = 32007;
    
    public static void main(String[] args) {
    
        String hostName = "localhost";
        Socket connection;
        BufferedReader incoming;
    
        try {
            connection = new Socket(hostName, LISTENING_PORT);
            System.out.println(connection.hashCode());
                
            incoming = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String lineFromServer = incoming.readLine();
            System.out.println();
            System.out.println(lineFromServer);
            System.out.println();
            incoming.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
  • Output on server side.服务器端的 Output。

    1804094807 1804094807

  • Output on client side. Output 在客户端。

    1826771953 1826771953

    Sun Mar 07 18:16:05 JST 2021 2021 年 3 月 7 日星期日 18:16:05 JST

The hashCode is different. hashCode 不同。 I thought it was the same我以为是一样的

This assumption is wrong.这个假设是错误的。 And I don't know where it comes from.而且我不知道它来自哪里。 But hashCode just returns a local identifier associated with a local object.但是hashCode只返回与本地object 关联的本地标识符。

As already stated by Steffen, the hashcodes of the server-side and client-side sockets is just based on the memory address of the two objects in two different JVMs.正如 Steffen 所说,服务器端和客户端 sockets 的哈希码仅基于两个不同 JVM 中两个对象的 memory 地址。

What will be equal is: equal是:

  • connection.getLocalSocketAddress() of the client客户端的connection.getLocalSocketAddress()
  • connection.getRemoteSocketAddres() of the server服务器的connection.getRemoteSocketAddres()

and viceversa.反之亦然。 That includes the result of the hashCode() and toString() methods, since InetSockeAddress overrides all three equals , hashCode and toString methods.这包括hashCode()toString()方法的结果,因为InetSockeAddress覆盖了所有三个equalshashCodetoString方法。

It depends on hashcode implementation, as you can see in case of Socket the hashcode is unique to the instance that was created within that process.它取决于哈希码的实现,正如您在 Socket 的情况下看到的那样,哈希码对于在该进程中创建的实例是唯一的。

Please take a look on this example请看一下这个例子

Hashcode is the same only because It is driven by state of Person, and we had to implement It. Hashcode之所以相同,只是因为它是由Person的state驱动的,我们必须实现它。 However the identity hashcode of person objects differ.然而,人对象的身份哈希码不同。

public static void main(String[] args) {
    class Person {
        private int age;
        private int cash;

        public Person(int age, int cash) {
            this.age = age;
            this.cash = cash;
        }

        @Override
        public int hashCode() {
            return (31 * age) + cash;
        }

        public int defaultHashCodeImplementation() {
            return super.hashCode();
        }
    }

    Person p1 = new Person(30, 100);
    System.out.println("p1 hashcode: " + p1.hashCode());
    System.out.println("p1 identity hashcode: " + p1.defaultHashCodeImplementation());

    Person p2 = new Person(30, 100);
    System.out.println("p2 hashcode: " + p2.hashCode());
    System.out.println("p2 identity hashcode: " + p2.defaultHashCodeImplementation());
}

Output Output

p1 hashcode: 1030
p1 object identity hashcode: 1712669532
p2 hashcode: 1030
p2 object identity hashcode: 1225373914

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

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