1,105,226 Community Members

java.net.ConnectException: Connection refused

Member Avatar
sciwizeh
Posting Pro in Training
484 posts since Jun 2008
Reputation Points: 62 [?]
Q&As Helped to Solve: 25 [?]
Skill Endorsements: 0 [?]
 
0
 

new to network stuff going through the java tutorials and i did what it said heres the code:

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

public class EchoClient {
    public static void main(String[] args) throws IOException {

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            echoSocket = new Socket("bill-laptop", 7);
            try{
                out = new PrintWriter(echoSocket.getOutputStream(), true);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for "
                               + "the connection to: OUT.");
                System.exit(1);
            }
            try{
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for "
                               + "the connection to: IN.");
                System.exit(1);
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        }

    BufferedReader stdIn = new BufferedReader(
                                   new InputStreamReader(System.in));
    String userInput;

    while ((userInput = stdIn.readLine()) != null) {
        out.println(userInput);
        System.out.println("echo: " + in.readLine());
    }

    out.close();
    in.close();
    stdIn.close();
    echoSocket.close();
    }
}

bill-laptop is the name of the computer that i am trying to run this on here is the stack trace:

java.net.ConnectException: Connection refused: connect
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
	at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
	at java.net.Socket.connect(Socket.java:519)
	at java.net.Socket.connect(Socket.java:469)
	at java.net.Socket.<init>(Socket.java:366)
	at java.net.Socket.<init>(Socket.java:180)
	at EchoClient.main(EchoClient.java:12)

running on a vista :-( but the laptop is brand new duel-core :-)
any advise

Member Avatar
darkagn
Veteran Poster
1,199 posts since Aug 2007
Reputation Points: 279 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 21 [?]
 
0
 

I know that your tutorial probably says to connect to port 7 for an echo, but try connecting to a port > 1024. Chances are your errors are due to trying to connect to a port that is in use.

Member Avatar
sciwizeh
Posting Pro in Training
484 posts since Jun 2008
Reputation Points: 62 [?]
Q&As Helped to Solve: 25 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks for the reply

i tried it, but it didn't work:(

Member Avatar
darkagn
Veteran Poster
1,199 posts since Aug 2007
Reputation Points: 279 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 21 [?]
 
0
 

Try replacing your computer name with your IP Address. You will need to create an InetAddress object using your IP and pass that object into the constructor for Socket.

Member Avatar
stephen84s
Nearly a Posting Virtuoso
1,328 posts since Jul 2007
Reputation Points: 550 [?]
Q&As Helped to Solve: 160 [?]
Skill Endorsements: 13 [?]
Featured
 
0
 

For running the program mentioned, you need an echo server running on your system, which is provided by most UNIX machines and runs on port no 7.
To check if your Vista box has one running just hit telnet localhost 7 , if it shows connection refused or could not connect, then it means you box does not have an echo server, and hence your program will not work and thats the reason I feel for your ConnectException exception.

I know that your tutorial probably says to connect to port 7 for an echo, but try connecting to a port > 1024. Chances are your errors are due to trying to connect to a port that is in use.

Now this prob would have occurred if we were trying to create a ServerSocket on the given port, But as we are creating a client socket here to connect on the given port we apparently want the opposite, that is the port should be in use by the "echo server" so that we can connect to it.

Member Avatar
sri.sista
Newbie Poster
1 post since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I too have the same problem.If u get any solution pls let me.

new to network stuff going through the java tutorials and i did what it said heres the code:

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

public class EchoClient {
    public static void main(String[] args) throws IOException {

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            echoSocket = new Socket("bill-laptop", 7);
            try{
                out = new PrintWriter(echoSocket.getOutputStream(), true);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for "
                               + "the connection to: OUT.");
                System.exit(1);
            }
            try{
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for "
                               + "the connection to: IN.");
                System.exit(1);
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        }

    BufferedReader stdIn = new BufferedReader(
                                   new InputStreamReader(System.in));
    String userInput;

    while ((userInput = stdIn.readLine()) != null) {
        out.println(userInput);
        System.out.println("echo: " + in.readLine());
    }

    out.close();
    in.close();
    stdIn.close();
    echoSocket.close();
    }
}

bill-laptop is the name of the computer that i am trying to run this on here is the stack trace:

java.net.ConnectException: Connection refused: connect
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
	at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
	at java.net.Socket.connect(Socket.java:519)
	at java.net.Socket.connect(Socket.java:469)
	at java.net.Socket.<init>(Socket.java:366)
	at java.net.Socket.<init>(Socket.java:180)
	at EchoClient.main(EchoClient.java:12)

running on a vista :-( but the laptop is brand new duel-core :-)
any advise

Member Avatar
stephen84s
Nearly a Posting Virtuoso
1,328 posts since Jul 2007
Reputation Points: 550 [?]
Q&As Helped to Solve: 160 [?]
Skill Endorsements: 13 [?]
Featured
 
0
 

Have you tried what I had mentioned in my previous post ?????

Member Avatar
sciwizeh
Posting Pro in Training
484 posts since Jun 2008
Reputation Points: 62 [?]
Q&As Helped to Solve: 25 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks for the replies,

telnet? i've never used telnet, is it command prompt? if so this doesn't have it, sorry for my ignorance

i think it may just be vista refusing the VM access, but other things that use java.net will run

Member Avatar
sciwizeh
Posting Pro in Training
484 posts since Jun 2008
Reputation Points: 62 [?]
Q&As Helped to Solve: 25 [?]
Skill Endorsements: 0 [?]
 
0
 

ok i figured out telnet and telnet localhost 7 did give connection failed, so what should i do to get an echo server running?

