Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello all,

I am new to xtremedotnettalk and this is my first thread.

 

I am trying to create a function in vb.net that sends a website header through a socket to the server and returns the response. This is my code.

 

    Public Function GetSiteResponse(ByVal IPEndpoint As IPEndPoint, ByVal client As Socket, ByVal header As String) As Byte()
       client.Connect(IPEndpoint)
       If client.Connected Then
           Dim sendbuffer As Byte() = Text.Encoding.ASCII.GetBytes(header)
           client.Send(sendbuffer, sendbuffer.Length, SocketFlags.None)

           Dim recievebytes As Byte() = New Byte() {}
           Do
               Dim recievebuffer(350) As Byte
               Dim bytesrecieved As Integer = client.Receive(recievebuffer, recievebuffer.Length, SocketFlags.None)
               Array.Resize(recievebytes, recievebytes.Length + bytesrecieved)
               Array.Copy(recievebuffer, 0, recievebytes, recievebytes.Length - bytesrecieved, bytesrecieved)
               If bytesrecieved < recievebuffer.Length Then Exit Do
           Loop
           Return recievebytes
       Else
           Return Nothing
       End If
   End Function

 

The code doesnt recieve the entire server response. What am i doing wrong?

Thanks in advance.

  • Administrators
Posted

IIRC the .Receive method will read whatever response the server has sent so far, if you call it while the server is still streaming data then you will only get a partial result. You would need to keep looping until the call to .Receive returns 0 as this means there was no data lft to be read.

 

http://msdn.microsoft.com/en-us/library/26f591ax.aspx#Y1200 has an example of what would be needed.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...