Results 1 to 10 of 10

Thread: [RESOLVED] websocket

  1. #1

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Resolved [RESOLVED] websocket

    Hi,

    Im trying to use the following socket code to send an http request and get a reply from google. However nothing is coming back, the app just freezes.
    Any idea where i'm doing it wrong thanks?


    vb Code:
    1. sender2.Connect("google.com", 80)
    2.  
    3.         TextBox2.Text = ("Socket connected to {0}" & sender2.RemoteEndPoint.ToString())
    4.  
    5.         ' Encode the data string into a byte array.
    6.         Dim msg As String = ("POST / HTTP/1.1 \r\n" & Environment.NewLine & _
    7. "Host: google.com \r\n" & Environment.NewLine & _
    8. "Accept: */* \r\n>" & Environment.NewLine & _
    9. "Content-Type: text/html \r\n" & Environment.NewLine & _
    10. "Content-Length: 0 \r\n" & Environment.NewLine)
    11.  
    12.         ' Send the data through the socket.
    13.         Dim bytesSent As Integer = sender2.Send(Encoding.ASCII.GetBytes(msg))
    14.  
    15.         TextBox1.Text = msg
    16.  
    17.         ' Receive the response from the remote device.
    18.         Dim bytesRec As Integer = sender2.Receive(bytes)
    19.  
    20.         TextBox2.Text = ("Echoed test = {0}" & Encoding.ASCII.GetString(bytes, 0, bytesRec))
    21.  
    22.         ' Release the socket.
    23.         sender2.Shutdown(SocketShutdown.Both)
    24.         sender2.Close()
    25.         sender2 = Nothing


    Thanks

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: websocket

    Unfortunately I know nothing about accessing Web pages, but I do wonder about the strings.
    It looks like this may have come from a C based language, with string formatting escape characters (e.g. "\r\n" for Carriage Return and LineFeed) embedded in the strings, and then you add them manually with '& Environment.NewLine'.
    My assumption is you wouldn't want the "\r\n" in the strings since VB is not going to interpret then and add the Return and Linefeed characters in their place.
    Try removing them from the strings and see if it helps.

  3. #3
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: websocket

    The webclient and http web request class was made for a reason.

  4. #4

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: websocket

    @passel: I tried your suggestion , but no luck, thc tho

    @Toph: If i use http request i'm pretty sure it is gonna work. But if possible i would like to stick to the current socket code.

    more info :
    if I submit with the following header
    Code:
    Host: google.com
    Accept: */*
    Content-Type: text/html
    Content-Length: 0
    User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.91 Safari/537.36
    I get a 400 bad reqest msg "Your browser sent a request that this server could not understand."

    but if I try to add an "GET" or "POST" infront nothing comes back . anything special about the two ?


    thanks

  5. #5

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: websocket

    I got it to work
    First I removed \r\n and only used with Environment.NewLine as suggested by passel
    Then I was only missing an additional Environment.NewLine at the end of my request.

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: [RESOLVED] websocket

    The application freezes because you are performing the actions on the UI thread. I can't see any reason why you need the direct Httpwebrequest over the http wrapped webclient. If you don't want the UI to freeze then don't perform the action on the ui thread.
    My Github - 1d3nt

  7. #7

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: [RESOLVED] websocket

    @ident
    actually the reason it was freezing, was because i didn't send the end of request signal (Environment.NewLine), as a result the server was waiting for me to receive the next request element. (i normally don't expect a simple http head request to take more than 1 sec)

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: [RESOLVED] websocket

    Oh right you must be the first person to perform a heavy load request on the UI with out freezing.
    My Github - 1d3nt

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: [RESOLVED] websocket

    How can you expect a time limit? It's up to the server load you are connecting to.
    My Github - 1d3nt

  10. #10

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: [RESOLVED] websocket

    Yes, it's up to the server, but you would expect the response time to be reasonable loll.... And that's probably why they invented the TTL thing to handle this kind of situation.

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