Error : java.net.ConnectException

hi, I have this exception :

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 Prova_Socket.Prova_SocketClient.setupConnection(Prova_SocketClient.java:37)errore

at Prova_Socket.Prova_SocketClient.main(Prova_SocketClient.java:26)

Exception in thread "main" java.lang.NullPointerException

at Prova_Socket.Prova_SocketClient.getFile(Prova_SocketClient.java:60)

at Prova_Socket.Prova_SocketClient.main(Prova_SocketClient.java:27)

The Source Code is Server :

public class Prova_SocketServerConcorrente

{

private int listenPort = 4035;

private Socket client;

public static void main(String args[])

{

Prova_SocketServer server = new Prova_SocketServer();

server.acceptConnections();

}

public void acceptConnections()

{

try

{

ServerSocket server = new ServerSocket(listenPort);

client = null;

while(true)

{

System.out.println("provo ad accettare");

client = server.accept();

System.out.println("ho accettato");

Esegui esecuzione = new Esegui();

esecuzione.start();

}

}catch(BindException e)

{

System.out.println("unable to bind port");

}

catch(IOException e )

{

System.out.println("Unable to instantiate a ServerSocket on port");

}

}

private class Esegui extends Thread

{

public Esegui()

{

}

public void run()

{

this.doIt();

}

public void doIt()

{ ... }

CLIENT :

public class Prova_SocketClient

{

private String hostip;

private int hostport;

private BufferedReader socket_br;

private PrintWriter socket_pw ;

public Prova_SocketClient (String vhostip , int vhostport)

{

hostip = vhostip;

hostport = vhostport;

}

public static void main(String[] args)

{

Prova_SocketClient prova_client = new Prova_SocketClient("127.0.0.1",4035);

prova_client.setupConnection();

String contenuto = prova_client.getFile("prova.txt");

prova_client.closeConnection();

System.out.println(contenuto);

}

public void setupConnection()

{

try

{

Socket client = new Socket (hostip,hostport);

socket_br = new BufferedReader(new InputStreamReader(client.getInputStream()));

socket_pw = new PrintWriter(client.getOutputStream());

}

catch(UnknownHostException e )

{

e.printStackTrace();

}

catch(IOException e )

{

System.out.println("errore");

e.printStackTrace();

}

}

public String getFile(String fileNameToGet)

{

String testo = null;

try

{

socket_pw.println(fileNameToGet);

socket_pw.flush();

testo ="";

String linea = null;

while((linea = socket_br.readLine())!= null)

{

testo += linea + "\n";

}

}

catch(IOException e)

{

System.out.println("errore nella lettura del file");

}

return testo;

}

public void closeConnection()

{

try

{

socket_pw.close();

socket_br.close();

}

catch(IOException e)

{

System.out.println("errore nella chiusura del socket");

}

}

}

Thanks to all.

[3893 byte] By [lifesuna] at [2007-11-27 5:52:42]
# 1
Hi lifesun,'Connection refused: connect' always means that there is no listener listening on the port given (no server process). Try the 'netstat' command to see if there is really a process listening on port 4035.
zincteapota at 2007-7-12 15:44:15 > top of Java-index,Core,Core APIs...