Page 1 of 1

a few network procedures for everyone

Posted: Mon May 12, 2003 10:46 pm
by aszid
since pb was lacking getnetworkstring() commands, i made a few of my own for an application i'm working on. feel free to use them as you please. i made 2 different ones, one that waits for data to be recieved (and times out after 30 seconds of waiting), and the other that assumes data is there & ready to be read

(you may want to de-allocate the buffer memory after use, i don't simply because my app uses it repeatedly, and each time it's re-allocated it frees the old memory)

Code: Select all


Procedure.s WaitNetworkString(conn)
  secstamp = Date()
  Repeat
    winact = WindowEvent()
    If winact = #PB_EventGadget
      If EventGadgetID() = #Cancel  ; this is my cancel button
        End
      EndIf
    EndIf
    netact = NetworkClientEvent(conn)
    Delay(20)
    If Date() > secstamp + 30
      netact = 10
    EndIf
  Until netact <> 0
  If netact = 10
    MessageRequester("Connection error","Server has timed out",0)
    ProcedureReturn ""
  Elseif netact = 2
    *buffer = AllocateMemory(#1,10240 , 0)
    strlen = ReceiveNetworkData(servconn,*buffer,10240)
    rdata$ = PeekS(*buffer)
    rdata$ = Left(rdata$,strlen)
    ProcedureReturn rdata$
  EndIf
EndProcedure 

Procedure.s GetNetworkString(conn)
  *buffer = AllocateMemory(#1, 10240 , 0)
  strlen = ReceiveNetworkData(servconn,*buffer,10240)
  rdata$ = PeekS(*buffer)
  rdata$ = Left(rdata$,strlen)
  ProcedureReturn rdata$
EndProcedure 

Aszid

Posted: Tue May 13, 2003 1:46 pm
by Tranquil
Everyone should use this procedures with care! No errorhandling on socket errors here. Loose of data is nearly garanted.

Posted: Tue May 13, 2003 9:10 pm
by aszid
why don't you suggest some changes then? i looked it over and i can't see the problem you're talking about. and from my use of these routines, i've yet to have any problems. I'm not doubting what you say, but saying something like that doesn't do me any good. where-as saying there's problems then suggesting solutions would be helpful.

(I did find a few other minor bugs though, and fixed those)

Posted: Wed May 14, 2003 9:40 am
by Tranquil
viewtopic.php?t=3812&highlight=correctly+network

and two thousend times more. Just search the forum.
You can try your routine yourselfe. LocalHost works allmost fine couse its very fast but in LAN or Internet you have to check some error/ warning messages from windows. VERY IMPORTEND!!

Posted: Wed May 14, 2003 1:41 pm
by ShDancer
Very nice code you have there, Tranquil.
But a simple question.

Can it be ported to Linux?

Thanks.