Is there any way to find port number in HttpsUrlConnection? - java

I created one app which will download data from a remote server. I used HttpsUrlConnection Class to perform this. Data is getting downloaded successfully. But I want to know ,through which port the communication is happening (Both my machine's port number and remote machine's port number) . Can anyone please help me to find out the port number? Thanks in advance.

As far as the remote host's port number goes that is whatever you specify as the port in your url. If that is not available for any reason you can get it by getting a URL object from the connection and then get the port from the URL object:
int port = connection.getURL().getPort();
When it comes to which port your local machine is using for the connection I am not sure java supports that. You can list active connections in many operating systems using netstat. Syntax may vary depending on operating system (Unix, Windows).

Related

How does DNS work with Java Sockets?

My question feels kind of basic, and yet it has made me curious for a while:
Does using the name of a server instead of its IP address work when using a Java Socket?
For example, if I am the manager of a certain server with the address "bogusserver.com" and use this address instead of the actual IP of the server when opening the Socket with a 'new Socket("bogusserver.com", 8080);' will it actually open the socket normally?
If it does, how exactly does the Socket class solve the address? Does it use the DNS server registered at the computer running the code?
I know that using the "localhost" address the Socket will open normally, but then, everything works with localhost :P
You just need to read the javadoc. A Socket is constructed using an InetAddress.
The InetAddress javadoc says:
Host name-to-IP address resolution is accomplished through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and Network Information Service(NIS). The particular naming services(s) being used is by default the local machine configured one. For any host name, its corresponding IP address is returned.
Does using the name of a server instead of its IP address work when using a Java Socket?
DNS maybe used by the OS to translate the hostname into an IP address. It doesn't matter if you use TCP, UDP or ICMP.
If it does, how exactly does the Socket class solve the address?
It passes it to the OS to do the lookup.
Does it use the DNS server registered at the computer running the code?
The OS has registered DNS servers and they may be contacted if there isn't a hosts entry for that hostname.

Using RMI over different network

The guest's computer can be on a network other than the server's.
If it isn't, everything is OK, but when he is, a ConnectException is thrown.
Do you know why ?
Extra info:
I modified the port from 1099 (default) to 80 to try to solve the problem, doesn't work.
Using Wireshark, I saw that guest were using port 50740. I don't understand why, and never saw this number before.
I grant all permissions with a .policy file.
Edit :
I already have server IP defined by a .bat. I also have Locate.createRegistry(80); at the beginning of my client. Is it enough to make client use port 80 ?
Strange thing is that i can try to log in (my app asks for a login when launched), see if my credentials are OK or not. Then, if client isn't on the same network, the ConnectException is thrown.
I modified the port from 1099 (default) to 80 to try to solve the problem, doesn't work. Using Wireshark, I saw that guest were using port 50740. I don't understand why, and never saw this number before.
When an RMI client wants to talk to a remote object it typically starts by contacting the RMI registry on port 1099 to ask where it can find the target object. The registry replies with a stub containing the address of the target object (host name and port number), and the client can then connect to the target host and port to talk to the remote object.
If you don't specify an explicit port number in the call to the UnicastRemoteObject superclass constructor or the static exportObject method, then RMI will select a random available port number to use. That's probably where the 50740 comes from - it's the port that the target object is listening on, as opposed to the registry.
But the second element of the target object address is the host name - if the object is listed in the registry at an address like 127.0.0.1:50740 then a client on a different machine will end up trying to connect to the object in the wrong place (on the client's localhost rather than the server's). The solution is to ensure that the objects are bound in the registry under a proper host name or IP address that is resolvable from the client - in theory this should happen automatically, but sometimes RMI gets it wrong. The solution is to pass a system property to the RMI server (the process that is binding the target object in the registry)
java -classpath .... -Djava.rmi.server.hostname=192.168.0.1 com.example.MyRmiServer
Replace 192.168.0.1 with the correct IP address that the client machine will use to talk to the server.
You need to export your remote objects on a fixed port number, and open that port in your firewall. The easiest port to use is 1099 as it's already reserved, but it requires that you start the Registry in your JVM via LocateRegistry.createRegistry() instead of using rmiregistry, so you can share the port.

Java Network Connection - No Further Information Error

