Need Help : Compress String for SendNetworkString()

Just starting out? Need help? Post your questions and find answers here.
tatanas
Enthusiast
Enthusiast
Posts: 186
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Need Help : Compress String for SendNetworkString()

Post by tatanas »

Hi,

I need your help.
I'm coding a client/server application and until now I was using SendNetworkString() to communicate. But the string is bigger and bigger (>10mb) so I would like to compress it.
Compress is not a problem (with compressmemory) but sending the data and receiving the data are. I should use SendNetworkData() instead of SendNetworkString() and the receiving is not a simple ReceiveNetworkData() + peekS anymore. I guess I should indicate the size of the compressed data in the packet but I'm a little lost...

Can someone suggest a example of client and server doing compressed data exchange ? Or another solution ?

Thanks for your time.
Windows 10 Pro x64
PureBasic 6.03 x64
User avatar
NicTheQuick
Addict
Addict
Posts: 1218
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Need Help : Compress String for SendNetworkString()

Post by NicTheQuick »

labefat181 wrote: Thu Jun 01, 2023 6:41 pm Here is an example of how to compress a string and send it over a network using PureBasic
That's just some bullshit from ChatGPT.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
benubi
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Mar 29, 2005 4:01 pm

Re: Need Help : Compress String for SendNetworkString()

Post by benubi »

You could send a header. It contains two longs: packed_size, unpacked_size. When they are both equal size the data is not packed. You send the header who is always 8 bytes long, and receive it the same way on the other size. Once received you read packed_size of bytes before receiving any other message from that connection. On the sender side you send the message (after the header) entirely before sending any other message.

Code: Select all

Structure myHeader
  packed_size.l
  unpacked_size.l
EndStructure

Procedure SendMyString(Connection, String$)
  Protected Header.myHeader
  protected str_bytes = StringByteLength(String$)
  
  ; compress string
  Header\packed_size = packed
  Header\unpack_size = str_bytes
  SendNetworkData(Connection, @Header, SizeOf(myHeader))
  
  ; Send data loop
EndProcedure

Procedure.s ReceiveMyString(Connection)
  Protected Header.myHeader
  Protected result$
  If ReceiveNetworkData(Connection, @Header, SizeOf(myHeader)) = SizeOf(myHeader)
    ; receive Loop
      result$ = space(Header\unpack_size / 2) ; we send packed unicode strings or you have to 
      ; Unpack received data to @result$
  EndIf
  ProcedureReturn result$
EndProcedure


User avatar
jacdelad
Addict
Addict
Posts: 1418
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Need Help : Compress String for SendNetworkString()

Post by jacdelad »

...or use one of the notwotk modules floating around here:
http://forums.purebasic.com/english/vie ... hp?t=73882
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
tatanas
Enthusiast
Enthusiast
Posts: 186
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: Need Help : Compress String for SendNetworkString()

Post by tatanas »

Thank you for your help.

I will try this module.
Windows 10 Pro x64
PureBasic 6.03 x64
Post Reply