Page 1 of 1

WebServer (test)

Posted: Tue Mar 07, 2006 9:18 am
by Progi1984
Hi, i begin to use buffers and others pointers, i don't understand why that doesn't run when we test to access at the web server.

For having the error :
-launch the program
-launch a navigator
-access at 127.0.0.1

Code: Select all

;-Déclaration des globales
Global port.l
Global nameserver.s

;-Déclaration des fonctions
Declare NoGoth_Receive(*RBuffer,ClientID)
Declare NoGoth_RThread(message)
;-Initialisation des globales
port=80
nameserver.s="Server"

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

*Buffer = AllocateMemory(10000)
If CreateNetworkServer(0,port)
  Repeat
    Repeat
      WEvent = WindowEvent()
      If WEvent = #PB_Event_CloseWindow
        Quit = 1
      EndIf
    Until WEvent = 0
    SEvent = NetworkServerEvent()
    If SEvent
      ClientID.l = EventClient()
      ;Debug sevent
      Select SEvent
        Case #PB_NetworkEvent_Connect  ; When a new client has been connected...
        Case #PB_NetworkEvent_Data  ; When a client has closed the connection...
          ReceiveNetworkData(ClientID.l, *Buffer, 2000)
          ;Debug PeekS(*Buffer)
          NoGoth_Receive(*Buffer,ClientID)         
        Case #PB_NetworkEvent_Disconnect
        Case #PB_NetworkEvent_File
        Default
      EndSelect
    Else
     ; Delay(20)  ; Don't stole the whole CPU !
    EndIf
  Until Quit = 1
  CloseNetworkServer(0)
Else
  MessageRequester(nameserver, "Erreur : impossible de créer le serveur (port en utilisation ?).", 0)
EndIf

Procedure NoGoth_Receive(*RBuffer,ClientID)
  PokeS(*Rbuffer,PeekS(*Rbuffer)+Chr(3)+Str(clientid))
  CreateThread(@NoGoth_RThread(),*RBuffer)
EndProcedure

Procedure NoGoth_RThread(*message)
  message.s= PeekS(*message)
  pos=FindString(message,Chr(3),1)
  clientID=Val(Mid(message,pos+1, Len(message)-pos-1))
  type.s=Left(message,3)
  If type="GET"
    ;Debug "get"
  FreeMemory(*Buffer)
  header.s = "HTTP/1.1 200 OK"+Chr(13)+Chr(10)
  header = header+ "Date: Wed, 07 Aug 1996 11:15:43 GMT"+Chr(13)+Chr(10)
  header = header+ "Server: Atomic Web Server 0.2b"+Chr(13)+Chr(10)
  header = header+ "Content-Length: "+Str(141)+Chr(13)+Chr(10)
  header = header+ "Content-Type: text/html"+Chr(13)+Chr(10)
  header = header+ Chr(13)+Chr(10)
  header = header+ "texte"
 
  *Buffer=AllocateMemory(Len(header)+1)
  PokeS(*Buffer,header)
  Debug PeekS(*Buffer)
 
  ;*FileBuffer=AllocateMemory(Len("texte")+1)
  ;PokeS(*FileBuffer, "texte")
  ;Debug PeekS(*FileBuffer)
 
  SendNetworkData(ClientID, *Buffer,MemorySize(*Buffer));*FileBuffer-*Buffer+6)
  ElseIf type="POS"
    Debug "pos"
  ElseIf type="HEA"
    Debug "hea"
  EndIf
EndProcedure
Thank you !

Posted: Tue Mar 07, 2006 2:28 pm
by Trond

Code: Select all

;-Déclaration des globales 
Global port.l 
Global nameserver.s 

;-Déclaration des fonctions 
Declare NoGoth_Receive(*RBuffer,ClientID) 
Declare NoGoth_RThread(message) 
;-Initialisation des globales 
port=80 
nameserver.s="Server" 

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

