i am new to java networking. my client program is giving an error
"Couldn't get I/O for the connection to:192.168.1.2" in the below program.will u please help me.and 192.168.1.2 this is my local machine.

code:

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


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


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


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

It prints that message because it is catching an exception and that is what you are telling it to print. Add a stacktrace to that catch block and try again, so that you at least know what is going wrong.

yes i have printed stack trace . stack trace due to IOException is
"java.net.ConnectException:Connection refused:connect".what might be reasons for this connection reject and how could i get rid of this error.

If you see the javadocs for ConnectException class, it tells you that this could be typically due to the fact that no process is listening on the port. Make sure the port number that you are using (32133) is being listened by some process.

As crazy (verruckt is crazy in German, which I assume crazy knows, but maybe it was just coincidence) says, it comes when there is nothing listening at the address:port combination that you attempt to connect to. That could be because the db is not running, or is not configured to accept TCP/IP connections, or is listening on a different port, or is running on a different server altogether, or that there is a firewall in the middle that actually rejects connection attempts rather than simply letting them silently drop.

Yes masijade gives a more specific answer in your case.

PS : And yes crazy does know, that verruckt = crazy in German, actually I got crazy translated in german, from google langauge tools. ;)

yaa that's true . My server program is listening on some other port. Thanks for your assistance.

@masijade : And I forgot to mention one thing, it's not a database he is connecting to, rather he hadn't mention to what program he was connecting at all, thats the reason I felt it to keep very generic in my post. And quite wonderfully, thats what the case is I guess, from his previous post.

Well, I used DB, but substitute <generic server> for DB and the point remains the same. ;-)

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.