title: MongoDB Prematurely reached end of stream
body: <p>I get a Prematurely reached end of stream error when trying to connect to MongoDB Atlas Free Tier.  Using Java 1.8 and Mongo Driver 3.8.0. Does this have any thing to do with the Cluster setup in Atlas? I got the same program working with my MongoDB localhost.</p>
  
  <p>Any Idea?</p>
  
  <p>Java Test class</p>
  
  <pre><code>import org.bson.Document;
  import com.mongodb.MongoClient;
  import com.mongodb.MongoClientURI;
  import com.mongodb.client.MongoCollection;
  import com.mongodb.client.MongoDatabase;
  
  public class MongoTest {
  
  public static void main(String args[]) {
  
      MongoClientURI uri = new MongoClientURI(
              "mongodb://username:password@cluster0-****.mongodb.net");
  
      MongoClient mongoClient = new MongoClient(uri);
      MongoDatabase database = mongoClient.getDatabase("account");
  
      System.out.println("Hello");
  
      MongoCollection&lt;Document&gt; collection = database.getCollection("users");
  
      System.out.println(collection.count());
  
      mongoClient.close();
  
    }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>    Jul 15, 2018 12:32:04 PM com.mongodb.diagnostics.logging.JULLogger log
  INFO: Cluster created with settings {hosts=[cluster0-****.mongodb.net:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
  Hello
  Jul 15, 2018 12:32:04 PM com.mongodb.diagnostics.logging.JULLogger log
  INFO: Cluster description not yet available. Waiting for 30000 ms before timing out
  Jul 15, 2018 12:32:04 PM com.mongodb.diagnostics.logging.JULLogger log
  INFO: Exception in monitor thread while connecting to server cluster0-****.mongodb.net:27017
  com.mongodb.MongoSocketReadException: Prematurely reached end of stream
  at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:92)
  at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:554)
  at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:425)
  at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:289)
  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:106)
  at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:63)
  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)
  
  Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=cluster0-****.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]
  at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:179)
  at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:41)
  at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:136)
  at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:94)
  at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:249)
  at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:172)
  at com.mongodb.client.internal.MongoCollectionImpl.executeCount(MongoCollectionImpl.java:248)
  at com.mongodb.client.internal.MongoCollectionImpl.count(MongoCollectionImpl.java:183)
  at com.mongodb.client.internal.MongoCollectionImpl.count(MongoCollectionImpl.java:171)
  at MongoTest.main(MongoTest.java:21)
  </code></pre>