Page 1 of 1

[All] Send/receive multiples files simultaneously sample

Posted: Wed Aug 14, 2002 3:25 am
by BackupUser
Code updated for 5.20+

Restored from previous forum. Originally posted by ricardo.

I was looking for a way to connect to a server and send and receive simultaneously 3 or more files or datas.

Here is one sample code of a possible solution, maybe some already knows it but could be usefull for beginners.
This connect 4 clients (from one single client app)using different threads for each client connection and then send some data and receive it and then display it.

What is was looking for is a way to connect (from one single app) to a server and receive/send various files simultaneously from/to the server.

ServerCode:

Code: Select all

Structure Type
  Cliente.b[100]
  Datos.b[100]
EndStructure


Recibe.Type

InitNetwork()

CreateNetworkServer(0,7777)


If OpenWindow(0,0,0,400,400,"Server",#PB_Window_SystemMenu)
  
  
  ContainerGadget(#PB_Any, 10, 10, 355, 150)
  EditorGadget(0,0,0,355,145)
  CloseGadgetList()
  StringGadget(1,10,160,355,20,"")
  StringGadget(2,10,180,355,20,"")
  StringGadget(3,10,200,355,20,"")
  StringGadget(4,10,220,355,20,"")
  
  
  
  Repeat
    EventID.l = WindowEvent()
    Delay(2)
    ServerEvent.l = NetworkServerEvent()
    ClientID = EventClient()
    Select ServerEvent
        
      Case 1
        SetGadgetItemText(0,-1,"Conectado: " + Str(ClientID) +  #CRLF$)
        
        
      Case 2
        ReceiveNetworkData(ClientID, Recibe, SizeOf(Type))
        Client$  =  PeekS(@Recibe\Cliente[0])
        Datos$   =  PeekS(@Recibe\Datos[0])
        Delay(10)
        
        SendNetworkData(ClientID,@Recibe, SizeOf(Type))
        SetGadgetText(Val(Client$),"ClientID: " +  Str(ClientID) + " : Data: " + Datos$ + " ThreadClient: " + Client$)
      Case 4
        SetGadgetItemText(0,-1,"Desconecta: " + Str(ClientID) +  #CRLF$)
        
    EndSelect
    
    
  Until EventID = #PB_Event_CloseWindow
EndIf
End
ClientCode:

Code: Select all

Structure Type
  Cliente.b[100]
  Datos.b[100]
EndStructure

Global Recibe.Type
Global Dim Regreso.Type(4)


InitNetwork()
Global ConnectionID.l
Global EventID.l
Global Dim gThreadID.l(4)



Procedure Conect(Cont.l)
  InitNetwork()
  PokeS(@Recibe\Cliente[0],Str(Cont))
  ConID = OpenNetworkConnection("127.0.0.1", 7777) 
  
  Delay(90)
  Repeat
    Delay(100)
    Generado.l = Generado + Random(100)
    PokeS(@Recibe\Datos[0],Str(Generado))
    PokeS(@Recibe\Cliente[0],Str(Cont))
    SendNetworkData(ConID,@Recibe, SizeOf(Type))
    
    ServerEvent.l = NetworkClientEvent(ConID)
    If ServerEvent
      
      Select ServerEvent
          
        Case 2
          ReceiveNetworkData(ConID,@Regreso(Cont), SizeOf(Type))
          Client$  =  PeekS(@Regreso(Cont)\Cliente[0])
          Datos$   =  PeekS(@Regreso(Cont)\Datos[0])
          If Client$
            SetGadgetText(Val(Client$),Datos$)
          EndIf
          
      EndSelect
      
    EndIf
    
  Until EventID = #PB_Event_CloseWindow
  End
EndProcedure


OpenWindow(numport,300,350,320,100,"Client",#PB_Window_SystemMenu)
StringGadget(1,10,10,300,20,"")
StringGadget(2,10,30,300,20,"")
StringGadget(3,10,50,300,20,"")
StringGadget(4,10,70,300,20,"")

For i = 1 To 4
  gThreadID(i)= CreateThread(@Conect(),i)
  Delay(500)
Next i

Repeat
  EventID.l = WindowEvent()
  Delay(2)
  
Until EventID = #PB_Event_CloseWindow
End

Posted: Wed Aug 14, 2002 3:08 pm
by BackupUser
Restored from previous forum. Originally posted by pusztry.

Where do you get the support for Rich Edit.

Thanks

Ryan

WinXP, PIII 800 MHz, 512MB RAM, SB Live 5.1, NVidia TNT 2 Ultra

Posted: Wed Aug 14, 2002 3:27 pm
by BackupUser
Restored from previous forum. Originally posted by Paul.

You can download the RichEdit ASM library from the PB Resources Site...
http://www.reelmediaproductions.com/pb