Member Avatar
amjad277
Newbie Poster
8 posts since Jun 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I used your code and executed it with localhost (as name of my computer) and the port 4000.
echoSocket = new Socket("localhost", 4000);
It works without errors.

you can use code for server like this:

try{
 serverSocket = new ServerSocket(4000);
} catch (IOException e) {
System.err.println("Could not listen on port: 4000.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
out = new DataOutputStream(clientSocket.getOutputStream());
is = clientSocket.getInputStream();
Member Avatar
sciwizeh
Posting Pro in Training
484 posts since Jun 2008
Reputation Points: 62 [?]
Q&As Helped to Solve: 25 [?]
Skill Endorsements: 0 [?]
 
0
 

woe, what? how would i run this, what type is is ? i'm not new to Java, just very new to networking, sorry about my ignorance :confused:

Member Avatar
ChaseVoid
Junior Poster
116 posts since Aug 2007
Reputation Points: 30 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 0 [?]
 
0
 

The problem is that there is no server running in your system. Install the IIS7 server in Vista. Look here for directions.

Also always use a port greater than 5000. And Do you have a ServerSocket running? I think you need it communicate with the Socket.

Member Avatar
sciwizeh
Posting Pro in Training
484 posts since Jun 2008
Reputation Points: 62 [?]
Q&As Helped to Solve: 25 [?]
Skill Endorsements: 0 [?]
 
0
 

this
doesn't say anything about writing a server and neither does the next page, nothing about an echo server

Member Avatar
ChaseVoid
Junior Poster
116 posts since Aug 2007
Reputation Points: 30 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 0 [?]
 
0
 

The Socket constructor used here requires the name of the machine and the port number to which you want to connect. The example program uses the host name taranis. This is the name of a hypothetical machine on our local network. When you type in and run this program on your machine, change the host name to the name of a machine on your network. Make sure that the name you use is the fully qualified IP name of the machine to which you want to connect. The second argument is the port number. Port number 7 is the port on which the Echo server listens.

As you can see, you need to have an echo server already running. So the hostname can be any computer on you network. I don't have any network connection open so I can't test this application. You needed to change 'tarnis' to a valid IP name of any other computer in your network. If you can't then skip this application. Try the next one. It'll explain you the use of ServerSocket and Socket. This example is used to demostrate how to read the data from the server.

This client program is straightforward and simple because the Echo server implements a simple protocol. The client sends text to the server, and the server echoes it back.

If you want I can give you an Example of ServerSocket and ClientSocket.

Member Avatar
sciwizeh
Posting Pro in Training
484 posts since Jun 2008
Reputation Points: 62 [?]
Q&As Helped to Solve: 25 [?]
Skill Endorsements: 0 [?]
 
0
 

i already have changed the name of the computer, i suppose i will just skip it, but my problem is not solved, so i don't know whether to mark this as solved or not, so ill leave it as unsolved for now

still any advice is welcome

Member Avatar
ChaseVoid
Junior Poster
116 posts since Aug 2007
Reputation Points: 30 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 0 [?]
 
0
 

>i don't know whether to mark this as solved or not
Well like I said, the problem lies in the fact that you need to conect to another system in your network, I don't know how to create a self echo server. I suppose if you can change the program to listen to the echo server on your own system, it might help.

Member Avatar
ChaseVoid
Junior Poster
116 posts since Aug 2007
Reputation Points: 30 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 0 [?]
 
0
 

You can use the following to create an EchoServer and then use it along with your EchoClient.

http://www.cs.princeton.edu/introcs/84network/EchoServer.java.html

A more complex version from Sun Java is here
https://java.sun.com/developer/onlineTraining/Beans/JBeansAPI/Magercises/SocketBean/EchoServer.java

Member Avatar
sciwizeh
Posting Pro in Training
484 posts since Jun 2008
Reputation Points: 62 [?]
Q&As Helped to Solve: 25 [?]
Skill Endorsements: 0 [?]
 
0
 

still doesn't work, it will work in telnet, but not from java :(

Member Avatar
bostown03
Newbie Poster
2 posts since Aug 2008
Reputation Points: 1 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
1
 

I played around with it for a bit and got it working.

Copy and paste it and run it! :)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class EchoClient {
    public static void main(String[] args) throws IOException {

	Socket echoSocket = null;
	PrintWriter out = null;
	BufferedReader in = null;

	ServerSocket serverSocket = null;

	String localhost = "127.0.0.1";

	try {
	    // Create a listener socket for the server
	    serverSocket = new ServerSocket(4000);

	    echoSocket = new Socket(localhost, 4000);

	    out = new PrintWriter(echoSocket.getOutputStream(), true);

	    // Accept ANY activity going to port 4000
	    Socket sock = serverSocket.accept();

	    // Take the InputStream from the "sock" socket
	    in = new BufferedReader(new InputStreamReader(sock.getInputStream()));

	} catch (UnknownHostException e) {
	    System.err.println("Don't know about host: " + localhost + ".");
	    e.printStackTrace();
	    System.exit(1);

	} catch (IOException e) {
	    System.err.println("Couldn't get I/O for " + "the connection to: " + localhost + ".");
	    e.printStackTrace();
	    System.exit(1);

	}

	BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
	String userInput;

	while ((userInput = stdIn.readLine()) != null) {
	    out.println(userInput);
	    System.out.println("echo: " + in.readLine());
	}

	out.close();
	in.close();
	stdIn.close();
	echoSocket.close();

	serverSocket.close();
    }

}
Member Avatar
sciwizeh
Posting Pro in Training
484 posts since Jun 2008
Reputation Points: 62 [?]
Q&As Helped to Solve: 25 [?]
Skill Endorsements: 0 [?]
 
0
 

cool! thanks!

Question Answered as of 5 Years Ago by ChaseVoid, stephen84s, darkagn and 3 others
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article