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

Recommended Answers

All 19 Replies

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.

thanks for the reply

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

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.

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.

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

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

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

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

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();

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:

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.

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

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.

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

>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.

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

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();
    }

}
commented: Solved my problem, finally some one did. thank you! +1

cool! thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.