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

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