coolkid5
December 31st, 2008, 09:13 PM
Hello, I've been trying to learn java network programming using a tutorial (http://java.sun.com/docs/books/tutorial/networking/sockets/readingWriting.html).
I tried copying and pasting the code from the tutorial into eclipse and changing the parameter in the construction of the socket from "taranis" to InetAddress.getLocalHost()
Here is 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(InetAddress.getLocalHost(), 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
System.exit(1);
} catch (IOException e) {
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();
}
}
When I try to run it, it returns:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Abstrac tPlainSocketImpl.java:310)
at java.net.AbstractPlainSocketImpl.connectToAddress( AbstractPlainSocketImpl.java:176)
at java.net.AbstractPlainSocketImpl.connect(AbstractP lainSocketImpl.java:163)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:381)
at java.net.Socket.connect(Socket.java:537)
at java.net.Socket.connect(Socket.java:487)
at java.net.Socket.<init>(Socket.java:384)
at java.net.Socket.<init>(Socket.java:227)
at EchoClient.main(EchoClient.java:13)
I'm not sure what the problem is though... Help would be appreciated :)
I tried copying and pasting the code from the tutorial into eclipse and changing the parameter in the construction of the socket from "taranis" to InetAddress.getLocalHost()
Here is 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(InetAddress.getLocalHost(), 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
System.exit(1);
} catch (IOException e) {
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();
}
}
When I try to run it, it returns:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Abstrac tPlainSocketImpl.java:310)
at java.net.AbstractPlainSocketImpl.connectToAddress( AbstractPlainSocketImpl.java:176)
at java.net.AbstractPlainSocketImpl.connect(AbstractP lainSocketImpl.java:163)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:381)
at java.net.Socket.connect(Socket.java:537)
at java.net.Socket.connect(Socket.java:487)
at java.net.Socket.<init>(Socket.java:384)
at java.net.Socket.<init>(Socket.java:227)
at EchoClient.main(EchoClient.java:13)
I'm not sure what the problem is though... Help would be appreciated :)