Page 1 of 1

Why does this code freeze

Posted: Fri Feb 01, 2013 10:42 pm
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

Re: Why does this code freeze

Posted: Fri Feb 01, 2013 11:13 pm
by luis
The browser keeps waiting for more data to come through the connection.

Code: Select all

size = SendNetworkString(ClientID,Result.s )
CloseNetworkConnection(ClientID)

Re: Why does this code freeze

Posted: Sat Feb 02, 2013 10:12 am
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.

Re: Why does this code freeze

Posted: Sat Feb 02, 2013 3:28 pm
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. :)

Re: Why does this code freeze

Posted: Sat Feb 02, 2013 5:51 pm
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.