I am unable to do any networking in Java when i run a simple networking program i am unable to get a connection, even when using the local host, The Error:
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at EchoClient.main(EchoClient.java:8)
This is the program:
import java.io.*;
import java.net.*;
import java.util.*;
public class EchoClient{
public static void main(String[] args) {
try{
Socket client = new Socket(InetAddress.getLocalHost(), 1234);
InputStream clientIn = client.getInputStream();
OutputStream clientOut = client.getOutputStream();
PrintWriter pw = new PrintWriter(clientOut);
BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));
Scanner stdIn = new Scanner(System.in);
System.out.println("Input Message:");
pw.println(stdIn.nextLine());
System.out.println("Recieved Message:");
System.out.println(br.readLine());
pw.close();
br.close();
client.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
I use windows 7, i have turned of windows firewall and i dont have an anti virus.
close()is done after the client wants response - which will lead to "deadlock"...