title: Is it possible in android to transfer file over http using java socket program?
body: <p>I'm creating an application to send a file from an android device to another device using http socket program. Here's the code,
  Server Side</p>
  
  <pre><code>try {
              ServerSocket servsock = new ServerSocket(4042);
              File myFile = new File(pathToFile);
              while (true) {
                  Socket sock = servsock.accept();
                  byte[] mybytearray = new byte[(int) myFile.length()];
                  BufferedInputStream bis = new BufferedInputStream(
                          new FileInputStream(myFile));
                  bis.read(mybytearray, 0, mybytearray.length);
                  OutputStream os = sock.getOutputStream();
                  os.write(mybytearray, 0, mybytearray.length);
                  os.flush();
                  bis.close();
                  sock.close();
  
              }
          } catch (Exception e) {
              Toast.makeText(MyActivity.context,
                      "Failed to send file!", Toast.LENGTH_SHORT)
                      .show();
          }
  </code></pre>
  
  <p>And the client side,</p>
  
  <pre><code>try {
                  Socket sock = new Socket(ipAddress, 4042);
                  byte[] mybytearray = new byte[1024];
                  InputStream is = sock.getInputStream();
                  FileOutputStream fos = new FileOutputStream("/mnt/sdcard/"
                          +fileName);
                  BufferedOutputStream bos = new BufferedOutputStream(fos);
                  int bytesRead = is.read(mybytearray, 0, mybytearray.length);
                  bos.write(mybytearray, 0, bytesRead);
                  bos.close();
                  sock.close();
              } catch (Exception e) {
                  Toast.makeText(MyActivity.context,
                          "Failed to download the file!", Toast.LENGTH_LONG)
                          .show();
              }
  </code></pre>
  
  <p>Both these codes are in an AsyncTask class. But client side is always getting timed out.
  Is this the right way to transfer a file over Internet in android? If so, please tell me where I'm doing wrong.
  Thanks in advance.</p>
  
  <p>EDIT : Here, the file needs to be send in Server side and Client is trying to download that file.</p>