a few network procedures for everyone

Share your advanced PureBasic knowledge/code with the community.
User avatar
aszid
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 01, 2003 8:38 pm
Location: California, USA
Contact:

a few network procedures for everyone

Post 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
Last edited by aszid on Tue May 13, 2003 9:11 pm, edited 2 times in total.
--Aszid--

Making crazy people sane, starting tomorrow.
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

Everyone should use this procedures with care! No errorhandling on socket errors here. Loose of data is nearly garanted.
Tranquil
User avatar
aszid
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 01, 2003 8:38 pm
Location: California, USA
Contact:

Post 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)
--Aszid--

Making crazy people sane, starting tomorrow.
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post 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!!
Tranquil
ShDancer
User
User
Posts: 51
Joined: Sat Apr 26, 2003 5:49 am

Post by ShDancer »

Very nice code you have there, Tranquil.
But a simple question.

Can it be ported to Linux?

Thanks.
Post Reply