Close the socket correctly.
[xudocci.git] / src / main / java / net / pterodactylus / irc / Connection.java
index ec063f6..336fdae 100644 (file)
@@ -35,6 +35,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.net.SocketFactory;
@@ -325,7 +326,9 @@ public class Connection extends AbstractExecutionThreadService implements Servic
         *              if an I/O error occurs
         */
        public void close() throws IOException {
-               connectionHandler.close();
+               if (connectionHandler != null) {
+                       connectionHandler.close();
+               }
        }
 
        //
@@ -344,6 +347,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                /* connect to remote socket. */
                try {
                        Socket socket = socketFactory.createSocket(hostname, port);
+                       socket.setSoTimeout((int) TimeUnit.MINUTES.toMillis(3));
                        connectionHandler = new ConnectionHandler(socket.getInputStream(), socket.getOutputStream());
 
                        /* register connection. */
@@ -744,6 +748,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                public void close() throws IOException {
                        Closeables.close(outputStream, true);
                        Closeables.close(inputStreamReader, true);
+                       Closeables.close(inputStream, true);
                }
 
        }