java net ConnectException Connection refused

0 votes

The server side of my attempt to establish a TCP connection goes smoothly, however when I run the client software (from the client computer), I receive the following error:

java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
        at java.net.Socket.connect(Socket.java:529)
        at java.net.Socket.connect(Socket.java:478)
        at java.net.Socket.<init>(Socket.java:375)
        at java.net.Socket.<init>(Socket.java:189)
        at TCPClient.main(TCPClient.java:13)

Can somebody tell me what is generating this issue and how to repair it? I tried changing the socket number in case it was already in use, but to no effect.

Server code:

//TCPServer.java

import java.io.*;
import java.net.*;

class TCPServer {
    public static void main(String argv[]) throws Exception {
        String fromclient;
        String toclient;

        ServerSocket Server = new ServerSocket(5000);

        System.out.println("TCPServer Waiting for client on port 5000");

        while (true) {
            Socket connected = Server.accept();
            System.out.println(" THE CLIENT" + " " + connected.getInetAddress()
                    + ":" + connected.getPort() + " IS CONNECTED ");

            BufferedReader inFromUser = new BufferedReader(
                    new InputStreamReader(System.in));

            BufferedReader inFromClient = new BufferedReader(
                    new InputStreamReader(connected.getInputStream()));

            PrintWriter outToClient = new PrintWriter(
                    connected.getOutputStream(), true);

            while (true) {

                System.out.println("SEND(Type Q or q to Quit):");
                toclient = inFromUser.readLine();

                if (toclient.equals("q") || toclient.equals("Q")) {
                    outToClient.println(toclient);
                    connected.close();
                    break;
                } else {
                    outToClient.println(toclient);
                }

                fromclient = inFromClient.readLine();

                if (fromclient.equals("q") || fromclient.equals("Q")) {
                    connected.close();
                    break;
                } else {
                    System.out.println("RECIEVED:" + fromclient);
                }

            }

        }
    }
}

The Client Code:

//TCPClient.java

import java.io.*;
import java.net.*;

class TCPClient {
    public static void main(String argv[]) throws Exception {
        String FromServer;
        String ToServer;

        Socket clientSocket = new Socket("localhost", 5000);

        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
                System.in));

        PrintWriter outToServer = new PrintWriter(
                clientSocket.getOutputStream(), true);

        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));

        while (true) {

            FromServer = inFromServer.readLine();

            if (FromServer.equals("q") || FromServer.equals("Q")) {
                clientSocket.close();
                break;
            } else {
                System.out.println("RECIEVED:" + FromServer);
                System.out.println("SEND(Type Q or q to Quit):");

                ToServer = inFromUser.readLine();

                if (ToServer.equals("Q") || ToServer.equals("q")) {
                    outToServer.println(ToServer);
                    clientSocket.close();
                    break;
                } else {
                    outToServer.println(ToServer);
                }
            }
        }
    }
}
Sep 22, 2022 in Java-Script by Tejashwini
• 3,820 points
497 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Java-Script

0 votes
0 answers

OSMdroid tiles not loading due to java.net.UnknownHostException

OpenStreetMap import and usage are required as ...READ MORE

Sep 29, 2022 in Java-Script by Abhinaya
• 1,160 points
690 views
0 votes
1 answer

How to call jQuery UI Dialog with ASP.NET button postback?

Hello @kartik, To solve the problem with ASP:Button ...READ MORE

answered Apr 28, 2020 in Java-Script by Niroj
• 82,880 points
3,123 views
0 votes
1 answer

How to catch net::ERR_CONNECTION_REFUSED?

Hello @kartik, You have access to online/offline in ...READ MORE

answered Jun 18, 2020 in Java-Script by Niroj
• 82,880 points
7,985 views
0 votes
1 answer

Error:Failed to load resource: net::ERR_FILE_NOT_FOUND loading json.js

Hii @kartik,  got the same error using: <link rel="stylesheet" ...READ MORE

answered Jun 19, 2020 in Java-Script by Niroj
• 82,880 points
19,597 views
0 votes
1 answer

How to Determine if page is valid in JavaScript - ASP.NET?

Hello @kartik,  Make the call on an input ...READ MORE

answered Sep 9, 2020 in Java-Script by Niroj
• 82,880 points
2,328 views
0 votes
1 answer

How to disable asp.net button after click to prevent double clicking?

Hello @kartik, On the front end, add these ...READ MORE

answered Sep 9, 2020 in Java-Script by Niroj
• 82,880 points
7,737 views
0 votes
1 answer

Best way to open a socket in Python

Opening sockets in python is pretty simple. ...READ MORE

answered Feb 21, 2019 in Python by SDeb
• 13,300 points
509 views
0 votes
0 answers

Track the exact location of IP address

I looked into IP addresses and a ...READ MORE

Feb 14, 2022 in Cyber Security & Ethical Hacking by Edureka
• 13,620 points
634 views
0 votes
0 answers

small ip scanner code in java

import java.net.*; public class myIP { public static void ...READ MORE

Feb 15, 2022 in Cyber Security & Ethical Hacking by Edureka
• 13,620 points
279 views
0 votes
1 answer

How do I resolve the "java.net.BindException: Address already in use: JVM_Bind" error?

If you are aware about what port ...READ MORE

answered Feb 22, 2022 in Java by Aditya
• 7,680 points
4,718 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP