Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: SSL issue: java.net.SocketException: Broken pipe

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default SSL issue: java.net.SocketException: Broken pipe

    I am trying to write simple class where I try to hit google.com, but I get java.net.SocketException: Broken pipe error. I have tried to run the code below on different Java versions: 8, 14, 15, but unfortunately I get the same error irrespective of Java version.

    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSocketFactory;
    import javax.net.ssl.TrustManagerFactory;
    import javax.net.ssl.HttpsURLConnection;
    import java.io.*;
    import java.net.*;
    import java.sql.SQLException;
    import java.security.KeyStore;
     
    public class Test{
     
      /**
      * Utility method for getting object from url
      */
    public static void main(String[] args){
              String ret = "success";
        try {
          String urlString;
     
          //Oh my god this is stupid!
          //https://stackoverflow.com/a/25735202
          URL url = new URL("https://www.google.com");
          URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
          urlString=uri.toASCIIString();
          url = new URL(urlString);
          HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
          con.setConnectTimeout(60000);
          con.setReadTimeout(60000);
     
          InputStream inputStream = con.getInputStream();
          inputStream.close();
          con.disconnect();
     
        } catch (InvalidClassException e)  {
          ret = "Invalid class: "+e.getMessage();
        } catch (MalformedURLException e) {
          ret = "Malformed URL "+e.getMessage();
        } catch (SocketTimeoutException e) {
          ret = "Timeout "+e.getMessage();
        } catch (UnknownHostException e) {
        } catch (IOException e) {
          ret = "Error in reading from (possible timeout)";
        } catch (URISyntaxException e) {
          ret = "URI syntax error "+e.getMessage();
        } catch (Exception e) {
          ret = "Getting data from URL "+e.getMessage();
        }
     
              System.out.println(ret);
      }
    }
    Last edited by dom.atom; December 1st, 2020 at 04:41 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: SSL issue: java.net.SocketException: Broken pipe

    Can you add a call to the printStackTrace method to get a fuller error message?

    There is a local path in the code that makes it impossible to test.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: SSL issue: java.net.SocketException: Broken pipe

    I have removed local path, it's not needed to repro the issue.

    This is the debug output for handshake:

    javax.net.ssl|DEBUG|01|main|2020-12-01 14:28:01.468 CET|SSLSocketOutputRecord.java:258|WRITE: TLS13 handshake, length = 409
    javax.net.ssl|DEBUG|01|main|2020-12-01 14:28:01.469 CET|SSLSocketOutputRecord.java:272|Raw write (
    0000: 16 03 03 01 99 01 00 01 95 03 03 24 2D 02 9C 07 ...........$-...
    0010: CB 24 22 CD 09 4F B0 8A F1 57 08 BC C3 A6 8C BC .$"..O...W......
    0020: A7 10 CC 77 93 18 2A CB 84 A9 FF 20 E7 11 2C 9F ...w..*.... ..,.
    0030: 2D A4 DE E5 9A 39 18 F6 7D A4 6F EB B5 4E 3A F4 -....9....o..N:.
    0040: 67 09 7E 9D 45 5B 05 C7 FE D8 F6 47 00 62 13 02 g...E[.....G.b..
    0050: 13 01 13 03 C0 2C C0 2B CC A9 C0 30 CC A8 C0 2F .....,.+...0.../
    0060: 00 9F CC AA 00 A3 00 9E 00 A2 C0 24 C0 28 C0 23 ...........$.(.#
    0070: C0 27 00 6B 00 6A 00 67 00 40 C0 2E C0 32 C0 2D .'.k.j.g.@...2.-
    0080: C0 31 C0 26 C0 2A C0 25 C0 29 C0 0A C0 14 C0 09 .1.&.*.%.)......
    0090: C0 13 00 39 00 38 00 33 00 32 C0 05 C0 0F C0 04 ...9.8.3.2......
    00A0: C0 0E 00 9D 00 9C 00 3D 00 3C 00 35 00 2F 00 FF .......=.<.5./..
    00B0: 01 00 00 EA 00 00 00 13 00 11 00 00 0E 77 77 77 .............www
    00C0: 2E 67 6F 6F 67 6C 65 2E 63 6F 6D 00 05 00 05 01 .google.com.....
    00D0: 00 00 00 00 00 0A 00 16 00 14 00 1D 00 17 00 18 ................
    00E0: 00 19 00 1E 01 00 01 01 01 02 01 03 01 04 00 0B ................
    00F0: 00 02 01 00 00 0D 00 28 00 26 04 03 05 03 06 03 .......(.&......
    0100: 08 04 08 05 08 06 08 09 08 0A 08 0B 04 01 05 01 ................
    0110: 06 01 04 02 03 03 03 01 03 02 02 03 02 01 02 02 ................
    0120: 00 32 00 28 00 26 04 03 05 03 06 03 08 04 08 05 .2.(.&..........
    0130: 08 06 08 09 08 0A 08 0B 04 01 05 01 06 01 04 02 ................
    0140: 03 03 03 01 03 02 02 03 02 01 02 02 00 11 00 09 ................
    0150: 00 07 02 00 04 00 00 00 00 00 17 00 00 00 23 00 ..............#.
    0160: 00 00 2B 00 09 08 03 04 03 03 03 02 03 01 00 2D ..+............-
    0170: 00 02 01 01 00 33 00 26 00 24 00 1D 00 20 DF 79 .....3.&.$... .y
    0180: 45 AD D3 E0 94 7E 25 2B 21 F9 89 5F AC 1D FD C6 E.....%+!.._....
    0190: D0 36 42 1C 80 78 83 48 DC 0A CC AA 0D 4B .6B..x.H.....K
    )
    javax.net.ssl|ERROR|01|main|2020-12-01 14:28:01.507 CET|TransportContext.java:361|Fatal (UNEXPECTED_MESSAGE): Connection reset (
    "throwable" : {
    java.net.SocketException: Connection reset
    at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.ja va:323)
    at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:3 50)
    at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java :803)
    at java.base/java.net.Socket$SocketInputStream.read(Socket.java :981)
    at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSock etInputRecord.java:478)
    at java.base/sun.security.ssl.SSLSocketInputRecord.readHeader(S SLSocketInputRecord.java:472)
    at java.base/sun.security.ssl.SSLSocketInputRecord.decode(SSLSo cketInputRecord.java:160)
    at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport. java:110)
    at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImp l.java:1488)
    at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord (SSLSocketImpl.java:1394)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLS ocketImpl.java:441)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLS ocketImpl.java:412)
    at java.base/sun.net.http://www.protocol.https.HttpsClien...ient.java:567)
    at java.base/sun.net.http://www.protocol.https.AbstractDe...tion.java:183)
    at java.base/sun.net.http://www.protocol.http.HttpURLConn...ion.java:1600)
    at java.base/sun.net.http://www.protocol.http.HttpURLConn...ion.java:1528)
    at java.base/sun.net.http://www.protocol.https.HttpsURLCo...Impl.java:224)
    at Test.main(Test.java:30)}

    )
    javax.net.ssl|ALL|01|main|2020-12-01 14:28:01.508 CET|SSLSessionImpl.java:1224|Invalidated session: Session(1606829281290|SSL_NULL_WITH_NULL_NULL)
    javax.net.ssl|DEBUG|01|main|2020-12-01 14:28:01.508 CET|SSLSocketOutputRecord.java:72|WRITE: TLS13 alert(unexpected_message), length = 2
    javax.net.ssl|WARNING|01|main|2020-12-01 14:28:01.509 CET|TransportContext.java:403|Fatal: failed to send fatal alert UNEXPECTED_MESSAGE (
    "throwable" : {
    java.net.SocketException: Broken pipe
    at java.base/sun.nio.ch.NioSocketImpl.implWrite(NioSocketImpl.j ava:420)
    at java.base/sun.nio.ch.NioSocketImpl.write(NioSocketImpl.java: 440)
    at java.base/sun.nio.ch.NioSocketImpl$2.write(NioSocketImpl.jav a:826)
    at java.base/java.net.Socket$SocketOutputStream.write(Socket.ja va:1051)
    at java.base/sun.security.ssl.SSLSocketOutputRecord.encodeAlert (SSLSocketOutputRecord.java:82)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportC ontext.java:400)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportC ontext.java:312)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportC ontext.java:307)
    at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport. java:144)
    at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImp l.java:1488)
    at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord (SSLSocketImpl.java:1394)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLS ocketImpl.java:441)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLS ocketImpl.java:412)
    at java.base/sun.net.http://www.protocol.https.HttpsClien...ient.java:567)
    at java.base/sun.net.http://www.protocol.https.AbstractDe...tion.java:183)
    at java.base/sun.net.http://www.protocol.http.HttpURLConn...ion.java:1600)
    at java.base/sun.net.http://www.protocol.http.HttpURLConn...ion.java:1528)
    at java.base/sun.net.http://www.protocol.https.HttpsURLCo...Impl.java:224)
    at Test.main(Test.java:30)}

    )
    javax.net.ssl|DEBUG|01|main|2020-12-01 14:28:01.509 CET|SSLSocketImpl.java:1727|close the underlying socket
    javax.net.ssl|DEBUG|01|main|2020-12-01 14:28:01.509 CET|SSLSocketImpl.java:1746|close the SSL connection (initiative)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: SSL issue: java.net.SocketException: Broken pipe

    I have removed local path
    Could you post the new code?

    When I comment out this line:
    // con.setSSLSocketFactory(createSocketFactory("/home/user/google.jks", "password")); //????
    plus the catch block that goes with it
    I get this printed: success
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: SSL issue: java.net.SocketException: Broken pipe

    I have updated the code in original post. However, I just tried to run the code on other machine which is Win with jdk1.8.0_261 and it went through successfully. So I guess the issue is with JDK/OpenJDK on my RHEL-7 machines where I can repro the issue.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: SSL issue: java.net.SocketException: Broken pipe

    it went through successfully.
    Ok, so you have it working with java.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: SSL issue: java.net.SocketException: Broken pipe

    Found the issue - it was the proxy server that was refusing SSL connection

Similar Threads

  1. [SOLVED] java.net.SocketException: Connection reset
    By aussiemcgr in forum Java Networking
    Replies: 2
    Last Post: January 22nd, 2020, 01:36 PM
  2. Replies: 0
    Last Post: November 22nd, 2012, 09:56 AM
  3. ClientAbortException java.net.socketException brokenPipe
    By Sethupathy in forum Member Introductions
    Replies: 0
    Last Post: October 28th, 2012, 01:12 AM
  4. send image file from java to php broken pipe exception occured
    By ashi123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2011, 02:53 PM