*Buffer = AllocateMemory(10000) 
If CreateNetworkServer(0,port) 
  Repeat 
    Repeat 
      WEvent = WindowEvent() 
      If WEvent = #PB_Event_CloseWindow 
        Quit = 1 
      EndIf 
    Until WEvent = 0 
    SEvent = NetworkServerEvent() 
    If SEvent 
      ClientID.l = EventClient() 
      ;Debug sevent 
      Select SEvent 
        Case #PB_NetworkEvent_Connect  ; When a new client has been connected... 
        Case #PB_NetworkEvent_Data  ; When a client has closed the connection... 
          ReceiveNetworkData(ClientID.l, *Buffer, 2000) 
          ;Debug PeekS(*Buffer) 
          NoGoth_Receive(*Buffer,ClientID)          
        Case #PB_NetworkEvent_Disconnect 
        Case #PB_NetworkEvent_File 
        Default 
      EndSelect 
    Else 
      Delay(1)  ; Don't comment out this, your app will use 100% CPU if you do!!!!!!!
    EndIf 
  Until Quit = 1 
  CloseNetworkServer(0) 
Else 
  MessageRequester(nameserver, "Erreur : impossible de créer le serveur (port en utilisation ?).", 0) 
EndIf 

Procedure NoGoth_Receive(*RBuffer,ClientID) 
  PokeS(*Rbuffer,PeekS(*Rbuffer)+Chr(3)+Str(clientid)) 
  CreateThread(@NoGoth_RThread(),*RBuffer) 
EndProcedure 

Procedure NoGoth_RThread(*message) 
  message.s= PeekS(*message) 
  pos=FindString(message,Chr(3),1) 
  clientID=Val(Mid(message,pos+1, Len(message)-pos-1)) 
  type.s=Left(message,3) 
  If type="GET" 
    ;Debug "get" 
  FreeMemory(*Buffer) 
  header.s = "HTTP/1.1 200 OK"+Chr(13)+Chr(10) 
  header = header+ "Date: Wed, 07 Aug 1996 11:15:43 GMT"+Chr(13)+Chr(10) 
  header = header+ "Server: Atomic Web Server 0.2b"+Chr(13)+Chr(10) 
  header = header+ "Content-Length: "+Str(141)+Chr(13)+Chr(10) 
  header = header+ "Content-Type: text/html"+Chr(13)+Chr(10) 
  header = header+ Chr(13)+Chr(10) 
  header = header+ "texte" 
  
  *Buffer=AllocateMemory(Len(header)+1) 
  PokeS(@*Buffer,header) ; I think you need and @ here -----------------------
  Debug PeekS(*Buffer) 
  
  ;*FileBuffer=AllocateMemory(Len("texte")+1) 
  ;PokeS(*FileBuffer, "texte") 
  ;Debug PeekS(*FileBuffer) 
  
  SendNetworkData(ClientID, *Buffer,MemorySize(*Buffer));*FileBuffer-*Buffer+6) 
  ElseIf type="POS" 
    Debug "pos" 
  ElseIf type="HEA" 
    Debug "hea" 
  EndIf 
EndProcedure

Posted: Tue Mar 07, 2006 6:23 pm
by Progi1984
That doesn't run because the error is on this line :

Code: Select all

SendNetworkData(ClientID, *Buffer,MemorySize(*Buffer))

Posted: Tue Mar 07, 2006 8:18 pm
by Trond
Progi1984 wrote:That doesn't run because the error is on this line :

Code: Select all

SendNetworkData(ClientID, *Buffer,MemorySize(*Buffer))
Don't you think I know which line the error is on?

Posted: Tue Mar 07, 2006 8:26 pm
by Nik

Code: Select all

  *Buffer=AllocateMemory(Len(header)+1)
  PokeS(@*Buffer,header)
*Buffer is already the address of the memory if you dereference it you have the address of the pointzer where only 4 bytes are allocated, so it causes a problem!

Posted: Tue Mar 07, 2006 8:29 pm
by Trond
Nik wrote:

Code: Select all

  *Buffer=AllocateMemory(Len(header)+1)
  PokeS(@*Buffer,header)
*Buffer is already the address of the memory if you dereference it you have the address of the pointzer where only 4 bytes are allocated, so it causes a problem!
Ah, you're right. I hate this pointer usage, there's absolutely NO reason, WHAT SO EVER to put that asterisk there.

