Hello,
My program contently connects to (like every second) to the web server. On each request the server responds with a header of 4 bytes. But the problem is there is a lag between the time the request is sent and the time the server responds.
Is there any solution to lower this? I read about this Keep-Alive header.. will it help to solve the problem?
Thanks
Constant connection to HTTP server
-
- 666
- Posts: 1033
- Joined: Mon Sep 01, 2003 2:33 pm
why are you connecting every second? if you want to DOS your own server, you are succeeding for sure 
What are you trying to do? your question is just so sweeping that how can anyone offer help
but one piece of advice don't keep opening and closing connections. Open once... close when you exit the program or are certain you don't want to send/receive more data.

What are you trying to do? your question is just so sweeping that how can anyone offer help

but one piece of advice don't keep opening and closing connections. Open once... close when you exit the program or are certain you don't want to send/receive more data.
It seems I was correct. Here is the code
Code: Select all
InitNetwork()
time=ElapsedMilliseconds()
con=OpenNetworkConnection("localhost",80)
text$="GET /index.php HTTP/1.1"+Chr(13)+Chr(10)
text$+"Host: localhost"+Chr(13)+Chr(10)
text$+"Connection: Keep-Alive"+Chr(13)+Chr(10)
text$+"User-Agent: ChiranthaSoft - Update - 1.0"+Chr(13)+Chr(10)
text$+Chr(13)+Chr(10)
SendNetworkData(con,@text$,Len(text$))
*buf=AllocateMemory(1024)
While Not NetworkClientEvent(con)
Delay(10)
Debug "wait"
Wend
While 1
len=ReceiveNetworkData(con,*buf,1024)
Debug PeekS(*buf)
FreeMemory(*buf)
*buf=AllocateMemory(1024)
If len<>1024
Break
EndIf
Wend
Debug "DONE"
SendNetworkData(con,@text$,Len(text$))
*buf=AllocateMemory(1024)
While Not NetworkClientEvent(con)
Delay(10)
Debug "wait"
Wend
While 1
len=ReceiveNetworkData(con,*buf,1024)
Debug PeekS(*buf,len)
FreeMemory(*buf)
*buf=AllocateMemory(1024)
If len<>1024
Break
EndIf
Wend
CloseNetworkConnection(con)
Debug "Time Taken "+Str(ElapsedMilliseconds()-time)
-
- User
- Posts: 15
- Joined: Fri Mar 13, 2009 3:13 pm
- Location: Chennai, India
I have the exact same requirement. Instead of using PHP or other server side scripting language and having to use my Apache server for keeping my connections open, I decided to run my own PB server-side code that listens on port 81 (say) on a dedicated server 
So far my tests with Ubuntu works well - and that would mean I buying a Ubuntu dedicated server soon (yay!).
Here is the server-side code I wrote (PB 4.30).
This runs very stably so far. Any comments on improvements/loopholes are welcome!
Hope this helps!

So far my tests with Ubuntu works well - and that would mean I buying a Ubuntu dedicated server soon (yay!).
Here is the server-side code I wrote (PB 4.30).
Code: Select all
;
; This Code is Demo friendly
; You can Copy/Paste this on a new PB window and it should run properly.
;
EnableExplicit
If InitNetwork() = 0
MessageRequester("Error", "Can't initialize the network !", 0)
End
EndIf
Global EOL$ : EOL$ = Chr(13)+Chr(10)
Global ReceiveFile$
Define Port, Title$, OpenWindows
NewList Clients()
Declare ProcessLine(Line$)
Port = 81
Title$ = "My Daemon"
OpenWindows = 0
OpenConsole()
ConsoleTitle(Title$)
If CreateNetworkServer(#PB_Any, Port)
OpenWindows+1
PrintN("[ "+Title$+" ]")
PrintN("-----------------------")
PrintN("Listening on Port " + Str(Port) + "...")
Repeat
Define SEvent, EventClient.l, *Buffer, Result
SEvent = NetworkServerEvent()
If SEvent
EventClient.l = EventClient() ; (o.O) Long
Select SEvent
Case #PB_NetworkEvent_Connect
PrintN("[Server] Hurray! We have a new client: " + Str(EventClient)) ;]
Case #PB_NetworkEvent_Disconnect
PrintN("[Server] Client has disconnected: " + Str(EventClient))
Case #PB_NetworkEvent_Data
*Buffer = AllocateMemory(2200)
Result = ReceiveNetworkData(EventClient, *Buffer, 2000)
If Result = -1 ; Oops, something broke while we were getting the data ;(
Define Down$
Down$ = "EventClient#" + Str(EventClient) + " is down!"
Debug Down$ + " I Repeat, " + Down$
Else
Define Buffer$, Pos, BuffLen, LOEOL, NewPos, Line$
Buffer$ = PeekS(*Buffer)
; Debug *Buffer
Pos = 1
BuffLen = Len(Buffer$)
LOEOL = Len(EOL$)
Repeat
NewPos = FindString(Buffer$, EOL$, Pos)
If Not NewPos : Break : EndIf ; Not found anymore... ;(
Line$ = Mid(Buffer$, Pos, NewPos-Pos)
Debug Line$
PrintN(" [#"+Str(EventClient)+"] " + Line$)
ProcessLine(Line$) ; Special Treatments ;]
Pos = NewPos + LOEOL
Until Pos > BuffLen
EndIf
FreeMemory(*Buffer)
Case #PB_NetworkEvent_File
Debug "#PB_Event_FileReceived fired"
Define FileName$, FSize
If ReceiveFile$
FileName$ = GetTemporaryDirectory() + ReceiveFile$
ReceiveFile$ = ""
Else
FileName$ = GetTemporaryDirectory() + "ServerSide" + ".data"
EndIf
Result = ReceiveNetworkFile(EventClient, FileName$)
Debug Result
FSize = FileSize(FileName$)
PrintN(" [#"+Str(EventClient)+"] " + "Wow! Got Data File: " + FileName$ + "(Size:"+Str(FSize)+")")
Default
Debug "o.O: What's this?" + Str(SEvent) ;]
EndSelect
EndIf
Delay(10)
ForEver
Else
PrintN("Port "+Str(Port)+" already in Use!")
PrintN(EOL$+EOL$)
PrintN("Press any key to exit...")
Repeat : Delay(10) : Until Inkey() <> ""
EndIf
Procedure ProcessLine(Line$)
Define Pos, Cmd$, AfterCmd$
Debug "Line$:" + Line$
Pos = FindString(Line$, ":", 1)
If Pos
Cmd$ = Mid(Line$, 1, Pos)
AfterCmd$ = Mid(Line$, Pos+1, Len(Line$)-1)
Debug "Cmd$:" + Cmd$ + ", AfterCmd$:" + AfterCmd$
Select Cmd$
Case "FILE:"
If CheckFilename(AfterCmd$)
ReceiveFile$ = AfterCmd$
Debug "Set ReceiveFile$ to: " + AfterCmd$
EndIf
EndSelect
EndIf
EndProcedure
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;; ;;;;;;;;;;;;;
;;;;;;;; ;;;;;;;;
;;;;; ;;;;;
;;; ;;;
;; ;;
; The ;
End
; ;
;; ;;
;;; ;;;
;;;;; ;;;;;
;;;;;;;; ;;;;;;;;
;;;;;;;;;;;;; ;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Hope this helps!
