Why does this code freeze

Just starting out? Need help? Post your questions and find answers here.
PeterBotes
User
User
Posts: 63
Joined: Tue Nov 15, 2011 2:12 pm

Why does this code freeze

Post by PeterBotes »

Anyone any ideas why this does not show the page until I stop the program?

What I was playing with is accepting data from a browser and dishing up dynamic pages, if I leave out this line "ReceiveNetworkData(ClientID, *Buffer, 4000)", I can see the text but it loops forever!

Code: Select all

If InitNetwork() = 0
  MessageRequester("Error", "Can't initialize the network !", 0)
  End
EndIf

Port = 80

*Buffer = AllocateMemory(4000)

Quit=0

If CreateNetworkServer(0, Port)


  If OpenWindow(0, 0, 0, 422, 180, "" , #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TextGadget(5, 10,  50, 450, 50, "")  
    SetGadgetText(5,"Server created.")

    Repeat
      
      Event = WindowEvent()
      SEvent = NetworkServerEvent()
    
      If SEvent
      
        ClientID = EventClient()
    
        Select SEvent
        
          Case #PB_NetworkEvent_Connect
            SetGadgetText(5,"Client connected")
    
          Case #PB_NetworkEvent_Data
            ReceiveNetworkData(ClientID, *Buffer, 4000)
            Result.s= "<html>"+#CRLF$+"<body>"+#CRLF$+"<h1>Welcome To your Webspace</h1>"+#CRLF$+"</body>"+#CRLF$+"</html>"+#CRLF$
            size = SendNetworkString(ClientID,Result.s )
            
          Case #PB_NetworkEvent_Disconnect
            SetGadgetText(5,"Client closed")
      
        EndSelect
      EndIf
      
      Select Event
        Case #PB_Event_CloseWindow          
          Quit=1
      EndSelect
      Delay(1)

    Until Quit = 1 
  EndIf
  
  CloseNetworkServer(0)
Else
  MessageRequester("Error", "Can't create the server (port in use ?).", 0)
EndIf

Thanks Pete
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Why does this code freeze

Post by luis »

The browser keeps waiting for more data to come through the connection.

Code: Select all

size = SendNetworkString(ClientID,Result.s )
CloseNetworkConnection(ClientID)
"Have you tried turning it off and on again ?"
A little PureBasic review
PeterBotes
User
User
Posts: 63
Joined: Tue Nov 15, 2011 2:12 pm

Re: Why does this code freeze

Post by PeterBotes »

Thanks luis, OK now I want to read the URL passed to the program so I can type this in my browser "http://MyIPAddress/SomeData" then in my code
do something like

Code: Select all

if URLParam1$="SomeData" 
  . ................
endif
Sorry if this is simple but I have been looking for hours and can't figure how to do it.

Pete.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Why does this code freeze

Post by Shield »

Don't forget browsers use the HTTP protocol to communicate with a server.
There is a lot more to be taken care of than just send HTML markup back.
You have to parse the header sent by the browser (this will give you the URL) and
then act accordingly. Another important feature to support is being able to send
the data in chunks. Almost all browsers expect your server to be able to do this. :wink:

Check out some of the examples here or in the code archive.
Also, PB comes with a very small example of a web server, you should check it out. :)
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Why does this code freeze

Post by luis »

PeterBotes wrote:OK now I want to read the URL passed to the program so I can type this in my browser "http://MyIPAddress/SomeData" then in my code do something like
You can just parse the *Buffer var and there you will find what you are looking for. The data can be urlencoded so you have to decode it, see URLDecoder().
Obviously you can follow almost any rule you like if your server is private, but it's better to stick to some common ground (your url above for example it's a path, /SomeData it's not a good way to pass your data).

See -> http://en.wikipedia.org/wiki/Query_string for a more standard format to follow.
"Have you tried turning it off and on again ?"
A little PureBasic review
Post Reply