Saving text out of Adaptive Server

This example describes how to set up a client application in Adaptive Server. Adaptive Server version12.5 does not support direct access to a file; this example is a workaround for this limitation.

You can write your own external server, which performs file operations, and connect to this new server from the Adaptive Server, using a socket created from a Socket class.

In the basic roles of client and server, the client connects to the server and streams the text, while the server receives the stream and streams it to a file.

This example shows how you can install a Java application in Adaptive Server, using java.net. This application acts as a client to an external server.

StepsThe client process:

  1. Receives an InputStream.

  2. Creates a socket using the Socket class to connect to the server.

  3. Creates an OutputStream on the socket.

  4. Reads the InputStream and writes it to the OutputStream:

    public static void writeOut(InputStream fin)         throws Exception{
    
    Socket socket = new Socket(“localhost”, 1718);
    
    OutputStream fout = new
    
    BufferedOutputStream(socket.getOutputStream());byte [] buffer = new byte [10];
    
    int bytes_read;
    
    while (bytes_read = fin.read(buffer)) ! = -1{ fout.write(buffer, 0, bytes_read);
    
    }
    
    fout.close();
    
    }
    

    Compile this program.

StepsThe server process:

  1. Creates a server socket, using the SocketServer class, to listen on a port.

  2. Uses the server socket to obtain a socket connection.

  3. Receives an InputStream.

  4. Reads the InputStream and writes it to a FileOutputStream.

NoteIn this example, the server does not use threads, and therefore it can receive a connection from only one client at a time.

public class FileServer {
public static void main (string[] args) throws         IOException{
 Socket client = accept (1718);
 try{
  InputStream in = client.getInputStream ();
  FileOutputStream fout = new
   FileOutputStream(“chastity.txt”);
   byte[] buffer = new byte [10];
   int bytes_read;
   while (bytes_read = in.read(buffer))!= -1){
    fout.write(buffer, 0, bytes_read);
 }
   fout.close();
 }finally {
  client.close ();
 }
}
static Socket accept (int port) throws
		IOException{
	System.out.prinln
		(“Starting on port “ + port);
	ServerSocket server = new
		ServerSocket (port);
	System.ou.println (“Waiting”);
	Socket client = server.accept ();
	System.out.println (“Accepted from “ +
		client.getInetAddress ());
	server.close ();
	return client;
	}
}

Compile this program.

To use this combination of client and server, you must install the client in Adaptive Server and start the external server:

witness% java FileServer &
[2] 28980
witness% Starting on port 1718
Waiting

Invoke the client from within Adaptive Server.

create table t(c1 text)
go
insert values into t1 (“samplestring”)
go
select TestStream2File.writeOut(c1) from t
go