server-client include

Share your advanced PureBasic knowledge/code with the community.
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

server-client include

Post by gnasen »

hi folks,

at first, sorry for my bad english.

I think it would just be fair to share some code, because I really liked and even like to use code from the forum. I tried to make it as easy as possible to use.

Its an include file to use server client communication. It allow you to:
-send Datablocks from the RAM
-Files from the HDD.
-handle different clients at the same time
-give feedback of incoming events (*mem,size,type,path,time,user)
-> which can be handled differently
-sending and receiving at the same time, different files/datas (see _sc_set_dynamic_mode())
-connect gapped transmissions
-supports unicode
-and some secret features !? :P

new version!
http://www.file-upload.net/download-162 ... t.rar.html

<<<< Removed examples, there is one for peer2peer file transfering added in the download

I already tested it, but if you can find a bug, pls post it.
At the moment for 32bit OS, Windows only, PB 4.2 (very easy to change for linux use, 64bit not tested yet)

greets gnasen
Last edited by gnasen on Wed May 06, 2009 6:54 pm, edited 8 times in total.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

How does it work with UNICODE?
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post by gnasen »

supports unicode & ascii

because it does not seems like that i could delete this post, I will use the edit function to make something usefull with it ;)

Procedures:
_sc_server_start(port.l)
creates the Server
_sc_server_stop()
shuts the Server down
_sc_client_connect(ip.s,port.l)
makes you able to connect to the specified server
_sc_client_disconnect()
closes the connection to the server
_sc_add_send_data(con_id,typus,mempos,Size)
adds the specified memory to the sending queue
_sc_add_send_file(con_id,typus,fileSourcePath.s,fileDestinationPath.s)
allows to send a file to the specified folder and file
_sc_loop_get(IsServer.l,timeout.l=1)
gets all incoming network data and stores them to the list: _sc_net_get_store
_sc_loop_send(timeout.l=1)
sends messages from the queue. Messages bigger than #_sc_max_bytes_send will be splitted
_sc_set_dynamic_mode(dynamic.l)
enables to send different messages at the same time, like more than one file, etc. If it is disabled, works the FIFO order.


I would like to know what you think / if it works
I will use it in a bigger project and can check on this way, if it runs without any errors.
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post by gnasen »

new version available, forgot to update the post for some time :oops:

-better example added
-some functions improved

Im working with it for some time now. Runs on a linux server (some little changes, nothing special, only timer api removed) for some time without any errors until yet. Seems to be stable ;)

have fun
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Post by QuimV »

@gnasen
Report a bug? :)
Sending an exe file (xmlmarker.exe) from client to server I get the next indication on server screen:
in/out = in
Typ = 4
Status = canceled
Pfad = income\xmlmarker.exe
I use PB4.3-B5. Is correct this behabiour?
Thanks in advanced
:)
QuimV
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post by gnasen »

There are 3 reasons, why a file download can be canceled from the server.
1. The Client disconnects (dont hope so ;) )
2. The transfer times out (#_sc_max_wait, normally = -1, what means ignore timeouts)
3. The Server cant create a file to write the memory on the HDD.

Are you running Vista (especially 64bit)?
Is the application allowed to write files in the directory? Be careful, it uses relative paths by default. That means if you want to test it, it would be the best to create an exe and run it in a folder where you know that you are allowed to. Compiling it from the IDE lets it run in the PB-path!

I used 4.2 to develop it, but I dont think that there is any important change which would cause it.

gnasen

PS: Pfad = Path (german...). Typ = 4 means File transfer, so far it seems to work (for different typs check the constants). At which point (x%) does it break?
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Post by QuimV »

:) Hi gnasen,
Thanks for your response.
I have some more news.
The problem appears if I compile with PB4.3-B5.
If I compile with PB4.2, the programm runs fine.
So, I'll compile with PB4.2 and I'll wait your update.
Thanks again

:)
QuimV
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post by gnasen »

did they change so much? amazing....

I think it will take a while to update it, because the main program im working on is running 4.2 and i wont change the pb version until it is finished.

The source is open, maybe you can have a try ;)
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Post by QuimV »

OK gnasen :D
I'll try to update it
:wink:
and Thanks
QuimV
MyTrial
Enthusiast
Enthusiast
Posts: 165
Joined: Thu Nov 30, 2006 11:47 am

Post by MyTrial »

Hi gnasen

thanks for your shared network part. The link "new version" does not work anymore. Is there a new part anywhere else?

I wanted to use your fine work in a application of mine but get errors in your "server_client.pbi". On line 529 I get "specified address is null". And on line 455 I get "Invalid memory access (read error at address 0)".

I changed the parts for purebasic 4.30 like you described in the german forum (19200).

Do you have any help for me? Or did I something wrong, which I have overseen? Thanks for your response.

Sigi
MyTrial
Enthusiast
Enthusiast
Posts: 165
Joined: Thu Nov 30, 2006 11:47 am

Post by MyTrial »

I added only this procedure to easy handle strings. Is here something wrong?

Code: Select all

Procedure _sc_add_send_string(con_id,content.s)
  Protected Size.q
  
  _sc_client_msg_counter + 1
  
  Size = Len(content) * _sc_charlength
  
  If content
  
    LastElement(_sc_net_send())
    AddElement(_sc_net_send())
    
    _sc_net_send()\send_mempos  = AllocateMemory(Size)  
    CopyMemoryString(content,_sc_net_send()\send_mempos)
    
    _sc_net_send()\send_file        = 0
    _sc_net_send()\send_filePath    = ""
    _sc_net_send()\send_filePathLen = 0
    _sc_net_send()\send_con_id      = con_id
    _sc_net_send()\send_Typus       = #_sc_typus_string
    _sc_net_send()\send_size        = Size
    _sc_net_send()\send_current     = 0
    _sc_net_send()\send_partA       = 1
    
    _sc_net_send()\send_partB   = Size/#_sc_max_bytes_send+1
    
    If Size%#_sc_max_bytes_send = 0
      _sc_net_send()\send_partB-1
    EndIf
    
    _sc_net_send()\send_msg_id  = _sc_client_msg_counter
    
    ProcedureReturn #True
    
  Else
    
    ProcedureReturn #False
    
  EndIf
  
EndProcedure
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post by gnasen »

I think it would have been more easy to make a macro, just as another idea.

Just saw a little mistake, you have to change the memory stuff like this:

Code: Select all

CopyMemory(@content,_sc_net_send()\send_mempos,Size)
You can also send a string like this:

Code: Select all

Define bla.s = "asdasd"
 _sc_add_send_data(conid,#_sc_typus_string,@bla,Len(bla)*_sc_charlength+1)

Edit:
I uploaded it again, there stood it has been removed because of abusing!?
pb 5.11
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

Looks interesting, I'm making some stuff like this myself so its fun to see how other people do things differently, etc.

But do you think this will work in a threaded application, if you want to have multiple downloads and uploads at the same time?
I like logic, hence I dislike humans but love computers.
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post by gnasen »

it has multiple uploads and downloads at the same time, but without threads (i dont like them) ;)

The example is a little peer2peer application where you can send different files between client and server at the same time. However the data you send first will be send primary, so if you send a couple of files, some will be faster than other.
pb 5.11
MyTrial
Enthusiast
Enthusiast
Posts: 165
Joined: Thu Nov 30, 2006 11:47 am

Post by MyTrial »

Thanks gnasen, now it works fine.
Post Reply