Did you compile with unicode on? Len() should return the number of characters in the string, not the byte length of the string.

Posted: Tue Mar 07, 2006 8:31 pm
by Nik
I simply love pointers, that's why I hate Java and .Net

Posted: Tue Mar 07, 2006 8:39 pm
by Trond
Nik wrote:I simply love pointers, that's why I hate Java and .Net
But there's no reason to put that asterisk there.

Why do you check for window events when you never opened any window?

Why do you pass the globally allocated memory to a thread and then continue to wait for more received data? (Which will be received into the same global buffer!)

Posted: Tue Mar 07, 2006 9:34 pm
by Progi1984
Finally, i realized what i want to do : Thank you Trond. You put me on the good way.

The code now :

Code: Select all

;-Déclaration des globales
Global port.l
Global nameserver.s

;-Déclaration des fonctions
Declare NoGoth_Receive(*RBuffer,ClientID)
Declare NoGoth_RThread(message)
;-Initialisation des globales
port=80
nameserver.s="Server"

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

*Buffer = AllocateMemory(10000)
If CreateNetworkServer(0,port)
  Repeat
    Repeat
      WEvent = WindowEvent()
      If WEvent = #PB_Event_CloseWindow
        Quit = 1
      EndIf
    Until WEvent = 0
    SEvent = NetworkServerEvent()
    If SEvent
      ClientID.l = EventClient()
      ;Debug sevent
      Select SEvent
        Case #PB_NetworkEvent_Connect  ; When a new client has been connected...
        Case #PB_NetworkEvent_Data  ; When a client has closed the connection...
          ReceiveNetworkData(ClientID.l, *Buffer, 2000)
          NoGoth_Receive(*Buffer,ClientID)         
        Case #PB_NetworkEvent_Disconnect
        Case #PB_NetworkEvent_File
        Default
      EndSelect
    Else
      Delay(1)  ; Don't comment out this, your app will use 100% CPU if you do!!!!!!!
    EndIf
  Until Quit = 1
  CloseNetworkServer(0)
Else
  MessageRequester(nameserver, "Erreur : impossible de créer le serveur (port en utilisation ?).", 0)
EndIf

Procedure NoGoth_Receive(*message,clientid)
  message.s= PeekS(*message)
  ;pos=FindString(message,Chr(3),1)
  ;clientID=Val(Mid(message,pos+1, Len(message)-pos-1))
  type.s=Left(message,3)
  If type="GET"
    Debug "get"
  FreeMemory(*Buffer)
  headerd.s = "HTTP/1.1 200 OK"+Chr(13)+Chr(10)
  headerd = headerd+ "Date: Tue, 07 Mar 2006 20:00:00 GMT"+Chr(13)+Chr(10)
  headerd = headerd+ "Server: My Web Server 0.2b"+Chr(13)+Chr(10)
  headerd = headerd+ "Content-Length: "
  
  headerf.s = Chr(13)+Chr(10)+ "Content-Type: text/html"+Chr(13)+Chr(10)
  headerf = headerf+ Chr(13)+Chr(10)
  content.s = "texte"
  
  clength= Len(content)
  header.s=headerd+Str(clength)+headerf+content
  
  *Buffer=AllocateMemory(Len(header)+1)
  Debug PeekS(*Buffer)
  PokeS(*Buffer,header) ; I think you need and @ here -----------------------
  Debug PeekS(*Buffer)
 
  ;*FileBuffer=AllocateMemory(Len("texte")+1)
  ;PokeS(*FileBuffer, "texte")
  ;Debug PeekS(*FileBuffer)
 
  SendNetworkData(ClientID, *Buffer,Len(PeekS(*Buffer)));*FileBuffer-*Buffer+6)
  ElseIf type="POS"
    Debug "pos"
  ElseIf type="HEA"
    Debug "hea"
  EndIf
EndProcedure

Posted: Tue Mar 07, 2006 10:20 pm
by Nik
Nik wrote:
I simply love pointers, that's why I hate Java and .Net
But there's no reason to put that asterisk there.
thats why I reported itz as non correct^^