In this blog, we will discuss on one of the most common exceptions, you have to work on when dealing with network programming in Java. “Connect” is the most common networking exception in Java.
The error comes when you try to make TCP connection from the client to the server. The similar error may come during RMI (Remote Method Invocation) because of RMI method also underneath TCP_IP protocol. With this blog let’s focus on why the error comes and possible ways to resolve the network error successfully. Normally, Java books don’t tell you how to handle these problems quickly. This is the reason we have compiled this tutorial to help you with the exception that is really helpful for Java professionals during network programming.
The network error “java.net.ConnectException: Connection refused” clearly states that the client is trying to make TCP connection with the server and it fails at the end. Here are the possible reasons:
In the above section, we have discussed all possible reasons closely. Here is the right time to approach a solution to deal with “java.net.ConnectException: Connection refused” network error professionally.
Check firewall connections and make sure that networks are accessible to each other to make successful communication. Also, you have to make sure that source and destination computer should be on same network, host, or router.
To establish communication at the network layer, we need a global addressing scheme that is named as logical addressing in the network world. These logical addresses are referred to as IPv4 and IPv6 addresses. Although IPv6 address scheme has been deployed into network world still there are many organizations rely on IPv4 addressing only. This is the reason why every network engineer should know how to troubleshoot IPv4 logical addressing on devices. The technical issues may be faced by network engineer while troubleshooting IPv4 like bad address, subnet mask or default gateway address etc.
Read More: Java Programmer Resume Sample
When IPv4 addresses are deployed on devices, they are assigned dynamically through DHCP protocol. The next topic of interest is Network Address Translation (NAT). NAT is required to translate IPv4 logical addresses to public addresses that can be further routed on the internet. This adds another complexity to the environment as you need to know how to troubleshoot NAT so that devices can be accessed quickly even externally to the organization. This is true that we are using IPv4 as of now, but soon there will come a time when IPv6 address scheme will be deployed in the organization. So you should also know how to troubleshoot IPv6 successfully. In future tutorials, we will discuss IPv6 addressing schemes, addressing technologies and their troubleshooting tricks in deep that can help you in starting a successful career with organizations. As of now, we are limited to troubleshooting tricks for IPv4 logical addressing at the network layer.
Here, we will focus on deep understanding of IPv4 addressing and how to check either each of the devices is addressed correctly on the network or not.
Every IPv4 address is composed of two major parts – one network/subnet mask and other is host part. When two devices wanted to communicate together, then it is necessary that both of them should share same network/subnet masks. In case, the subnet mask for both of the systems is different then it will notify back with addressing issues that need to troubleshoot by a network engineer.
In the given figure, sample subnet for both PCs is (10.1.1.0/26) and they are sharing default gateway R1. Let us discuss on network addressing how it works actually. Take an example where PC1 wants to communicate with PC2. In that case, it will start for DNS look up to know the IP address for the PC2. After DNS lookup, IP address 10.1.1.20 is returned. Now PC1 will check either PC2 is located on the same network or they are sharing same subnet mask or not. For this purpose, it will convert the Decimal number to Binary to get the accurate results. As you can see that both binary bits are exactly the same, so it is clear that PC1 and PC2 share the same network and packets can be exchanged directly without any need of default gateway R1. In technical words, PC1 will create a MAC address of its own system as source and MAC address For PC2 as the destination. Now let us see what happens when PC1 wants to communicate with a web server (192.0.2.1). It will again start for DNS look up to know the IP address of the web server. After DNS lookup, IP address 192.0.2.1 is returned. Now PC1 will check either PC2 is located on the same network or they are sharing same subnet mask or not. For this purpose, it will convert the Decimal number to Binary to get the accurate results. As you can see that both binary bits are not exactly the same so it is clear that PC1 and web server don’t share the same network and packets cannot be exchanged directly. Here, you need default gateway R1 to establish the communication further. In technical words, PC1 will create MAC address of its own system as source and MAC address of web server as the destination. The above discussion makes sure that correct IP address is paramount for success. Now the question comes what will happen if PC1 is configured with wrong subnet mask by mistake. This is given in figure 9-2.
After comparison PC1 conclude that PC2 is not on the same network and it needs to route the packets to the default gateway R1. SO, it will create a MAC address of its own system as the source and MAC address of the default gateway R1 as the destination. As a result, the packets will be addressed to the wrong device even if both of the systems were connected to the same network only. This is an example of Incorrect IP addressing that need to troubleshoot further by a network engineer. Not only an improper subnet mask will cause issues, but improper IP addressing along with a right subnet mask will also cause connectivity issues. Additionally, if the default gateway is not configured correctly, then it will route the packets to the wrong destination that is not acceptable to any of the organization. Being a troubleshooter or network engineer, you should be aware of all possible connectivity issues, you may face during network configuration. If you are aware of all possible situations only then you can resolve the issues quickly. The IP information can be verified on Windows PC with "ipconfig" command and you can check the same details on a router or switch with show IP interface inter- face_typeinterface_number command.
Here is a quick tour that will help you in determining IP addresses within a subnet. The Figure 9-3 “Determining IP Address within a Subnet” will give you a clear idea how they are actually assigned.
Take the last octet of the subnet mask and subtract it from 256. In this case, the last octet is 192 and when it is subtracted from 256, the result would be 64. So subnet for this network can be defined as 10.1.1.0/26 to 10.1.1.63/26. Next subnet would be 10.1.1.64/26 to 10.1.1.127/26. The third subnet would be 10.1.1.128/26 to 10.1.1.191/26 and so on.
You May Like:
If you are sure about subnet creation, IP addressing and routing then this would be easy for you to solve the network issues correctly and quickly too. Now you have the complete range and you can easily either computer are connected to the same network or not. With this java tutorial, you can quickly check both source and destination computer are connected to the same network so that network error can be avoided quickly.
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
Receive Latest Materials and Offers on Java Course