Special OFFER: 1st Enroll Flat 25% OFF OR 2nd Enroll Get 40% OFF | Use Coupon JTOFFER25

- Java Blogs -

Java.net Connectexception connection refused

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.

Java.net.ConnectException: Connection refused

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.

Read More: Difference between JavaScript and JQuery

Reasons for the Network Error

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:

  • Either client or server or both of them are not in the network, in that case, Java will throw an error - java.net.ConnectException: Connection refused
  • If the server is not working properly or there is an as low network connection, in that case also Java will throw an error - java.net.ConnectException: Connection refused. The most irritating thing to notice here is that the reasons are possibly different but Java throws a single error in all possible scenarios that may be difficult to deal with the Java programmer. If there is any server issue then you can check the status of the server with the command “ping” or there are lots of similar options to get the response back from the port.
  • The most typical case is when the server is running well but not listening to the port. This is the most advanced reason that could not be figured out quickly. The developer needs to check out the network configuration properly. In case of hierarchical configuration, there are chances of overriding where default settings will override your actual settings.
  • If the network is protected by firewalls then same network error will be thrown by the Java programming language. Here, Java developer needs to submit a request to the network administrator for continuous communication between IP addresses and the port. In case, permission is not allowed by the firewall then it will show same error again and again.
  • When you are working on networks, the most obvious terms you should know about our host, port, MAC address, IP address, router, switch etc. If you are not sure any of the terms then networking programming may be a challenge for the developers. The next possible situation is the wrong combination of host and port number. This is possible that you have provided the either wrong host or port number then network will throw the same error again. Here the only solution is to check for active configuration settings and make necessary changes as required for successful communication.
  • During network connection, you have to make sure that correct protocol has been passed as expected by the server. For RMI, Client-Server architecture, and HTTP, most obvious protocol to establish network connection includes TCP.

Ways to Deal With the Error

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.

Read: Node.JS Interview Questions and Answers for Fresher, Experienced
  1. First of all, use the ping command to check to serve is connected to the port or not. If you are able to ping successfully without any error then it shows that client and server both are well connected and the reason is somewhat else.
  2. At the second step, try to connect with server and port with telnet. With “Telnet” command, you would be able to know either problem is with server or client code. Also, you would be able to know serving is running well or not.
  3. Check the configuration of the client that will tell you about correct IP address and port number. For highly complex connections, you should try with RMI calls so that you can be sure on right protocol and the connection string. You can use the same process when using MySQL and JDBC.

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.

Troubleshooting IPv4 Logical Addressing at Network Layer

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.

Troubleshooting IPv4 Addressing

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.

Read: How to Start a Career as a Java Developer or Programmer

IPv4 Addressing Issues

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. Java.net Connectexception connection refused

Figure-1 Correct IPv4 Addressing example

In the given figure, sample subnet for both PCs is ( 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 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. Java.net Connectexception connection refused 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 ( It will again start for DNS look up to know the IP address of the web server. After DNS lookup, IP address 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. Java.net Connectexception connection refused 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. Java.net Connectexception connection refused

Figure-2 Incorrect IPv4 Addressing Example

Java.net Connectexception connection refused 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.

Do you know how to determine IP addresses within a subnet?

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. Java.net Connectexception connection refused

Figure-3 Determining IP Address within a Subnet

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 to Next subnet would be to The third subnet would be to and so on.

Read: What Is the Difference Between Java And JavaScript?

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.  

FaceBook Google+ LinkedIn Pinterest

    Janbask Training

    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.


Trending Courses


  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

3 days 24 Sep 2021


  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

3 days 24 Sep 2021

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

4 days 25 Sep 2021


  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

10 days 01 Oct 2021


  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

4 days 25 Sep 2021


  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

3 days 24 Sep 2021

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

3 days 24 Sep 2021

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

4 days 25 Sep 2021


  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation

Upcoming Class

4 days 25 Sep 2021

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks

Upcoming Class

4 days 25 Sep 2021

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning

Upcoming Class

17 days 08 Oct 2021


  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop

Upcoming Class

3 days 24 Sep 2021

Search Posts


Receive Latest Materials and Offers on Java Course