Webserver on NXT

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: roger, 99jonathan, imaqine

Webserver on NXT

Postby ChefFD » Mon Jan 07, 2013 8:57 am

Hey Guys,

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
ChefFD
New User
 
Posts: 5
Joined: Mon Dec 17, 2012 11:32 am

Re: Webserver on NXT

Postby cgp » Tue Jan 08, 2013 4:28 am

I don't know anything about the NXT system and the code, but what were the errors? Where did you get stuck?
cgp
New User
 
Posts: 1
Joined: Tue Jan 08, 2013 4:25 am

Re: Webserver on NXT

Postby ChefFD » Tue Jan 08, 2013 7:30 am

Hi and thanks for reply

i get stuck very early during connection at exactly this point:
"sock = new Socket("localhost", 8081);"

i get "connection refused" at this point.

Maybe you can help

greetings
ChefFD
New User
 
Posts: 5
Joined: Mon Dec 17, 2012 11:32 am


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff