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).

1  
!636
2  
!quicknew
3  
4  
import android.widget.*;
5  
import android.view.*;
6  
import android.content.Context;
7  
import android.app.Activity;
8  
import java.util.*;
9  
import java.io.*;
10  
import java.net.*;
11  
12  
import java.nio.ByteBuffer;
13  
import java.nio.channels.FileChannel;
14  
import java.nio.charset.Charset;
15  
import java.security.KeyStore;
16  
import java.text.SimpleDateFormat;
17  
import java.util.logging.Level;
18  
import java.util.logging.Logger;
19  
import java.util.regex.Matcher;
20  
import java.util.regex.Pattern;
21  
import java.util.zip.GZIPOutputStream;
22  
23  
import javax.net.ssl.*;
24  
25  
main {
26  
static class MyHTTPD extends NanoHTTPD {
27  
28  
     /**
29  
     * Constructs an HTTP server on given port.
30  
     */
31  
    public MyHTTPD(int port) throws IOException {
32  
        super(port);
33  
    }
34  
35  
36  
@Override
37  
    public Response serve( String uri, Method method,
38  
            Map<String, String> header, Map<String, String> parms,
39  
            Map<String, String> files )
40  
    {
41  
        System.out.println( method + " '222" + uri + "' " );
42  
43  
      String ip = header.get("remote-addr");
44  
      if (!ip.equals("127.0.0.1"))
45  
        peers.add(ip);
46  
47  
      if (uri.equals("/movie.avi"))
48  
        return serveBinary("/sdcard/Download/fal.avi");
49  
50  
51  
        String msg = "<html><body><h1>Hello server</h1>\n";
52  
        if ( parms.get("username") == null )
53  
            msg +=
54  
                "<form action='?' method='get'>\n" +
55  
                "  <p>Your name: <input type='text' name='username'></p>\n" +
56  
                "</form>\n";
57  
        else
58  
            msg += "<p>Hello, " + parms.get("username") + "!</p>";
59  
60  
        msg += "uri: " + uri + "<br>";
61  
        new List<String> links;
62  
        for (String p : peers)
63  
          links.add("<a href=\"http://" + p + ":8888\">" + p + "</a>");
64  
        msg += "All known peers: " + join(", ", links);
65  
        msg += "</body></html>\n";
66  
        return newFixedLengthResponse(msg);
67  
    }
68  
69  
static String join(String glue, Iterable<String> list) {
70  
  new StringBuilder buf;
71  
  boolean first = true;
72  
  for (String s : list) {
73  
    if (first) first = false;
74  
    else buf.append(glue);
75  
    buf.append(s);
76  
  }
77  
  return buf.toString();
78  
}
79  
80  
    Response serveBinary(String path) {
81  
      System.out.println("Serving " + path);
82  
      long length = new File(path).length();
83  
      System.out.println("File size: " + length);
84  
      FileInputStream fis = null;
85  
    try {
86  
        fis = new FileInputStream(path);
87  
    } catch (FileNotFoundException e) {
88  
        // TODO Auto-generated catch block
89  
        e.printStackTrace();
90  
    }
91  
    return new Response(Status.OK, "application/binary", fis, length);
92  
  }
93  
}
94  
95  
  private static MyHTTPD server;
96  
  private static int port = 8888;
97  
98  
  static new (Tree)Set<String> peers;
99  
  
100  
  psvm {
101  
          server = new MyHTTPD(port);
102  
          server.start();
103  
          System.out.println("HTTP server started (listening on port " + port + "!)");
104  
          printMyIPs();
105  
          server.join(); // keep main thread alive
106  
  }
107  
  
108  
  static void printMyIPs() {
109  
  String ip;
110  
    try {
111  
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
112  
        while (interfaces.hasMoreElements()) {
113  
            NetworkInterface iface = interfaces.nextElement();
114  
            // filters out 127.0.0.1 and inactive interfaces
115  
            if (iface.isLoopback() || !iface.isUp())
116  
                continue;
117  
118  
            Enumeration<InetAddress> addresses = iface.getInetAddresses();
119  
            while(addresses.hasMoreElements()) {
120  
                InetAddress addr = addresses.nextElement();
121  
                ip = addr.getHostAddress();
122  
                System.out.println(iface.getDisplayName() + " " + ip);
123  
            }
124  
        }
125  
    } catch (Throwable e) {
126  
        e.printStackTrace();
127  
    }
128  
  }
129  
}
130  
131  
!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: 814 / 955
Referenced in: [show references]