DarkZero
DarkZero

Reputation: 51

Sending "227 \r\n" on port 21 terminates connection

Let me start off by stating that this is my first post here on stackoverflow. Feel free to point out any mistakes made while posting.

Okay, so the problem at hand is a little weird. I'm currently implementing the FTP protocol for my project, but ran into the following problem. Every time my application has to return the message "227 Entering Passive Mode (<ip1>,<ip2>,<ip3>,<ip4>,<port1>,<port2>)." the connection is terminated. The stack trace being:


    java.net.SocketException: Software caused connection abort: socket write error
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(Unknown Source)
        at java.net.SocketOutputStream.write(Unknown Source)
        at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
        at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
        at sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
        at sun.nio.cs.StreamEncoder.flush(Unknown Source)
        at java.io.OutputStreamWriter.flush(Unknown Source)
        at java.io.BufferedWriter.flush(Unknown Source)
        at Server$1.run(Server.java:30)
        at java.lang.Thread.run(Unknown Source)

In order to replicate the behaviour I decided to build a prototype which accepts a connection on port 21 and sends the message "227 " after recieving an arbitrary message from the client. This also results in the connection being terminated.

(Prototype Code snippet:)


    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.ServerSocket;
    import java.net.Socket;


    public class Server {
        public static void main(String[] args) {
            Thread thread = new Thread(new Runnable() {
                public void run() {
                    ServerSocket server = null;
                    try {
                        server = new ServerSocket(21);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    while(true) {
                        try {
                            Socket client = server.accept();

                            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
                            BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));

                            String line = null;
                            while((line = reader.readLine()) != null) {
                                writer.write("227 \r\n");
                                writer.flush();
                            }
                        } catch(IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            thread.start();
        }
    }

(I'm aware the above code is not the nicest implementation, but is imho sufficiently adequate for representing the problem at hand.)

After some tinkering with the prototype I have figured out that this behaviour only occurs when sending "227 " in conjunction with using port 21 (e.g. using port 4500 works just fine).

Now of course the obvious workaround to this is to avoid using port 21, but i would like to know why i'm experiencing this. Any input on the matter is highly appreciated ;) (just to make sure, i'm using JavaSE-1.7).

Upvotes: 2

Views: 139

Answers (0)

Related Questions