The getLocalPort() method returns the port to which the socket binds to. Whereas the getPort() method returns the remote port to which the socket connects to.
For example
ServerSocket serverSocket = new ServerSocket(6045, 50, <InetAddress>);
You create a Socket using the serverSocket's InetAddress and port number to connect to it. You may use this socket to read and write or you can also use the socket returned by serverSocket.accept() to read and write. However, they have different local and remote ports.
socket.getPort() will return the port number used in the above line which is 6045. socket.getLocalPort will return something else, lets say 53222.
To this socket, the server is remote.
socket2.getPort() will return 53222 and socket2.getLocalPort() will return 6045.
To this socket2, the server is local.
------------------------------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
For example
ServerSocket serverSocket = new ServerSocket(6045, 50, <InetAddress>);
You create a Socket using the serverSocket's InetAddress and port number to connect to it. You may use this socket to read and write or you can also use the socket returned by serverSocket.accept() to read and write. However, they have different local and remote ports.
A. Using the created socket
Socket socket = new Socket(<InetAddress>, 6045);socket.getPort() will return the port number used in the above line which is 6045. socket.getLocalPort will return something else, lets say 53222.
To this socket, the server is remote.
B. Using the accepted socket
Socket socket2 = serverSocket.accept();socket2.getPort() will return 53222 and socket2.getLocalPort() will return 6045.
To this socket2, the server is local.
------------------------------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
No comments:
Post a Comment