Networkstream Returning Null Value

  • Thread starter Dustin Brisebois via DotNetMonster.com
  • Start date
D

Dustin Brisebois via DotNetMonster.com

This is coming from a Windows 2003 Server to a HP Pocket PC (WinCE 2003)
over 802.11b. The device is on the Network and can be pinged, it can write
data to the Server stream - but not read the reverse.

I'd appreciate any help.

Server
------
Dim TcpC as New TcpListener(IPAddress.Parse("192.168.1.5"), 8500)
TcpC.Start()
Dim tcpclient As TcpClient = TcpC.AcceptTcpClient
Dim ns As NetworkStream = tcpclient.GetStream
Dim bytes() = System.Text.Encoding.ASCII.GetBytes("Testing Connection
Write")
ns.Write(bytes, 0, bytes.length)
tcpclient.Close()
TcpC.Stop()

Mobile
------
Dim tcpc As New TcpClient
tcpc.Connect(IPAddress.Parse("192.168.1.5"), 8500)
Dim bytes(30000) As Byte
Dim NS As NetworkStream = tcpc.GetStream
NS.Read(bytes, 0, CInt(tcpc.ReceiveBufferSize))

it fails on the last line giving an ArgumentOutOfRangeException saying that
IsNothing(bytes) = True.
 
A

Adam Goossens

Hi Dustin,
Dim bytes(30000) As Byte

You forgot to instantiate your array:

---
Dim bytes(30000) As New Byte
---

Give that a go. Without the new operator your bytes variable will be
Nothing (as it hasn't been instantiated yet and hence the error. :)

Regards,
-Adam Goossens.
 
A

Adam Goossens

Oops. I mis-read the exception type :) I think I need a bit more
sleep...my head feels groggy :p

You may have received more data than your array can handle (the
ArgumentOutOfRange exception is a good clue to that). You should only
read as much data as you have space to handle it.

Try something like this instead:

---
NS.Read(bytes, 0, bytes.Length)
---

Or, dimension your array to accomodate all of the data received:

---
Dim bytes(tcpc.Length) As Byte
---

As for my stupid comment about instantiating your array: ignore it. I
don't know what I was thinking - you can't instantiate arrays with New.
I must be tired.

Sorry :)
Regards,
-Adam.
 

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