Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

128
LINES

< > BotCompany Repo | #1001647 // Android web server (port 8888), serving a movie

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (2031L/17K/40K).

!636

import java.util.*;
import java.io.*;
import java.net.*;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;

import javax.net.ssl.*;

main {
static class MyHTTPD extends NanoHTTPD {

     /**
     * Constructs an HTTP server on given port.
     */
    public MyHTTPD(int port) throws IOException {
        super(port);
    }


@Override
    public Response serve( String uri, Method method,
            Map<String, String> header, Map<String, String> parms,
            Map<String, String> files )
    {
        System.out.println( method + " '222" + uri + "' " );

      String ip = header.get("remote-addr");
      if (!ip.equals("127.0.0.1"))
        peers.add(ip);

      if (uri.equals("/k.mp4"))
        return serveBinary("/storage/sdcard0/k.mp4");


        String msg = "<html><body><h1>Hello server</h1>\n";
        if ( parms.get("username") == null )
            msg +=
                "<form action='?' method='get'>\n" +
                "  <p>Your name: <input type='text' name='username'></p>\n" +
                "</form>\n";
        else
            msg += "<p>Hello, " + parms.get("username") + "!</p>";

        msg += "uri: " + uri + "<br>";
        new List<String> links;
        for (String p : peers)
          links.add("<a href=\"http://" + p + ":8888\">" + p + "</a>");
        msg += "All known peers: " + join(", ", links);
        msg += "</body></html>\n";
        return newFixedLengthResponse(msg);
    }

static String join(String glue, Iterable<String> list) {
  new StringBuilder buf;
  boolean first = true;
  for (String s : list) {
    if (first) first = false;
    else buf.append(glue);
    buf.append(s);
  }
  return buf.toString();
}

    Response serveBinary(String path) {
      System.out.println("Serving " + path);
      long length = new File(path).length();
      System.out.println("File size: " + length);
      FileInputStream fis = null;
    try {
        fis = new FileInputStream(path);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new Response(Status.OK, "application/binary", fis, length);
  }
}

  private static MyHTTPD server;
  private static int port = 8888;

  static new (Tree)Set<String> peers;
  
  psvm {
          server = new MyHTTPD(port);
          server.start();
          System.out.println("HTTP server started (listening on port " + port + "!)");
          printMyIPs();
          server.join(); // keep main thread alive
  }
  
  static void printMyIPs() {
  String ip;
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface iface = interfaces.nextElement();
            // filters out 127.0.0.1 and inactive interfaces
            if (iface.isLoopback() || !iface.isUp())
                continue;

            Enumeration<InetAddress> addresses = iface.getInetAddresses();
            while(addresses.hasMoreElements()) {
                InetAddress addr = addresses.nextElement();
                if (addr instanceof Inet4Address) { // we still stick to the old stuff... until the new stuff comes along!
                  ip = addr.getHostAddress();
                  System.out.println("\nPlease visit http://" + ip + ":" +  port + " to see me!");
                }
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
  }
}

!include #1000433 // class NanoHTTP

Author comment

Began life as a copy of #1000596

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cahelewubzku, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001647
Snippet name: Android web server (port 8888), serving a movie
Eternal ID of this version: #1001647/1
Text MD5: e5268d2163e703e8ba56f8f4370ff026
Transpilation MD5: 2e56061d892245850bc4dfe58abe0a95
Author: stefan
Category: javax android
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-31 22:04:28
Source code size: 3901 bytes / 128 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 694 / 664
Referenced in: [show references]