a8595a792484 — QuintillusCFC 6 years ago
Wait for potential secondary/tertiary/etc. replies.  This happens if the reply is too big to fit in a single TCP frame.

Also restructure the code some to improve readability.  The core loops are still very unintuitive, but the rest is decent now.
M src/main/java/com/ajtjp/gopherarchiver/Gopher.java +48 -37
@@ 12,46 12,57 @@ import javax.net.SocketFactory;
  * @author Andrew
  */
 public class Gopher {
+    
+    static int bytesRead = 0;
+    
     public static void main(String[]args) {
         try {
-            SocketFactory.getDefault();
-            Socket s = new Socket("66.166.122.165", 70); //floodgap
-//            Socket s = new Socket("205.166.94.16", 70);   //SDF
-//            Socket s = new Socket("sdf.org", 70);
-            OutputStream os = s.getOutputStream();
-//            byte[] newline = new byte[2];
-//            newline[0] = 13;
-//            newline[1] = 10;
-            byte[] newline = new byte[3];
-            newline[0] = 49;
-            newline[1] = 13;
-            newline[2] = 10;
-            os.write("\r\n".getBytes());
-//            os.write("/gopher/proxy\r\n".getBytes());   //floodgap
-//            os.write("/users/sanjuro/indian-food\r\n".getBytes());
-//            os.write("/users/sanjuro/indian-food/aam-ki-launji.txt\r\n".getBytes());
-//            os.write("\r\n".getBytes());
-            os.flush();
-//            Thread.sleep(1000); //seems to need to wait for the server to respond...
-            InputStream is = s.getInputStream();
-            boolean replyArrived = false;
-            for (;;) {
-                if (is.available() == 0 && !replyArrived) {
-//                    System.out.println("zzz...");
-                    Thread.sleep(10);
-                    continue;
-                }
-                while (is.available() > 0) {
-                    byte[] buffer = new byte[is.available()];
-                    int actual = is.read(buffer);
-                    String valueRead = new String(buffer, "Windows-1252");
-                    System.out.println(valueRead);
-                }
-                break;
-            }
+            new Gopher();
         }
-        catch(IOException | InterruptedException ex) {
-            System.err.println("IO Exception :(");
+        catch(Exception ex) {
+            System.err.println("Ex: " + ex.getMessage());
         }
     }
+    
+    public Gopher() throws IOException, InterruptedException {
+        downloadPageContents(ReferenceURLs.floodgap);
+        
+        System.out.println("Read total of " + bytesRead + " bytes");
+    }
+    
+    private String downloadPageContents(GopherURL url) throws IOException, InterruptedException {
+        Socket s = new Socket(url.host, url.port);
+        OutputStream os = s.getOutputStream();
+        os.write((url.selector + "\r\n").getBytes());
+        os.flush();
+        InputStream is = s.getInputStream();
+        boolean replyArrived = false;
+        int timeSinceLastReply = 0;
+fetch:  for (;;) {
+            if (is.available() == 0 && !replyArrived) {
+//                    System.out.println("zzz...");
+                Thread.sleep(10);
+                continue;
+            }
+            while (is.available() > 0) {
+                byte[] buffer = new byte[is.available()];
+                int actual = is.read(buffer);
+                bytesRead+=actual;
+                String valueRead = new String(buffer, "Windows-1252");
+                System.out.println(valueRead);
+            }
+wait:       for (;;) {
+                Thread.sleep(10);
+                timeSinceLastReply+=10;
+                if (is.available() > 0) {
+                    continue fetch;
+                }
+                if (timeSinceLastReply > 1000) {
+                    break;
+                }
+            }
+            break;
+        }
+        return "";
+    }
 }

          
A => src/main/java/com/ajtjp/gopherarchiver/GopherURL.java +18 -0
@@ 0,0 1,18 @@ 
+
+package com.ajtjp.gopherarchiver;
+
+/**
+ *
+ * @author Andrew
+ */
+public class GopherURL {
+    String host;
+    int port;
+    String selector;
+
+    public GopherURL(String host, int port, String selector) {
+        this.host = host;
+        this.port = port;
+        this.selector = selector;
+    }
+}

          
A => src/main/java/com/ajtjp/gopherarchiver/ReferenceURLs.java +13 -0
@@ 0,0 1,13 @@ 
+
+package com.ajtjp.gopherarchiver;
+
+/**
+ * A few sample URLs.
+ * @author Andrew
+ */
+public class ReferenceURLs {
+
+    static GopherURL floodgap = new GopherURL("66.166.122.165", 70, "");
+    static GopherURL sdf = new GopherURL("sdf.org", 70, "");
+    static GopherURL indianRecipes = new GopherURL("sdf.org", 70, "/users/sanjuro/indian-food");
+}