title: Getting com.mongodb.MongoSocketReadException: Prematurely reached end of stream- MongoDB
body: <p>I'm trying to connect my java application to MongoDB server. I'm using </p>
  
  <p>java 8,
  mongodb server 4.0.9 ,
  mongo-java-driver:3.10.0</p>
  
  <p>I am able to connect with my localhost server but getting following error when connecting to remote server which is ssl enabled by self signing. </p>
  
  <pre><code>INFO: Cluster created with settings {hosts=[host-name:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='300000 ms', maxWaitQueueSize=500}
  Jun 07, 2019 3:33:36 PM com.mongodb.diagnostics.logging.JULLogger log
  INFO: Cluster description not yet available. Waiting for 300000 ms before timing out
  Jun 07, 2019 3:33:37 PM com.mongodb.diagnostics.logging.JULLogger log
  INFO: Exception in monitor thread while connecting to server host-name:27017
      com.mongodb.MongoSocketReadException: Prematurely reached end of stream
      at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:112)
      at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:570)
      at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:441)
      at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295)
      at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
      at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83)
      at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33)
      at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:105)
      at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:62)
      at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
      at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
      at java.lang.Thread.run(Thread.java:748)
  Timed out after 300000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{host-name:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]
  
  </code></pre>
  
  <p>i have commented out  bind address in mongod.conf file.</p>
  
  <pre><code> System.setProperty("javax.net.ssl.trustStore", "path\\cacerts");
   System.setProperty("javax.net.ssl.trustStorePassword", "xxxx");
   CountDownLatch latch = new CountDownLatch(1);
   MongoCredential credential = MongoCredential.createCredential("username", 
                              "dbname","xxxx".toCharArray());
   MongoClientSettings settings = MongoClientSettings.builder()
     .credential(credential)
     .applyToSslSettings(ssl -&gt; { ssl.enabled(true); })
     .applyToClusterSettings(builder -&gt;
      builder.serverSelectionTimeout(1, TimeUnit.MINUTES)
     .hosts(Arrays.asList(new ServerAddress("host-address", 27017))))
     .build();
   MongoClient mongoClient = MongoClients.create(settings);
   MongoDatabase database = mongoClient.getDatabase("sample");
   MongoCollection&lt;Document&gt; collection = database.getCollection("data");
   System.out.println(collection.countDocuments());
  </code></pre>