
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;
//InetAddress address ="192.168.1.2";
try
{
echoSocket = new Socket("sunil-PC", 7);
out = new PrintWriter(echoSocket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: sunil-PC.");
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for " + "the connection to: sunil-PC."+ e);
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();
}
}
this is my code, compiled with no errors, but when running giving "java.net.connectexception connection refused: connect"

Check that the server (something like EchoServer) is up and running.

Thanx i got the answer, i just turned off the firewall & its running fine...
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.