Solved

How do I keep my connection from getting reset?

Posted on 2008-06-23
2
1,581 Views
Last Modified: 2013-12-29
I have a Java applet that uploads and downloads files to a server. With one file in particular, I'm always getting the same exception thrown:
java.net.SocketException:Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at...
    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
    at FTA.copyInputStream(FTA.java:1023)
    etc
and also
java.net.SocketException: Connection reset
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkEOF(Unknown Source)
    at...
    at FTA.DownloadZipFile(FTA.java:756)
    etc.
Caused by javax.net.ssl.SSLException: java.net.SocketException: Connection reset
    at...
    at FTA.copyInputStream(FTA.java:1023)
    at FTA.DownloadZipFile(FTA.java:754)
    ... 5 more
Caused by java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    ... 19 more
My question is: what does this mean (beyond the obvious) and how do I get my connection to not reset itself? Thanks.
// FTA.java
 
private String DownloadZipFile()
{
  File f = null;
  try 
  {
    URL u = new URL(Url);
    HttpURLConnection conn = (HttpURLConnection)u.openConnection();
 
    InputStream is = conn.getInputStream();
    f =  File.createTempFile("FTA", ".tmp");
    f.deleteOnExit();
    FileOutputStream fw = new FileOutputStream(f);
    int size = conn.getContentLength();
 
    BufferedOutputStream bos = new BufferedOutputStream( fw );
    copyInputStream(is, bos, size, true );     // line 754
 
    is.close();                                // line 756
    bos.close();
    fw.flush();	
    fw.close();
 
    return f.getAbsolutePath();
  }
  catch (SocketException sx)
  {
    System.err.println(sx.getMessage());
    sx.printStackTrace();
    return null;
  }
  finally {
    if(f != null) { f.delete(); }
  }
}
 
private void copyInputStream(InputStream in, OutputStream out, int totalLength, boolean showProgress) throws IOException
{
  try
  {
    byte[] buffer = new byte[1024];
    Date start = new Date();
 
    int bytesSent = 0;
    int len;                //line 1023 -- how is this throwing an exception???
 
    while ((len = in.read(buffer)) >= 0 && !shouldCancel)
    {
      bytesSent += len;
      out.flush();
    }
    out.flush();
  }
// We might need to try throwing these to the calling method.
  catch (SocketException sx)
  {
    System.err.println(sx.getMessage());
    sx.printStackTrace();
  }
  catch (IOException iox)
  {
    System.err.println(iox.getMessage());
    iox.printStackTrace();
  }
// there should be a finally clause right here to deal with the socket
// resetting the connection
}

Open in new window

0
Comment
Question by:tjgquicken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 21850185
I don't understand how copyInputStream is working *at all* - nothing is written to the OutputStream
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 21851263
You can't stop that, it is caused by the client disconnecting. eg. if they cancel the upload/download.
You should instead detect it occurring and handle it garcefully (by basically ignoring it and cancell up/download)

0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb message driven bean mdb creation steps 2 54
web project error add remove 1 80
American Express @Work site and Java 4 95
Java array 10 86
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question