Link to home
Start Free TrialLog in
Avatar of sangameshrh
sangameshrh

asked on

java.net.SocketException: Connection reset While using Apache HTTPClient

I am using HTTPClient for file upload.
The first time it is giving an exception like this

java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(Unknown Source)
        at java.io.BufferedInputStream.fill(Unknown Source)
        at java.io.BufferedInputStream.read(Unknown Source)
        at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:78)
        at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:106)
        at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1116)
        at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1973)
        at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1735)
        at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1098)
        at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
        at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
        at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
        at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
        at test.Test.actionPerformed(Test.java:459)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)


But when I do it again (In JFrame by pressing submit button again) its working fine.

What is the problem?

My code looks like

client.getHostConfiguration().setProxy(proxyHost,port);
if(userName!=null)
{
      HttpState httpState = new HttpState();
      httpState.setProxyCredentials(new AuthScope(proxySettings.proxyHost,port),new NTCredentials(proxySettings.userName,proxySettings.password,proxySettings.proxyHost,proxySettings.proxyDomain));
      filePost.setDoAuthentication(true);
      client.setState(httpState);
}
else
{
      filePost.setDoAuthentication(false);
}
client.getHttpConnectionManager().getParams().setConnectionTimeout(1000*60*60);
int status = client.executeMethod(filePost);

line no. 459(indicated in error trace)  is last line --->>int status = client.executeMethod(filePost);
Avatar of Mick Barry
Mick Barry
Flag of Australia image

the server is closing the connection for some reason, check the server logs.
It means that the server for some reason closes the connection while the client is still reading from the socket. If you've got access to the server logs, take a look in there to see if you find an indication about what is happening.
Avatar of sangameshrh
sangameshrh

ASKER

I am using Apache Tomcat 6.0 app server.
There I have checked for these files
stdout.log
localhost.log
Catalina.log
stderr.log
 and unable to find any traces of it.
check whatever you are using to recieve the upload
Its a servlet for receiving file upload & the code looks like

ServletFileUpload            upload            =      new ServletFileUpload();
FileItemIterator            iter            =      upload.getItemIterator(request);
FileItemStream            fi            =      iter.next();
fileName                              =      fi.getName();
Streams.copy(fi.openStream(), new BufferedOutputStream(new FileOutputStream(new File("c:\\upload\\temp\\", fileName)),1024*1024*10), true, new byte[(1 << 20)*10]);

and there sre some lines for database connection to MySQL.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well in that case it should not work for the second time also.
The servlet may see a new request. You need to make the alteration in case you need to avoid a null pointer exception too
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial