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

131
LINES

< > BotCompany Repo | #1000437 // Android web server port 8888, extending web interface

JavaX source code (Android) [tags: use-pretranspiled] - run with: the app

Libraryless. Click here for Pure Java version (1973L/16K/39K).

!636
!quicknew

import android.widget.*;
import android.view.*;
import android.content.Context;
import android.app.Activity;
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("/movie.avi"))
        return serveBinary("/sdcard/Download/fal.avi");


        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();
                ip = addr.getHostAddress();
                System.out.println(iface.getDisplayName() + " " + ip);
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
  }
}

!include #1000433 // class NanoHTTP

Author comment

Began life as a copy of #1000436

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, exkalrxbqyxc, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, mrjhfnjfopze, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000437
Snippet name: Android web server port 8888, extending web interface
Eternal ID of this version: #1000437/1
Text MD5: 79a15e971048c3c2558ad0ca465b65ff
Transpilation MD5: db65a11fa7bcdf658a0c276ea65c8e69
Author: stefan
Category: javax android
Type: JavaX source code (Android)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-06 05:09:16
Source code size: 3858 bytes / 131 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 815 / 956
Referenced in: #1000596 - Cross-platform web server (port 8888), serving another movie
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)