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

128
LINES

< > BotCompany Repo | #1000596 // Cross-platform web server (port 8888), serving another movie

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

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

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("/home/stefan/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 #1000437

download  show line numbers  debug dex  old transpilations   

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

Comments [hide]

ID Author/Program Comment Date
198 #1000604 (pitcher) 2015-08-18 00:50:45
182 #1000610 (pitcher) 2015-08-18 00:50:07

add comment

Snippet ID: #1000596
Snippet name: Cross-platform web server (port 8888), serving another movie
Eternal ID of this version: #1000596/1
Text MD5: ce18e106e63ef6b89982d2bbe9852731
Transpilation MD5: 71c1eee69a9ea253c0916fd59c9056a9
Author: stefan
Category: javax android
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-22 22:30:34
Source code size: 3897 bytes / 128 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 973 / 900
Referenced in: [show references]