Knowledge Base Nr: 00220 LiveCamServer.java - http://www.swe-kaiser.de

Downloads:

java: implementation of a socket server.
starts new processes and communicates with them.

  
//****************************************************************************
//* Imports *
//****************************************************************************

import java.net.*;
import java.io.*;
import java.lang.*;

//****************************************************************************
//* LiveCamServer *
//****************************************************************************

public class
LiveCamServer
extends Thread
{

//****************************************************************************
//* Constants *
//****************************************************************************

public static final int TELNET_PORT = 4711;

//****************************************************************************
//* Variables *
//****************************************************************************

protected ServerSocket listener;

//****************************************************************************
//* main *
//****************************************************************************

public static void
main( String args[] )
{
new LiveCamServer();
}

//****************************************************************************
//* fail *
//****************************************************************************

public static void
fail( Exception e, String msg )
{
System.err.println( msg + "." + e );
System.exit( 1 );
}

//****************************************************************************
//* Constructor *
//****************************************************************************

public
LiveCamServer()
{
// Try and create the socket...
try
{
listener = new ServerSocket( TELNET_PORT, 5 );
}
catch ( IOException e )
{
fail( e, "Could not start LiveCam server" );
}

// Show something to the user...
System.out.println( "LiveCam server started" );

// Start up the thread...
start();
}

//****************************************************************************
//* run *
//****************************************************************************

public void
run()
{
try
{
while ( true )
{
Socket client = listener.accept();
System.out.println( "Accepted client..." );
LiveCamThread c = new LiveCamThread( client );
}
}
catch ( IOException e )
{
fail( e, "Unable to accept incoming client connection" );
}
}
}

//****************************************************************************
//* LiveCamThread *
//****************************************************************************

class
LiveCamThread
extends Thread
{

//****************************************************************************
//* Variables *
//****************************************************************************

protected Socket sClient;
protected BufferedReader disIn;
protected PrintWriter psOut;

//****************************************************************************
//* Constructor *
//****************************************************************************

public
LiveCamThread( Socket client )
{
try
{
// Save a copy of our client socket...
sClient = client;

// Don't cross the streams!
disIn = new BufferedReader( new InputStreamReader( client.getInputStream() ) );
psOut = new PrintWriter( client.getOutputStream(), true );

// Start up the client...
start();
}
catch ( IOException e )
{
try
{
client.close();
}
catch ( IOException ioe )
{
System.err.println( "Error creating streams: " + ioe );
}
}
}

//****************************************************************************
//* run *
//****************************************************************************

public void run()
{
String cmd = null, res="";
int n = 0;

System.out.println( "Running client..." );

Process grabber = null;

try
{
grabber = Runtime.getRuntime().exec("e:/MyStuff/JavaCam.exe e:/projects/internet/my_home/images/k_images/livecam/javacam.bmp 4 0 217 19");
BufferedReader grabberIn = new BufferedReader( new InputStreamReader( grabber.getInputStream() ) );
PrintWriter grabberOut = new PrintWriter( grabber.getOutputStream(), true );

while ( true )
{
cmd = disIn.readLine();
if (cmd == null)
break;

System.out.println( "client request: <" + cmd + ">");

if (cmd.indexOf("Switch") >= 0)
{
Process switcher = Runtime.getRuntime().exec("\"E:/projects/msvc60/CControlIF/Debug/CControlIF.exe\" " + cmd);
try { switcher.waitFor(); } catch(InterruptedException e) { }

psOut.println(cmd + ": ok!");
}
else if (cmd.indexOf("GetPic") >= 0)
{
String param = cmd.substring(cmd.indexOf(" "));

grabberOut.println(param);
res = grabberIn.readLine();

Process converter = Runtime.getRuntime().exec("\"c:/programme/thumbnailer/THUMB5\" /h /f e:/projects/internet/my_home/images/k_images/livecam/javacam.bmp e:/projects/internet/my_home/images/k_images/livecam/javacam.jpg");
try { converter.waitFor(); } catch(InterruptedException e) { }

psOut.println("Grabber: " + res);
}
else
{
psOut.println("Unknown command!");
}

if (psOut.checkError())
{
System.err.println("checkError() == true");
break;
}
}
}
catch ( IOException e )
{
System.err.println( "Error in while loop: " + e );
}

finally
{
try
{
if (grabber != null)
grabber.destroy();

sClient.close();
System.err.println( "closing client socket");
}
catch ( IOException ioe )
{
System.err.println( "Error closing client socket: " + ioe );
}
}

System.out.println ( "thread finished!" );
}

}