Using StreamSocket in external app to talk to OOo
Posted: Wed Feb 20, 2013 11:56 am
I have an external application, which connects to OpenOffice.org on a Port 2081. I wish to implement a OOBasic macro, which will connect to the external application and get data from that application. For this I have implemented the osl::AcceptorSocket class which listens to a port ( different than the earlier port). I am trying to connect to it from OOBasic macro. The code in the external application is as follows
The macro function in OOBasic which connects to it is as follows
My problem is that, I dont return from the call below, immediately after a write (followed by flush) on the connection by the OOBasic macro.
Also the macro runs through and reports an exception "com.sun.star.lang.IllegalArgumentException arguments len differ" at the line
I am looking forward to help in making this work. I wish to be able to send and recieve data from the OOBasic macro.
Also I wish to know, how the data should be passed from the OOBasic macro to the external application. Basically how to convert the byte sequence to XInputStream and XOutputStream.
Thanks
Mangesh
Code: Select all
class MacroListener
{
public:
static const sal_Int32 DEFAULT_PORT;
MacroListener();
MacroListener(sal_Int32 port);
~MacroListener();
int CreateSocket(sal_Int32 port);
int ListenOnSocket();
private:
AcceptorSocket* m_socket;
SocketAddr* m_socAddr;
sal_Int32 m_port;
};
const sal_Int32 MacroListener::DEFAULT_PORT = 2042;
MacroListener::MacroListener() : m_port(DEFAULT_PORT)
{
CreateSocket(m_port);
}
MacroListener::MacroListener(sal_Int32 port)
{
CreateSocket(m_port);
}
MacroListener::~MacroListener()
{
delete m_socket;
delete m_socAddr;
}
int MacroListener::CreateSocket(sal_Int32 port)
{
m_socAddr = new SocketAddr(OUString::createFromAscii("localhost"), port);
if(!m_socAddr->is())
{
printf(" There was an error in creating the SocketAddr \n");
return 1;
}
m_socket = new AcceptorSocket();
if(m_socket == 0)
{
printf("error creating a socket\n");
return 1;
}
m_socket->bind(*m_socAddr);
return 0;
}
int MacroListener::ListenOnSocket()
{
StreamSocket rConnect;
// number of connections at a time
m_socket->listen(5);
int error = 0;
do
{
oslSocketResult result = m_socket->acceptConnection(rConnect);
if(result != oslSocketResult::osl_Socket_Ok)
{
oslSocketError error = osl_getLastSocketError(m_socket->getHandle());
printf(" There was an error in accepting connection \n");
}
// Read data sent by the OOBasic macro
{
sal_Char buffer[256];
sal_uInt32 size_buffer = 20;
sal_Int32 n = rConnect.read(buffer, 256); // Returns only after the exception in the macro is thrown
if(n < 0)
{
printf(" error reading from socket \n");
error = 1;
}
printf("here is the message: %s \n", buffer);
}
// send data back to the OOBasic macro
{
OUString msgBuf = OUString::createFromAscii("I got your message");
sal_Int32 n = rConnect.write("I got your message", 18);
if(n < 0)
{
printf("error writing to socket");
error = 1;
}
}
}
while(error == 0);
return 0;
}
Code: Select all
Sub Test2
oConnector = createUnoService( "com.sun.star.connection.Connector" )
oConnection = oConnector.connect("socket,host=localhost,port=2042")
desc = oConnection.getDescription
cCR = Chr(13)
cLF = Chr (10)
oConnection.write( StringToByteArray( "Hello World" + cCR + cLF) )
oConnection.flush()
aByteArray = Array()
nBytesRead = oConnection.read()(aByteArray, 200) //com.sun.star.lang.IllegalArgumentException arguments len differ
oConnection.close()
End Sub
Code: Select all
sal_Int32 n = rConnect.read(buffer, 256)
Code: Select all
nBytesRead = oConnection.read()(aByteArray, 200)
Also I wish to know, how the data should be passed from the OOBasic macro to the external application. Basically how to convert the byte sequence to XInputStream and XOutputStream.
Thanks
Mangesh