title: Time out on sending to a server socket
body: <pre><code> public void sendToServer(String fileToSend, String ip, int sendPort)
  {
      int port = sendPort;
      String url = ip;
      File file = new File(fileToSend);
      String fileName = file.getName();
  
      Socket sock;
      try {
          sock = new Socket(url,port);
  
          //Send the file name
          OutputStream socketStream = sock.getOutputStream();
          ObjectOutput objectOutput = new ObjectOutputStream(socketStream);
          objectOutput.writeObject(fileName);
  
          //Send File
          byte [] mybytearray  = new byte [(int)file.length()];
          FileInputStream fis = new FileInputStream(file);
          BufferedInputStream bis = new BufferedInputStream(fis);
          bis.read(mybytearray,0,mybytearray.length);
          OutputStream os = sock.getOutputStream();
          os.write(mybytearray,0,mybytearray.length);
          fileSentOkay();
          os.flush();
          sock.close();
  
      } catch (UnknownHostException e) {
          hostNotFound();
      } catch (IOException e) {
          hostNotFound();
      }
  }
  </code></pre>
  
  <p>When I try to send something to the server when the server isn't listening for the connection, the phone keeps attempting to send the file. As a result of this, my Android program will eventually force close. </p>
  
  <p>How could I set a time out for this to happen? Would I have to use something like <code>setSoTimeout()</code> on the socket that is sending the data?</p>