title: Java: How to properly close a socket connection using ServerSocket and Socket
body: <p>I currently have a simple instant messaging program which is utilizing Java's <a href="http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html" rel="nofollow">Socket</a> and <a href="http://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html" rel="nofollow">ServerSocket</a> classes.  It is functioning as intended but when I attempt to close the connection it is not using the 4 way handshake TCP teardown to close the connection.  Instead it is closing the connection abruptly with an RST packet.</p>
  
  <p>The way in which I am closing the connection is sending a string from the client to the server which the server will recognize as the command to close the connection.  I then use the <code>ServerSocket.close()</code> method on the server and the <code>Socket.close()</code> method on the client. </p>
  
  <p>What is the correct way and/or order of events to properly close a TCP connection utilizing these classes?</p>
  
  <p>Client side disconnect code:</p>
  
  <pre><code>//Disconnects from remote server
  //Returns true on success, false on failure
  public boolean disconnect(){
      try{
          this.clientOut.println("0x000000");
          this.clientRemoteSocket.close();
          this.isConnected = false;
          return true;
      }catch(Exception e){
          return false;
      }
  }
  </code></pre>
  
  <p>Server side disconnect code:</p>
  
  <pre><code>//Check to see if the client wants to close the connection
  //If yes, then close the connection and break out of the while loop
  if(incoming.equals("0x000000")){
      serverLocalSocket.close();
      break;
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  The code works perfectly fine. I'm just trying to learn socket programming in Java and know that a proper TCP teardown process is to include a 4 way handshake. A FIN packet to the remote host, then an ACK packet from the remote host back.  Then a FIN packet from the remote host, then an ACK packet to the remote host.  When monitoring the traffic via Wireshark I am not getting that. Instead I am getting a FIN to the remote server, then a RST/ACK back from the server.</p>
  
  <p>This <a href="https://images.search.yahoo.com/images/view;_ylt=AwrB8pBX0UZVRSQAGAAunIlQ;_ylu=X3oDMTIyNmdzazAzBHNlYwNzcgRzbGsDaW1nBG9pZAM0MmYyNzMyMDRjYWRjZmM4YjJmMWZiYzk0MGJmZDIzNgRncG9zAzEEaXQDYmluZw--?.origin=&amp;back=https%3A%2F%2Fimages.search.yahoo.com%2Fyhs%2Fsearch%3Fp%3Dtcp%2Bteardown%2Bsequence%26fr%3Dyhs-mozilla-002%26fr2%3Dpiv-web%26hsimp%3Dyhs-002%26hspart%3Dmozilla%26tab%3Dorganic%26ri%3D1&amp;w=2000&amp;h=1600&amp;imgurl=upload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F5%2F55%2FTCP_CLOSE.svg%2F2000px-TCP_CLOSE.svg.png&amp;rurl=http%3A%2F%2Fhackingsoftware41.blogspot.com%2F2014%2F10%2Finfo-back-to-basic-ketahui-perkara-asas.html&amp;size=117.6KB&amp;name=info]%20Back%20to%20Basic-%20Ketahui%20perkara%20asas%20dalam%20jaringan%20internet%20...&amp;p=tcp%20teardown%20sequence&amp;oid=42f273204cadcfc8b2f1fbc940bfd236&amp;fr2=piv-web&amp;fr=yhs-mozilla-002&amp;tt=info]%20Back%20to%20Basic-%20Ketahui%20perkara%20asas%20dalam%20jaringan%20internet%20...&amp;b=0&amp;ni=21&amp;no=1&amp;ts=&amp;tab=organic&amp;sigr=12qqsq9vq&amp;sigb=14fcblnoq&amp;sigi=12obn0uc3&amp;sigt=125pat5l6&amp;sign=125pat5l6&amp;.crumb=Y4qaq3O/9bF&amp;fr=yhs-mozilla-002&amp;fr2=piv-web&amp;hsimp=yhs-002&amp;hspart=mozilla" rel="nofollow">image</a> depicts a proper TCP 4 way teardown process.</p>
  
  <p>So far everything I've found suggest that all one needs is a call to close() or to just let Java's Try-with-resources statement handle the clean up.  I can't see Java implementing functionality which does not comply with the standard TCP specifications though.  It is very possible I may be calling certain lines in an incorrect order or something of the sort, I'm just unaware of it.</p>