WebServer (test)

Just starting out? Need help? Post your questions and find answers here.
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

WebServer (test)

Post 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 !
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

That doesn't run because the error is on this line :

Code: Select all

SendNetworkData(ClientID, *Buffer,MemorySize(*Buffer))
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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?
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post 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!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Last edited by Trond on Tue Mar 07, 2006 8:34 pm, edited 1 time in total.
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

I simply love pointers, that's why I hate Java and .Net
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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!)
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post 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
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post 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^^
Post Reply