SocketException: Connection reset

B

bigBrother82

I am currently trying to write a simple ftp client. I have only
recently started and I am running into a problem. Obviously I have not
coded everything yet. I am trying to connect to a remote ftp server,
but not to the login state yet. This is the error I receive:

SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at csci4311.ftp.FTPcommands.serverReply(FTPcommands.java:78)
at csci4311.ftp.FTPcommands.successful(FTPcommands.java:67)
at csci4311.ftp.FTPcommands.connect(FTPcommands.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

The problem is coming from my inputStream. I am using a BufferedReader
as the input stream. It appears that the error is being sparked when I
envoke the bufferedreader method readLine(). Here is my code:

package ftp;

import java.io.*;
import java.net.*;
import java.util.*;

public class FTPcommands{

private Socket socketConnection = null; //the socket we are conected
to the ftp server
private PrintStream outputStream = null;//the socket outputstream
private BufferedReader inputStream = null;//the socket inputstream
private boolean connected;

public FTPcommands(){
this.connected = false;
}

public void connect(String host, int port) throws
UnknownHostException, IOException{
this.socketConnection = new Socket(host, port);
this.outputStream = new
PrintStream(socketConnection.getOutputStream());
this.inputStream = new BufferedReader(new
InputStreamReader(socketConnection.getInputStream()));
if (!successful())
disconnect();
else
this.connected = true;
}

public void disconnect(){
{
if (this.outputStream != null) {
try {
if (this.connected)
logout();
this.outputStream.close();
this.inputStream.close();
this.socketConnection.close();
} catch (IOException e) {}

this.outputStream = null;
this.inputStream = null;
this.socketConnection = null;
}
}
}

public void logout() throws IOException{
int response = executeCommand("quit");
this.connected = !successful();
}

public int executeCommand(String command) throws IOException{
this.outputStream.println(command);
return serverReply();
}

public boolean isConnected(){
return this.connected;
}

public boolean successful() throws IOException{
boolean successful;
int code = serverReply();
if (code >= 200 && code < 300)
successful = true;
else
successful = false;
return successful;
}

public int serverReply() throws IOException{
String serverReply;
do {
serverReply = this.inputStream.readLine();
} while(!(Character.isDigit(serverReply.charAt(0)) &&
Character.isDigit(serverReply.charAt(1)) &&
Character.isDigit(serverReply.charAt(2)) &&
serverReply.charAt(3) == ' '));

return Integer.parseInt(serverReply.substring(0, 3));
}

}//end class FTPcommands.java

The readLine() method is invoked in serverReply() above.

I am stumped, and have searched google vigarously for severel days now
lookin for answers or suggestions. Any insight at all would be greatly
appreciated.

Sincerely,
Jacob Schoen
 
R

Roedy Green

SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at csci4311.ftp.FTPcommands.serverReply(FTPcommands.java:78)
at csci4311.ftp.FTPcommands.successful(FTPcommands.java:67)
at csci4311.ftp.FTPcommands.connect(FTPcommands.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

you are not getting any line numbers. Try using the Java.exe that
comes with the JDK not the JRE. Perhaps that will help you narrow it
down.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top