Hi Mike,
Actually I just got it to work - there was a problem with the offset, I guess I
just
don't understand the offset argument on the read method, I thought it meant
thats where you want to start reading, since we already read the first 36
chars, anyway on the second call I now get the whole message back, which is
fine, otherwise I would need to concatenate the header anyway.
The only problem I am currently having is returning the content, and still
wondering why it hang before on the old code I was using.
<cfx_JSocket4
host="144.55.11.67"
port="5498"
message="ASPSS90 ASPSS90 dsascot 00000065YHDASCDRON
0400 9 SHELLCO#chr(10)#YRQAnything Else 1
AL N#chr(10)#YTRENDDRON
3#chr(10)#"
response="response_"
debug="yes">
<cfdump var="#response_#">
It gives me a "Variable RESPONSE_ is undefined." error
The Java code is
import com.allaire.cfx.*;
import java.io.*;
import java.net.*;
public class TransportLayer implements CustomTag
{
public void processRequest( Request request, Response
response )
throws Exception
{
// Check if all required parameters are passed to theCFX tag
if ( !request.attributeExists( "host" ) )
throw new Exception( "Missing host parameter !!! You must passa
valid
server name, containing a valid host IP address." );
if( !request.attributeExists( "port" ) )
throw new Exception( "Missing port parameter !!! You must pass a
port
parameter, containing a numeric port on the remote server" );
if( !request.attributeExists( "message" ) )
throw new Exception( "Missing message parameter !!! You must pass a
message parameter, containing the message to be sent" );
if( !request.attributeExists( "response" ) )
throw new Exception( "Missing response parameter !!! You must pass
a
response parameter. This will be the cf variable used to write the returned
message to" );
Socket Socket = null;
PrintWriter out = null;
BufferedReader in = null;
String returnContent = "";
//Create socket connection
try
{
Socket = new Socket( request.getAttribute( "host" ),
request.getIntAttribute( "port" ) );
if ( request.debug() )
{
response.writeDebug( "Socket connection established ..." );
}
out = new PrintWriter( Socket.getOutputStream(), true );
if ( request.debug() )
{
response.writeDebug( "Writer to remote socket created ..." );
}
in = new BufferedReader( new InputStreamReader
(Socket.getInputStream() ) );
//BufferedReader inHeader = new BufferedReader( new
InputStreamReader( Socket.getInputStream() ), 36 );
if ( request.debug() )
{
response.writeDebug( "Reader from remote socket created ...");
}
//Send data over socket
// Valid test message "ASPSS90 ASPSS90
dsascot00000065YHDASCDRON 0400 9
SHELLCO\nYRQAnythingElse 1 AL N\nYTRENDDRON 3\n"
out.println( request.getAttribute( "message" ) );
if ( request.debug() )
{
response.writeDebug( "Message sent to remote server ..." );
}
out.flush();
//Receive text from server
if ( request.debug() )
{
response.writeDebug( "Text from remote server about to be
read ..." );
}
try
{
char[] myHeader = new char[ 36 ];
in.read( myHeader, 0, 36 );
if ( request.debug() )
response.writeDebug( "Converting the header to a string
... " );
String header = new String(myHeader);
if ( request.debug() )
response.writeDebug( "Outputting the header: " + header
+ " ... " );
String byteToReadString = header.substring( 28, 36 );
if ( request.debug() )
{
response.writeDebug( "Getting the message length: " +
byteToReadString + " ... " );
response.writeDebug( "Parsing the string ... " );
}
int byteToRead = Integer.parseInt( byteToReadString );
char[] myMessage = new char[ byteToRead ];
if ( request.debug() )
response.writeDebug( "Reading the rest of the message
... " );
try
{
in.read( myMessage, 0, byteToRead );
}
catch ( Exception e )
{
response.writeDebug( "Error reading the rest of the
message,
byteToRead: " + byteToRead + " ... message: " + e.getMessage() + " ... " );
}
String message = new String(myMessage);
if ( request.debug() )
response.writeDebug( "Outputting the message " +
message
+ " ... " );
response.writeDebug( message );
in.close();
}
catch ( IOException e )
{
response.writeDebug( "The read from the socket " +
Socket.getPort()
+ " terminated unexpectedly ... " + e.getMessage() );
}
// write content to cf variable
try
{
response.setVariable( "response", returnContent );
}
catch ( Exception e )
{
response.writeDebug( "There was an error trying to set the
return
variable ..." + e.getMessage() );
}
if ( request.debug() )
{
response.writeDebug( "Finished reading text from remote server
..." );
response.writeDebug( "Content returned: " + returnContent );
}
// close socket
Socket.close();
if ( request.debug() )
{
response.writeDebug( "Socket closed ..." );
}
}
catch ( UnknownHostException e )
{
response.writeDebug( "Unknown host ..." );
}
catch ( IOException e )
{
response.writeDebug( "I/O Exception ..." );
}
catch ( Exception e )
{
response.writeDebug( "An exception has occurred ..." + e.getMessage
() );
}
}
}
________________________________
Taco Fleur - E-commerce Development Manager
Shelco Searches & Services
An Authorised ASIC Information Broker
www.shelco.com.au
Ph: + 61 7 3236 2605
---
You are currently subscribed to cfaussie as: [email protected]
To unsubscribe send a blank email to [EMAIL PROTECTED]
Aussie Macromedia Developers: http://lists.daemon.com.au/