From e34e14ea3a024862c9ffd557f322d7720882a515 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 8 Aug 2008 20:04:36 +0000 Subject: Client: Fixed connection reset problem The problem with NMAP freaking out the Client was that NMAP didn't actually send any data to the socket and thus the connection reset and the Client wasn't actually programmed to handle that yet. Fixes #24 --- .../com/fourisland/instadisc/InstaDiscThread.java | 59 ++++++++++++------ .../instadisc/Item/SubscriptionFile.java | 69 +++++++++++++++------- 2 files changed, 87 insertions(+), 41 deletions(-) (limited to 'client/trunk/src/com') diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java b/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java index 5895c10..07b5b28 100644 --- a/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java +++ b/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java @@ -27,26 +27,32 @@ public class InstaDiscThread implements Runnable { } public void run() { - try { + try + { ServerSocket svr = new ServerSocket(); java.net.InetSocketAddress addr = new java.net.InetSocketAddress(4444); svr.bind(addr); Runtime.getRuntime().addShutdownHook(new Thread(new CloseServerSocketThread(svr))); - while (!cancelled) { - try { + while (!cancelled) + { + try + { Socket s = svr.accept(); HandleItemThread hit = new HandleItemThread(s); Thread hitt = new Thread(hit); hitt.start(); - } catch (SocketException ex) { + } catch (SocketException ex) + { cancel(); - } catch (Exception ex) { + } catch (Exception ex) + { cancel(); Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex); } } svr.close(); - } catch (IOException ex) { + } catch (IOException ex) + { Logger.getLogger(InstaDiscThread.class.getName()).log(Level.SEVERE, null, ex); } @@ -56,62 +62,77 @@ public class InstaDiscThread implements Runnable { class HandleItemThread implements Runnable { Socket s; - + public HandleItemThread(Socket s) { this.s = s; } public void run() { - try { + try + { InputStream is = s.getInputStream(); int buffer[] = new int[1000]; int rs = 0; int i = 0; - while (rs != -1) { - try { + while (rs != -1) + { + try + { rs = is.read(); - if (rs != -1) { + if (rs != -1) + { buffer[i] = rs; } i++; - } catch (IOException ex) { + } catch (SocketException ex) + { + return; + } catch (IOException ex) + { Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); } } StringBuilder result = new StringBuilder(); int j = 0; - for (j = 0; j < i; j++) { + for (j = 0; j < i; j++) + { result.append(Character.toString((char) buffer[j])); } String[] headers = result.toString().split("\n"); HashMap headerMap = new HashMap(); i = 0; - while (1 == 1) { - try { + while (1 == 1) + { + try + { String[] nameVal = headers[i].split(": "); String name = nameVal[0]; String value = nameVal[1].trim().replace("__INSTADISC__", ": "); headerMap.put(name, value); - } catch (Exception ex) { + } catch (Exception ex) + { break; } i++; } //Logger.getLogger(HandleItemThread.class.getName()).log(Level.INFO, headerMap.toString()); - try { + try + { s.close(); - } catch (IOException ex) { + } catch (IOException ex) + { Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); } Item idI = new Item(headerMap); idI.start(); - } catch (IOException ex) { + } catch (IOException ex) + { Logger.getLogger(HandleItemThread.class.getName()).log(Level.SEVERE, null, ex); } } diff --git a/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java b/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java index fc91eaf..d9ed348 100644 --- a/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java +++ b/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java @@ -28,19 +28,23 @@ public class SubscriptionFile { public SubscriptionFile(URL url, JLabel status) { status.setText("Checking...."); - try { + try + { HttpURLConnection urc = (HttpURLConnection) url.openConnection(); Thread th = new Thread(new SubscriptionFileThread(urc, status)); th.start(); - } catch (FileNotFoundException ex) { + } catch (FileNotFoundException ex) + { status.setText("Error: Subscription File doesn't exist"); - } catch (IOException ex) { + } catch (IOException ex) + { Logger.getLogger(SubscriptionFile.class.getName()).log(Level.SEVERE, null, ex); } } public static void deleteSubscription(Subscription s, boolean deleteFromData) { - if (deleteFromData) { + if (deleteFromData) + { Wrapper.deleteSubscription(s.getURL()); } @@ -50,8 +54,10 @@ public class SubscriptionFile { int i = 0; Filter f[] = Wrapper.getAllFilter(); - for (i = 0; i < f.length; i++) { - if (f[i].getSubscription().equals(s.getURL())) { + for (i = 0; i < f.length; i++) + { + if (f[i].getSubscription().equals(s.getURL())) + { Wrapper.deleteFilter(f[i].getID()); } } @@ -70,49 +76,61 @@ class SubscriptionFileThread implements Runnable { public void run() { InputStream is = null; - try { + try + { is = urc.getInputStream(); int[] buffer = new int[1000]; int rs = 0; int i = 0; - while (rs != -1) { - try { + while (rs != -1) + { + try + { rs = is.read(); - if (rs != -1) { + if (rs != -1) + { buffer[i] = rs; } i++; - } catch (IOException ex) { + } catch (IOException ex) + { Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex); } } StringBuilder result = new StringBuilder(); int j = 0; - for (j = 0; j < i; j++) { + for (j = 0; j < i; j++) + { result.append(Character.toString((char) buffer[j])); } String[] headers = result.toString().split("\n"); HashMap headerMap = new HashMap(); i = 0; - while (1 == 1) { - try { + while (1 == 1) + { + try + { String[] nameVal = headers[i].split(": "); String name = nameVal[0]; String value = nameVal[1].trim(); headerMap.put(name, value); - } catch (Exception ex) { + } catch (Exception ex) + { break; } i++; } - if (headerMap.containsKey("Subscription")) { - if (headerMap.containsKey("Title")) { - if (headerMap.containsKey("Category")) { + if (headerMap.containsKey("Subscription")) + { + if (headerMap.containsKey("Title")) + { + if (headerMap.containsKey("Category")) + { Subscription s = new Subscription(); s.setURL(headerMap.get("Subscription")); s.setTitle(headerMap.get("Title")); @@ -134,12 +152,19 @@ class SubscriptionFileThread implements Runnable { } else { status.setText("Error: Subscription file is not well-formed"); } - } catch (IOException ex) { + } catch (FileNotFoundException ex) + { + status.setText("Error: Subscription File doesn't exist"); + } catch (IOException ex) + { Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex); - } finally { - try { + } finally + { + try + { is.close(); - } catch (IOException ex) { + } catch (IOException ex) + { Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex); } } -- cgit 1.4.1