Copy files via IR

G

Guest

Hi

I'm trying to copy a text file from one handheld to another handheld, they are both iPAQ h2210's, via IR. I found an example of how to do this on the net and almost have it working. I can receive the stream over IR but I can't seem to write it to a file. Here is my code to receive and write it to a file

===================================================================
'set up file to write t
Dim outputStream As IO.Strea
outputStream = System.IO.File.Open("test.txt", IO.FileMode.Create

'create listener and start i
m_irListener = New Net.Sockets.IrDAListener(IRDA_SERVICE_NAME
m_irListener.Start(

'accept the I
Dim irdaClient As Net.Sockets.IrDAClien
irdaClient = m_irListener.AcceptIrDAClient(

'create in Strea
Dim irStreamIn As System.IO.Strea
irStreamIn = irdaClient.GetStrea

Const BUFFER_SIZE = 102
Dim inBuffer(BUFFER_SIZE) As Byt
Dim bytesRead As Intege

D
'Read the bytes in from the IR por
bytesRead = irStreamIn.Read(inBuffer, 0, BUFFER_SIZE

'Write the bytes out to our output strea
If (bytesRead > 0) The
outputStream.Write(inBuffer, 0, bytesRead
End I
Loop While (bytesRead > 0

outputStream.Flush() 'Finish writing any outpu

'Close our input strea
irStreamIn.Close(
'Close the irda clien
irdaClient.Close(
'Close the listener if its runnin
StopListenerIfRunning(
'Close the file we have been writing t
outputStream.Close(
=================================================================

my file 'test.txt' is zero length and nothing is written to it

But if I put the following code

========================================
Dim i As Intege
While i < 10
MessageBox.Show(Chr(inBuffer(i)).ToString
i +=
End Whil
=========================================

right after the do/while loop, I get my characters one at a time in the messagebox...so I know the files contents are getting sent over the IR stream, but I can't seem to get it to write out to a file. Does the size of the file I'm sending matter? It's about 7500 bytes

If anyone can help it would be much appreciated

Thanks
skicow
 
A

Alex Feinman [MVP]

The problem must be on the sending side. It is not supplying data fast
enough so the receiving stream gets 0-length buffers. There are several
solutions. You can establish a protocol, whereby you first send file size
followed by the file data and then on receiving side keep reading until you
get that many bytes or timeout expires. You can also introduce a delay loop
checking for NetworkStream.DataAvailable to become true (again with some
sort of timeout, so that you do not block forever in case of error).

--
Alex Feinman
---

skicow said:
Hi,

I'm trying to copy a text file from one handheld to another handheld, they
are both iPAQ h2210's, via IR. I found an example of how to do this on the
net and almost have it working. I can receive the stream over IR but I can't
seem to write it to a file. Here is my code to receive and write it to a
file:
====================================================================
'set up file to write to
Dim outputStream As IO.Stream
outputStream = System.IO.File.Open("test.txt", IO.FileMode.Create)

'create listener and start it
m_irListener = New Net.Sockets.IrDAListener(IRDA_SERVICE_NAME)
m_irListener.Start()

'accept the IR
Dim irdaClient As Net.Sockets.IrDAClient
irdaClient = m_irListener.AcceptIrDAClient()

'create in Stream
Dim irStreamIn As System.IO.Stream
irStreamIn = irdaClient.GetStream

Const BUFFER_SIZE = 1024
Dim inBuffer(BUFFER_SIZE) As Byte
Dim bytesRead As Integer

Do
'Read the bytes in from the IR port
bytesRead = irStreamIn.Read(inBuffer, 0, BUFFER_SIZE)

'Write the bytes out to our output stream
If (bytesRead > 0) Then
outputStream.Write(inBuffer, 0, bytesRead)
End If
Loop While (bytesRead > 0)

outputStream.Flush() 'Finish writing any output

'Close our input stream
irStreamIn.Close()
'Close the irda client
irdaClient.Close()
'Close the listener if its running
StopListenerIfRunning()
'Close the file we have been writing to
outputStream.Close()
==================================================================

my file 'test.txt' is zero length and nothing is written to it.

But if I put the following code:

=========================================
Dim i As Integer
While i < 100
MessageBox.Show(Chr(inBuffer(i)).ToString)
i += 1
End While
==========================================

right after the do/while loop, I get my characters one at a time in the
messagebox...so I know the files contents are getting sent over the IR
stream, but I can't seem to get it to write out to a file. Does the size of
the file I'm sending matter? It's about 7500 bytes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top