i'm trying to run a webserver on the NXT Brick.
The aim is to control the NXT via Bluetooth-connection and webbrowser. Has anybody done this before?
I used an example out of this forum but it doesnt work properly.
I tried the following code together with nxjsocketproxy. Maybe somebody can help?
- Code: Select all
import lejos.nxt.comm.*;
import java.io.*;
import java.util.*;
import java.net.*;
/**
* Simple web server
*
*/
public class webserver {
private DataInputStream ins;
private DataOutputStream outs;
private BTConnection btc = null;
private ServerSocket serverSock = null;
private Socket sock = null;
public webserver() throws Exception{
print("Waiting");
btc = Bluetooth.waitForConnection();
NXTSocketUtils.setNXTConnection(btc);
sock = new Socket("localhost", 8081);
//serverSock = new ServerSocket(8081,btc);
print("Connected");
while(true){
print("Accepting");
sock = serverSock.accept();
print("Accepted");
ins = new DataInputStream(sock.getInputStream());
outs = new DataOutputStream(sock.getOutputStream());
String s = readLine(); // Print the HTTP command line
print(s);
StringTokenizer tokens = new StringTokenizer(s," ");
String cmd = tokens.nextToken();
print("Command = " + cmd);
String file = tokens.nextToken();
print("File = " + file);
while (true) { // Suck in the rest
s = readLine();
if (s.length() == 0 || s == null) break;
}
// Send reply
writeLine("HTTP/1.1 000 OK");
writeLine("");
writeLine(file);
closeStream();
}
}
public void print(String s){
System.out.println(s);
}
public void closeStream() throws IOException{
ins.close();
outs.close();
}
public String readLine() {
String s = "";
while (true) {
try {
int b = ins.readByte();
if (b == 13) continue;
if (b == 10) return s;
s += (char) b;
} catch (IOException ioe) {
return null;
}
}
}
public void writeLine(String s) {
try {
for(int i=0;i<s.length();i++) {
outs.writeByte((byte) s.charAt(i));
}
outs.writeByte(13);
outs.writeByte(10);
outs.flush();
} catch (IOException ioe) {}
}
public static void main(String [] args) throws Exception
{
new webserver();
}
}
Many thanks to all of you, who can help me!
greetings ChefFD
