Results 1 to 15 of 15

Thread: [2008] Find Socket Buffer Size

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    25

    Exclamation [2008] Find Socket Buffer Size

    I have connected to a host, but how do I find the correct buffer size?
    (The Buffer is currently set to 8k (8192))

    Code:
            Dim webget As New Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            webget.Connect(host, port)
            webget.Send(data, 0, data.Length, SocketFlags.None)
            Dim rData(8192) As Byte
            webget.Receive(rData, rData.Length, 0)

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Find Socket Buffer Size

    You can use buffers of any size, there is no correct size.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    25

    Re: [2008] Find Socket Buffer Size

    But, say I go to a long wikipedia page, If my buffer is, say 1024, not all the page will load.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Find Socket Buffer Size

    Yes, thats why you must keep on reading data from the stream and into the buffer until you've reached the end of the stream.

    In the specific case of HTTP responses, you should read from the stream until you have all the headers (that is, when you've reached the double linebreaks), then examine the content-length header. The value that this header holds tells you exactly how large the document in the HTTP body is. You could either declare a byte array (buffer) of that size exactly and read it all in one go, or declare a smaller byte array and continously read from the stream until you have read the entire response body.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    25

    Re: [2008] Find Socket Buffer Size

    Ok, Sounds complicated.
    What ive got currently is completly wrong then.
    Code:
            Dim rData(1024) As Byte
            webget.Receive(rData, rData.Length, 0)
    How do I stream the data into the buffer?

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Find Socket Buffer Size

    What you've got is not completely wrong. However..
    Theres a really simple way to do this. As soon as you've sent the request, create a StreamReader from your sockets stream.
    Now, call ReadLine to read one line from the stream, that is one headerline. Do this until ReadLine returns an empty line, this is the final linebreak that denotes the start of the response body.
    Now, go through the lines you read earlier to find the "Content-Length" header and retrieve its value. Voila, you now know how many more bytes there are in the stream, and you are positioned in the stream to read them all at once (this time use the Socket's Receive method like you're currently doing, and not the StreamReader).
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    25

    Re: [2008] Find Socket Buffer Size

    How do I create a streamreader from my sockets stream?

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Find Socket Buffer Size

    Since the Socket class itself doesnt expose a stream in a property or such, like the TcpClient does, one has to create a NetworkStream and pass the Socket in its constructor, and then pass that NetworkStream to the StreamReaders constructor:
    VB.NET Code:
    1. Dim reader As New System.IO.StreamReader(New System.Net.Sockets.NetworkStream(mySocket))
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    25

    Re: [2008] Find Socket Buffer Size

    So how do I use the streamreader to read the http response then?
    Ive got this so far:
    Code:
            webget.Send(data, 0, data.Length, SocketFlags.None)
            Dim reader As New System.IO.StreamReader(New System.Net.Sockets.NetworkStream(webget))
            MessageBox.Show(reader.ReadLine())
    How do I know when I get to the content length header, and the return line?

    Im just testing this:
    Code:
            Do Until reader.ReadLine = vbCrLf
                reader.ReadLine()
            Loop
    Doesent work...
    Last edited by NZHawk; Jan 5th, 2009 at 06:09 PM.

  10. #10
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Find Socket Buffer Size

    You could read each headerline into a collection and THEN search it to find the Content-Length one, OR you could do it while reading them.
    VB.NET Code:
    1. Dim tempHeaderLine As String
    2. Dim header As String = "content-length:"
    3. Dim contentLength As Integer
    4. Do
    5.     tempHeaderLine = reader.ReadLine()
    6.         If tempHeaderLine.StartsWith(header, StringComparison.CurrentCultureIgnoreCase) Then
    7.             contentLength = Integer.Parse(tempHeaderLine.Substring(header.Length).Trim())
    8.         End If
    9. Until tempHeaderLine = String.Empty
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    25

    Re: [2008] Find Socket Buffer Size

    Thats Great Thanks!
    Now I need to pass the contentLength value to the "Dim rData(1024) As Byte" size value, and recieve everything? Ill try it

    Thanks again and you missed a "Loop" from before the "Until" on the last line

    EDIT:
    Code:
            Dim rData(contentLength) As Byte
            webget.Receive(rData, rData.Length, 0)
            sck_server.Send(rData, rData.Length, SocketFlags.None)
    Doesn't Work, Im not sure why..

    EDIT2:
    Ok, I think that Ive firguard out why it was not working, When you load a page, you send the response and get a request. When I loaded up wikipedia, I got the initial page (A request to send to www.wikipedia,org) then when it tried to load a .jpg file, As there is no content length in the file, It wont work, This is what the web browser receives... and the program freezes

    Code:
    *J(??V?????(???( QE?	K?rll?Y
    ????E%?$w;	,I?x~N[u??b????????@%c?K??9L???J?g?`E??, ?AH*)D4Q?j?	EZN)?8?????{?L????#/8?c??{??g?]?JC?)'?????!J?D???????2?O???j9I;?e???N??cQi?p??Ns??n?>??????????
    X???&??Z?/?L?y?2????
    Also when I download a file through it, it doesnt work.? How could I get past this problem?
    Last edited by NZHawk; Jan 6th, 2009 at 01:33 AM.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    25

    Re: [2008] Find Socket Buffer Size

    And to confirm, when I manually change the buffersize to a big number, The pages load correctly.

    When I download a file through the proxy, This doesent work, as this program is downloading it first, then sending it to the client, How to I get it to stream the file to the Client?

  13. #13
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Find Socket Buffer Size

    There should always be a content-length header, no matter if the contents is a html file or an image.

    How does your code currently look?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    25

    Re: [2008] Find Socket Buffer Size

    ok,

    Code:
            webget.Send(data, data.Length, SocketFlags.None)
            Dim reader As New System.IO.StreamReader(New System.Net.Sockets.NetworkStream(webget))
            Dim tempHeaderLine As String
            Dim header As String = "content-length:"
            Dim contentLength As Integer
            Do
                tempHeaderLine = reader.ReadLine()
                If tempHeaderLine.StartsWith(header, StringComparison.CurrentCultureIgnoreCase) Then
                    contentLength = Integer.Parse(tempHeaderLine.Substring(header.Length).Trim())
                End If
            Loop Until (tempHeaderLine = String.Empty)
    
            Dim rData(contentLength) As Byte
            webget.Receive(rData, rData.Length, 0)
            TextBox2.Text = (System.Text.ASCIIEncoding.ASCII.GetString(rData))
    
            SendBack(rData)
    and send back is=

    Code:
        Private Sub SendBack(ByRef Data() As Byte)
            sck_server.Send(Data, Data.Length, SocketFlags.None)
        End Sub
    Last edited by NZHawk; Jan 6th, 2009 at 10:43 PM.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    25

    Re: [2008] Find Socket Buffer Size

    Also,
    When I use the gmail notifier it requests a CONNECT request with :443 as the port, If I set the port to this and try to connect to the host, I get this error: "An existing connection was forcibly closed by the remote host"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width