Alright, I have a java server setup using port 6567 and IP address 0.0.0.0 as to accept any connection. When I attempt to connect over my local network (192.168.1.15) I am able to connect just fine using the server. However when I switch to a non-local IP address (my routers public IP) I am unable to connect to it.
I have the router port forwarded and the proper rules in place on my firewall/etc. Is there any limitations on Java connecting in this fashion? I'm able to connect externally but not internally. Any thoughts on what might be causing this problem?
I'm starting to think it might be a router-specific problem, being that it could be rejecting the connection but I am unable to test that currently.
Turns out it was just the router itself that rejects internal connections using an external IP address. My personal fix was to just add a bit of testing code that automatically changes the IP if on a local machine to 127.0.0.1 rather then the external IP.
Worked flawlessly both on my own PC and having people connect externally once I set that up.
Hmm I'm not sure about it but maybe that will help.
Most probable, Your ServerSocket gets bound to a local IP address (e.g. 0.0.0.0) and ServerSocket binds to the port address there; and wouldn't respond to any requests coming from an IP address. Try new ServerSocket(4444, 50, InetAddress.getByAddress(new byte[] { YOU IP ADDRESS }).
or check again firewall
edit: Tell me how did you tried to connect from other IP than local?

getting the port number of a website

How can i obtain the port number of a website using a program. Is there any method / way that i can use to know the port number of a website ?
Or, if i know that my port number 52970 is connected with 212.58.241.131 this ip , can i know the port number with which the port number of my PC is connected ?
I believe you need to review the concept of port numbers.
By default, HTTP uses port 80. So an individual website you visit won't need access to any other port.
TCP and UDP port numbers
I am not sure what you are looking for but from a PHP script you can get the port the client uses to connect to your web server with $_SERVER["REMOTE_PORT"]
The port number for http is port 80 and 443 for ssl.
If you are on windows start up cmd and type netstat -a -b to see what program connects where.
Please elaborate or post an example of what you want to achieve as it's not quite clear to me.
€dit: in php you can find the remote or server port with
$_SERVER['REMOTE_PORT']
or
$_SERVER['SERVER_PORT']
If I am right, you are looking for the remote port.
http://php.net/manual/en/reserved.variables.server.php
for telnet look here :
http://www.geckotribe.com/php-telnet/
I am not sure if this helps you at all but the gold standard for scanning ports has to be nmap.
http://nmap.org/
You can scan open ports for a specific IP address.
echo $_SERVER['SERVER_PORT']."<br/>";
echo $_SERVER['REMOTE_PORT']."<br/>";
'SERVER_PORT'
The port on the server machine being used by the web server for
communication. For default setups, this will be '80'; using SSL, for
instance, will change this to whatever your defined secure HTTP port
is.
'REMOTE_PORT'
The port being used on the user's machine to communicate with the web
server.

BACNet plugin for Building Management System

I'm trying to get a BACNet scanner up on an Seimens server running the Apogee system with a BACNet interface. I've tried using BACNet4j put i get a port bind error on the LocalDevice object for test/Scan.java.
Does anyone know of any other libraries I could use or a reference to instructions for setting up a BACNet plugin to a building management system?
I have had the same problem before, i.e. the BACnet client needs to both send and receive from UDP port 47808. Since the BACnet server already uses that port to listen (and reply) my solution was to use a virtual IP (a bridge) so that my client runs on the same Ethernet card but with a different IP address. A bit convoluted, I know, but it works.
Whether or not the Apogee system supports virtual (or simply additional) network drivers is another question altogether. On my Linux and Windows machines I can run as many servers and clients as I need (I actually don't know what is the limit, I have run up to 5 servers and 3 clients without any problems).
Concerning the port bind error, you may have to configure your firewall because:
BACnet/IP is using UDP
the default port number is 47808 (0xBAC0)
Your issue might be the use of a (BACnet port #) socket that is already in-use; you have to ensure that it's not in exclusive-use - before binding to the socket, but also (slightly more) important, also ensure it's marked for reuse.
But unless you're listening for Who-Is broadcasts, I'd recommend listening for the (unicast) responses upon a different port #, e.g. 0xBAC1/47809, but still send upon the standard port # 0xBAC0/47808.

Categories