2

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).

DarkZero
  • 51
  • 5
  • If it works with port 4500, but not with 21, doesn't that indicate a possible network problem? Use `tcpdump` or similar to see what's happening on the network level. – Kayaman Mar 06 '15 at 13:07
  • 1
    It indeed looks like some firewall is inspecting a traffic on port 21 and terminates the connection. – Martin Prikryl Mar 06 '15 at 14:05
  • Okay, as suggested by Kayaman i wanted to look at the network traffic on tcp port 21 (using wireshark)..... and all of a sudden it doesn't disconnect anymore when connecting to the server from a remote device. As of this moment the problem only occurres when connecting to the port using "localhost" as hostname. Using "127.0.0.1" as host also works for some reason. Im happy about this.... but puzzled. Also because this is the first time in weeks it actually works. – DarkZero Mar 06 '15 at 14:57
  • Using the prototype code, i have been able to figure out that the disconnect is caused by a TCP RST Packet being send from the host machine to the client device. I am inclined to believe this is the same reason why the actual application (used to) fail. Does anyone have an idea why the machine might decide to send the TCP RST packet when sending "227 \r\n" over port 21? – DarkZero Mar 06 '15 at 16:40

0 Answers0