Click to See Complete Forum and Search --> : client control server's parallel port via jsp..help..


sauron
06-12-2006, 02:24 PM
Hi everyone:

I m not sure wheather i post at the right place.. but i have no idea..


My problem:
I need to complete a project which required controlling pc(server) parallel port remotely, via internet from another pc(client) with webpage interface.

User will need to complete the registration form in order to login and controlling the server side parallel port.

i just ignore the registration side first since i hv an example about the database-related example from "Jakarta Struts for Dummies".
www.dummies.com/go/jakarta

And now i need to create a simple jsp, with servlet which will enable client to set or reset the parallel port.
Here is an example about controlling the parallel port remotely,
http://www.epanorama.net/circuits/parallel_output.html
but it run a program called portcontrol.exe with php, i need to controlled it without such .exe program.

I have successfully tested program below to set the bit0 of parallel port, but in java.
--------------------------------------------------------------------------------------------------
import parport.ParallelPort;
class SimpleIO {
public static void main ( String []args )
{
ParallelPort lpt1 = new ParallelPort(0x378); // 0x378 is normally the base address for the LPT1 port
int aByte;
aByte = 0x01; // a simple and irrelevant operation
lpt1.write(aByte); // write a byte to the port's DATA pins
System.out.println("Output to port: " + aByte);
}
}
-----------------------------------------------------------------------------------------------

but .. how to "put" these program into .jsp so it can controll by client?
let say.. the page will provide a button, and when it pressed, server will
set it parallel port's bit0.


I have set up a tomcat web-server.

I m still beginner in jsp and servlet programming, and i really need help from all of you. please guide.
Thanks in advance.

Khalid Ali
06-12-2006, 05:07 PM
interesting....I have seen people do such things using active x controls from IE browser. Java runs in a sandbox environment..that means to make a servlet do something outside of its context, you will have to comeup with some crude tricks.

sauron
06-13-2006, 04:26 AM
i have try this ..


import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import parport.ParallelPort;
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.*;
public class httpServer_io
{
public static void main(String args[]) {
int port;
ServerSocket server_socket;
try {
port = Integer.parseInt(args[0]);
}
catch (Exception e) {
port = 7170;
}
try {
server_socket = new ServerSocket(port);
System.out.println("httpServer running on port " +
server_socket.getLocalPort());
// server infinite loop
while(true) {
Socket socket = server_socket.accept();
System.out.println("New connection accepted " +
socket.getInetAddress() +
":" + socket.getPort());
// Construct handler to process the HTTP request message.
try {
httpRequestHandler request =
new httpRequestHandler(socket);
// Create a new thread to process the request.
Thread thread = new Thread(request);
// Start the thread.
thread.start();
}
catch(Exception e) {
System.out.println(e);
}
}
}
catch (IOException e) {
System.out.println(e);
}
}
}
class httpRequestHandler implements Runnable
{
final static String CRLF = "\r\n";
Socket socket;
InputStream input;
OutputStream output;
BufferedReader br;
// Constructor
public httpRequestHandler(Socket socket) throws Exception
{
this.socket = socket;
this.input = socket.getInputStream();
this.output = socket.getOutputStream();
this.br =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
// Implement the run() method of the Runnable interface.
public void run()
{
try {
processRequest();
}
catch(Exception e) {
System.out.println(e);
}
}
private void processRequest() throws Exception
{
while(true) {
String headerLine = br.readLine();
System.out.println(headerLine);
if(headerLine.equals(CRLF) || headerLine.equals(""))
break;
StringTokenizer s = new StringTokenizer(headerLine);
String temp = s.nextToken();
if(temp.equals("GET")) {
ParallelPort lpt1 = new ParallelPort(0x378); // 0x378 is normally the base address for the LPT1 port
int aByte;


//aByte = lpt1.read(); // read a byte from the port's STATUS pins
aByte = 0x01; // a simple and irrelevant operation
lpt1.write(aByte); // write a byte to the port's DATA pins
System.out.println("Output to parallel port: " + aByte);

//System.out.println("Input from parallel port: " + aByte);
// Get today's date
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println(" It is now : " + formatter.format(now.getTime()));
System.out.println();
// Construct the response message.
String serverLine = "Server: fpont simple java httpServer";
String statusLine = null;
String contentTypeLine = null;
String entityBody = null;
String contentLengthLine = "error";
statusLine = "HTTP/1.0 200 OK" + CRLF ;
System.out.println("statusLine: "+statusLine);
contentTypeLine = "text/html" ;
entityBody = "<HTML>" +
"<HEAD> <TITLE> I/O port </TITLE> </HEAD>"
+" Simple HTTP Server <br> "+formatter.format(now.getTime())
+"<br> I/O port - Status:"+aByte+"<br> StatusByte< ";
int i = 256; //max number * 2
while( (i >>= 1) > 0) {
System.out.print(((aByte & i) != 0 ? "1" : "0"));
entityBody +=(((aByte & i) != 0 ? "1" : "0"));
}
entityBody += "><br>";
contentLengthLine = "Content-Length: "
+ (new Integer(entityBody.length()).toString() )
+ CRLF;
// Send the status line.
output.write(statusLine.getBytes());
// Send the server line.
output.write(serverLine.getBytes());
// Send the content type line.
output.write(contentTypeLine.getBytes());
// Send the Content-Length
output.write(contentLengthLine.getBytes());
// Send a blank line to indicate the end of the header lines.
output.write(CRLF.getBytes());
System.out.println("\nentityBody: "+entityBody);
output.write(entityBody.getBytes());
}
}
try {
output.close();
br.close();
socket.close();
}
catch(Exception e) {}
}
}

By running the java program below, once client access to
http://localhost:7170, it will set server's parallel port bit0 to high.

I wish to change it to servlet (maybe with jsp) and hv a graphic interface which enable client to set bit(s) of parallel port.

Any idea on modified the code? Please guide me the steps needed...

Thanks a lot . !!