//: slComm.java
//  SYNOPSIS:      slComm
//  DESCRIPTION:   slComm is a class that can talk to a SecondLife script
//                 Usiing HttpRequest and HttpReponse.
//                 The program creates a tcp socket in the inet 
//                 domain, binds it to port 8555, listen and accept 
//                 a connection from tcpClient0, and receives any message
//                 arrived to the socket and prints it out
/////////////////////////////////////////////////////////////////////////

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

class slComm {  
  // Choose a port outside of the range 1-1024:
    private final int PORT = 8555;
    private ServerSocket s;
    private Socket socket;

    /* Connect to the socket */
    public void start_slc() throws IOException {
        // Create a Server Socket and binds it to the port 8555
	s = new ServerSocket(PORT);
    }
    
    /* Connect to the socket */
    public void end_slc() throws IOException {
 	/* Close the socket connection */
	s.close();
    }
    
    /* Wait for a request on the socket, read it, and return the string */
    public String receive_slc() throws IOException {

	// Blocks until a connection occurs:
	socket = s.accept();
	 
	// System.out.println("Connection accepted: "+ socket);
	// Read from InputStream from the Socket
	BufferedReader in = 
	    new BufferedReader(
	        new InputStreamReader(
         	     socket.getInputStream()));
	  
	// get the Recived string 
	String str = in.readLine();
	    
	return str;
    }

    /* Send a response back to the requester */
    public void send_slc(String reply) throws IOException {
	try {
	    PrintWriter out = new PrintWriter(
	        new BufferedWriter(
	           new OutputStreamWriter(
	              socket.getOutputStream())),true);

	    // send reply to the tcpServer
	    out.println(reply);
	  
	    // Always close the two sockets...
	} finally {
	    System.out.println("closing...");
	    socket.close(); 
	}
    }
}

/* This is a simple driver to demonstrate the use of slComm */
public class slDriver {
    /* The mainline */
    public static void main(String[] args) throws IOException {

	/* Set up console IO operations */
	InputStreamReader converter = new InputStreamReader(System.in);
	BufferedReader console = new BufferedReader(converter);

	/* Create a SL communication Agent */
	slComm slc = new slComm();

	try {
	 
	  /* Initialize the connection */
	  slc.start_slc();

	  while (true) {
	      /* Wait for a message from SL */
	      System.out.print("waiting for message from SL...");
	      String mess = slc.receive_slc();

	      /* Display the message */
	      System.out.println("message received: " + mess);

	      // Get a reply from the user and send it back
	      System.out.print("Enter Reply: ");
	      String reply = console.readLine();
	      slc.send_slc(reply);

	  } 
	} finally {
	    /* End the connection */
	    slc.end_slc();
	}
    }
}

-- JimSkon - 2011-06-14

Topic revision: r1 - 2011-06-14 - JimSkon
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback