| View previous topic :: View next topic |
| Author |
Message |
colinmac Newbie


Joined: 23 Mar 2004 Posts: 2
|
Posted: Tue Mar 23, 2004 6:04 am Post subject: StarOffice 5.2 and making TCP/IP connections using Basic |
|
|
Hi. I'm trying to create a TCP/IP connection in a Basic macro, using StarOffice 5.2. In theory, I believe the following code should work:
connector = createUnoService("com.sun.star.connection.Connector")
conn = connector.Connect("socket,host=mailserver,port=25")
In practice, I get an error saying 'No conversion found for com.sun.star.connection.XConnection'.
I'm guessing that this is because the Basic engine can't find some way to convert from an XConnection to something else. But that's just a guess.
Does anyone have any idea what this actually means? Or do you have some sample code (in StarBasic) that will create a TCP/IP connection?
Thanks in advance. |
|
| Back to top |
|
 |
erikanderson3 OOo Advocate

Joined: 25 Feb 2004 Posts: 332 Location: San Francisco peninsula
|
Posted: Tue Mar 23, 2004 7:28 am Post subject: |
|
|
| colinmac wrote: | | I'm trying to create a TCP/IP connection in a Basic macro, using StarOffice 5.2. In theory, I believe the following code should work: |
Your snippet works for me (mostly), in more than just theory, but then I'm running OOo 1.1.0, so that may be the key difference.
I say 'mostly' as I have nothing running on port 25. Running the following little ditty: | Code: | Sub TestConnections()
Dim connector as object, conn as object
connector = createUnoService("com.sun.star.connection.Connector")
conn = connector.Connect("socket,host=mailserver,port=25")
End Sub |
produces an error message reading,
BASIC runtime error.
An exception occurred
Type: com.sun.star.connection.NoConnectException
Message: Connector : couldn't connect to socket (Success).
The 'Success' on the end suggests to me that the code in general works. Likewise, the API docs for the connect method explain that the NoConnectException means that the method "Couldn't reach a server (e.g. network failure), no server is listening", which makes sense as I have nothing on port 25.
Anyway, rambling aside, it sounds like your issue might be with SO 5.2 in particular. You may want to look into upgrading.
HTH,
Erik |
|
| Back to top |
|
 |
colinmac Newbie


Joined: 23 Mar 2004 Posts: 2
|
Posted: Wed Mar 24, 2004 4:11 am Post subject: |
|
|
| erikanderson3 wrote: | Anyway, rambling aside, it sounds like your issue might be with SO 5.2 in particular. You may want to look into upgrading.
|
Unfortunately this has to roll out to a whole stack of people, and the upgrade to SO7 is still some way off. At least it confirms that the code is, apparently, OK, which is something.
Thanks for your help. |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Wed Mar 24, 2004 7:35 am Post subject: |
|
|
Ooops, sorry, after posting the following, I realized you are on SO 5 not OOo 1.1.0.
I don't think this does what we are expecting. That is, I don't think it gives us a "raw" TCP/IP stream.
I tried the following code.
| Code: |
Private oListener
Private oOutput
Sub Main
oOutput = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
Writer_PrintLn( oOutput, "Opening connection." )
oConnector = createUnoService( "com.sun.star.connection.Connector" )
oConnection = oConnector.connect("socket,host=www.cnn.com,port=80")
' Create a scream listener.
Writer_PrintLn( oOutput, "Creating scream listener." )
oListener = createUnoListener( "ScreamListener_", "com.sun.star.io.XStreamListener" )
oConnection.addStreamListener( oListener )
cCR = Chr(13)
Writer_PrintLn( oOutput, "Sending HTTP request." )
oConnection.write( StringToByteArray( "GET / HTTP/1.0" + cCR + cCR ) )
Writer_PrintLn( oOutput, "Flush output." )
oConnection.flush()
Writer_PrintLn( oOutput, "Reading result." )
aByteArray = Array()
nBytesRead = oConnection.read( aByteArray, 200 )
Writer_PrintLn( oOutput, "Result follows..." )
Writer_PrintLn( oOutput, ByteArrayToString( aByteArray ) )
Writer_PrintLn( oOutput, "Read " + CStr( nBytesRead ) + " bytes." )
Writer_PrintLn( oOutput, "Close connection." )
oConnection.close()
' Remove scream listener.
Writer_PrintLn( oOutput, "Removing scream listener." )
oConnection.removeStreamListener( oListener )
End Sub
'########################################
' My Scream Listener implementation.
' interface: com.sun.star.io.XStreamListener
Sub ScreamListener_started()
Writer_PrintLn( oOutput, "ScreamListener_started() was called." )
End Sub
' interface: com.sun.star.io.XStreamListener
Sub ScreamListener_closed()
Writer_PrintLn( oOutput, "ScreamListener_closed() was called." )
End Sub
' interface: com.sun.star.io.XStreamListener
Sub ScreamListener_terminated()
Writer_PrintLn( oOutput, "ScreamListener_terminated() was called." )
End Sub
' interface: com.sun.star.io.XStreamListener
Sub ScreamListener_error( oException )
Writer_PrintLn( oOutput, "ScreamListener_error() was called." )
End Sub
' interface: com.sun.star.lang.XEventListener
' oEventSource is a struct com.sun.star.lang.EventObject
Sub ScreamListener_disposing( oEventSource )
Writer_PrintLn( oOutput, "ScreamListener_disposing() was called." )
End Sub
'########################################
'----------------------------------------
' Stuff ripped out of my library
'----------------------------------------
' The next four routines were previously posted
' http://www.oooforum.org/forum/viewtopic.php?t=6910
' Convert an array of bytes to a string.
' Pass in an array of bytes.
' Each "byte" in the array is an integer value from -128 to +127.
' The array of bytes could have come from reading
' from a com.sun.star.io.XInputStream.
' This function returns a string.
' This function is the opposite of StringToByteArray().
Function ByteArrayToString( aByteArray )
cBytes = ""
For i = LBound( aByteArray ) To UBound( aByteArray )
nByte = aByteArray(i)
nByte = ByteToInteger( nByte )
cBytes = cBytes + Chr( nByte )
Next i
ByteArrayToString() = cBytes
End Function
' Convert a string into an array of bytes.
' Pass a string value to the cString parameter.
' The function returns an array of bytes, suitable
' for writing to a com.sun.star.io.XOutputStream.
' Each "byte" in the array is an integer value from -128 to +127.
' This function is the opposite of ByteArrayToString().
Function StringToByteArray( ByVal cString As String )
nNumBytes = Len( cString )
Dim aBytes(nNumBytes-1) As Integer
For i = 1 To nNumBytes
cChar = Mid( cString, i, 1 )
nByte = Asc( cChar )
nByte = IntegerToByte( nByte )
aBytes(i-1) = nByte
Next
StringToByteArray() = aBytes()
End Function
' Convert a byte value from the range -128 to +127 into
' an integer in the range 0 to 255.
' This function is the opposite of IntegerToByte().
Function ByteToInteger( ByVal nByte As Integer ) As Integer
If nByte < 0 Then
nByte = nByte + 256
EndIf
ByteToInteger() = nByte
End Function
' This function is the opposite of ByteToInteger().
Function IntegerToByte( ByVal nByte As Integer ) As Integer
If nByte > 127 Then
nByte = nByte - 256
EndIf
IntegerToByte() = nByte
End Function
' The next two routines were previously posted...
' http://www.oooforum.org/forum/viewtopic.php?t=7068
' Sugar Coated way to Print into a Writer document.
' The oOutput parameter can be any of....
' com.sun.star.text.TextDocument
' com.sun.star.drawing.Text
' com.sun.star.text.TextCursor
Sub Writer_Print( oOutput, cString )
If oOutput.SupportsService( "com.sun.star.text.TextDocument" ) Then
oText = oOutput.getText()
oCursor = oText.createTextCursor()
ElseIf oOutput.SupportsService( "com.sun.star.drawing.Text" ) Then
oText = oOutput
oCursor = oText.createTextCursor()
ElseIf oOutput.SupportsService( "com.sun.star.text.Text" ) Then
oText = oOutput
oCursor = oText.createTextCursor()
ElseIf oOutput.SupportsService( "com.sun.star.text.TextCursor" ) Then
oCursor = oOutput
oText = oCursor.getText()
Else
Exit Sub
EndIf
oCursor.gotoEnd( False )
nLen = Len( cString )
nStart = 1
Do
nPos = Instr( nStart, cString, Chr(13) )
bCRFound = (nPos > 0)
If Not bCRFound Then
nPos = nLen + 1
EndIf
cSegment = Mid( cString, nStart, nPos-nStart )
nStart = nPos + 1
oText.insertString( oCursor, cSegment, False )
If bCRFound Then
oText.insertControlCharacter( oCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False )
EndIf
Loop While bCRFound
End Sub
' Same as Writer_Print(), just adds a line ending.
Sub Writer_PrintLn( oOutput, Optional cString )
If IsMissing( cString ) Then
cString = ""
EndIf
Writer_Print( oOutput, cString + Chr(13) )
End Sub
|
Now everything seems okay, but the Writer document which is where all the "Print" statements are printing to, accumulates the following output....
| the earlier code fragment wrote: |
Opening connection.
Creating scream listener.
Sending HTTP request.
Flush output.
Reading result.
ScreamListener_started() was called.
Result follows...
HTTP/1.1 400 Bad request
Server: Netscape-Enterprise/6.1 AOL
Date: Wed, 24 Mar 2004 15:28:48 GMT
Content-length: 147
Content-type: text/html
Connection: close
<HTML><HEAD><TITLE>Bad request</T
Read 200 bytes.
Close connection.
ScreamListener_closed() was called.
Removing scream listener.
|
Now if this was a raw TCP connection, then the line that said...
oConnection.write( StringToByteArray( "GET / HTTP/1.0" + cCR + cCR ) )
should have written a standard HTTP GET request to get the root document from the web server.
If I go to Linux and type....
| Code: | netcat www.cnn.com 80<cr>
GET / HTTP/1.0<cr>
<cr>
|
I get back standard HTTP headers, plus a huge amount of HTML text from the front page.
(If you don't have handy hacker tools like netcat isntalled, then you can substitute "telnet" for "netcat", but telnet adds various control codes to the connection when trying to negotiate what type of terminal features are supported -- but telnet does work well enough to talk to HTTP servers.) _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
dmccol Guest
|
Posted: Sun Mar 28, 2004 11:18 am Post subject: |
|
|
Hi,
You code is fine, you just forgot to send carriage return linefeed pairs. ie chr(10) after each chr(13).
| Code: |
cCR = Chr(13)
cLF = Chr (10)
Writer_PrintLn( oOutput, "Sending HTTP request." )
oConnection.write( StringToByteArray( "GET / HTTP/1.0" + cCR + cLF+ cCR +cLF ) )
|
Dave |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|