Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
String hostName = "YODA"; // "127.0.0.1" // "149.169.87.78" // "localhost"
try {
echoSocket = new Socket(hostName, 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
}
catch (UnknownHostException e) {
System.out.println("Can't connect to host: " + hostName);
System.exit(1);
}
catch (IOException e) {
System.out.println("Can't create I/O for connection to: " + hostName);
System.exit(1);
}
Quote:My problem: I'm getting the IOException. That is, IOException is being thrown by either of these lines:
getOutputStream public OutputStream getOutputStream() throws IOException Returns an output stream for this socket. If this socket has an associated channel then the resulting output stream delegates all of its operations to the channel. If the channel is in non-blocking mode then the output stream's write operations will throw an IllegalBlockingModeException. Returns: an output stream for writing bytes to this socket. Throws: IOException - if an I/O error occurs when creating the output stream or if the socket is not connected.
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
That is, if you comment out the "in =" statement the exception will be thrown. And if you comment out the "out =" the exception will be thrown. And the Socket(hostName, port) constructor is working I believe, because if you put anything else as the host, like "omghikthx", the UnknownHostException will be thrown. So AFAIK I'm connecting alright, but I just can't create an I/O stream from the socket. The program (if you didn't analyze the sun tutorial) works by creating an input (taking input from the net) and an output (sending output to the net) stream on the same socket, so the EchoClient prog is both a an echo client and echo server. The network I'm on is at a uni dorm, and my IP is assigned via DHCP. So everything you see here:
String hostName = "YODA"; // "127.0.0.1" // "149.169.87.78" // "localhost" i've tried: YODA is the host name I get by doing an ipconfig /all on WinXP console; 149.169.87.78 is the local IP assigned by the LAN here. Don't know if that makes a difference, though... I haven't tried changing the port number yet. Could 7 be reserved? (yes, I know, i'm a newb, etc.) Thanks for your time. I hope I explained it all enough.