|
-
Feb 8th, 2010, 05:19 PM
#1
Thread Starter
Addicted Member
Can anyone suggest a better way to do this?
Code:
Private receivebuffer(1024) As Byte
Private allbytesread As New List(Of Byte)
Code:
Private Sub gettcpdata(ByVal ar As IAsyncResult)
Dim netstream As NetworkStream = CType(ar.AsyncState, NetworkStream)
Dim numberofbytesread As Integer = netstream.EndRead(ar)
If numberofbytesread = 0 Then Exit Sub
'Is there a more elegant way to do this below?
'-------------------------------------------------
ReDim Preserve receivebuffer(numberofbytesread - 1)
allbytesread.AddRange(receivebuffer)
ReDim receivebuffer(1024)
'-------------------------------------------------
While netstream.DataAvailable
netstream.BeginRead(receivebuffer, 0, 1024, New AsyncCallback(AddressOf gettcpdata), netstream)
End While
'Console.writeline below for debugging purposes only.
Console.WriteLine("received:{0}", Encoding.ASCII.GetString(allbytesread.ToArray))
'Raise an event to signal / send data back to main thread as a list of bytes not a string
allbytesread.Clear()
netstream.BeginRead(receivebuffer, 0, 1024, New AsyncCallback(AddressOf gettcpdata), netstream) 'keep on looping
End Sub
This code works. Its missing some exception handling etc but it IS NOT good code can anyone suggest a way to improve it.
Thanks
Adrian
-
Feb 8th, 2010, 06:25 PM
#2
Re: Can anyone suggest a better way to do this?
You seem to be reading data asynchronously from a TcpClient. You might like to follow the CodeBank link in my signature and check out my Asynchronous demo.
-
Feb 8th, 2010, 07:09 PM
#3
Thread Starter
Addicted Member
Re: Can anyone suggest a better way to do this?
Ok thanks for the pointer. Had a look at your code. Definately cleared up a lot of questions /problems.
One thing tho. I dont really want to create a string. Just a List of bytes (same difference I suppose.)
Create a string then convert it to a byte array but that seems a long way to go about it.
I most source a good college and try and enrol in some courses. I want to do so much but just dont know enough.
Last edited by 2ndmessiah; Feb 8th, 2010 at 07:13 PM.
-
Feb 8th, 2010, 07:39 PM
#4
Re: Can anyone suggest a better way to do this?
My code was designed specifically for sending text messages. The intention was that you could feed text in one end and get the same text out the other. Internally though, the data must be transmitted as Bytes, as must all data. If you've got Bytes to begin with then your code will be simpler than mine, because you can remove the steps that convert text to binary and back again. Everything else would be basically the same though.
-
Feb 8th, 2010, 08:53 PM
#5
Thread Starter
Addicted Member
Re: Can anyone suggest a better way to do this?
Yup I know it all comes out in bytes. And with a stringbuilder it is easy to assemble it all into one nice string.
I just want to assemble the bytes into one object. Either a byte array or a list of bytes.
Im sure it will be obvious to me in the morning.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|