;-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
*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!
*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 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!)
;-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