I'm stupid or what!!??

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Hi all.

I'm still working on an edonkey clone. Till now everything works fine but in the last weeks I found out that the receiving and send of datas through the network is not so easy as it look on the first view.
I wrote two procedures which should send and receive a string till it is complete. It works - SOMETIMES.

If I send some long Datastrings to the local host server I loose Data and I dont know why.
Here are my two procedures I wote:

Code: Select all


Procedure send(id,txt$)
  ; calculate bufferlength
  length=Len(txt$)+2                  ; String + EOL
  length+4                            ; Packet "length"-Long at beginning
  *mem.l=AllocateMemory(0,length,0)   ; Allocate our memory
  PokeL(*mem.l,length)                ; Store the length of our Datapacket
  PokeS(*mem.l+4,txt$)                ; Now Store the string
  send_(id,*mem.l,length,0)           ; Send it to our client
  FreeMemory(0)                       ; Free the memory again
EndProcedure

Procedure receive(id)  
  *mem.l=AllocateMemory(0,4,0)         ; alloc 4 Bytes of memory for our header
  re=recv_(id,*mem.l,4,0)              ; Read our headers first 4 bytes
  packetsize.l=PeekL(*mem.l)           ; Get our Packetsize length
  length=packetsize.l-4                ; -4 couse we still have the header
  FreeMemory(0)                        ; Free our 4 Bytes again
  *mem.l=AllocateMemory(0,length,0)    ; Allocate the mem for the needed rest
  Repeat                               ; Read ALL our Datas till its complete
    re=recv_(id,*mem.l,length,0)       ; Read the RAW Datas and count received bytes in re
    received+re                        ; Received charts + re
  Until received=length                ; Till we have all our datas complete
  ProcedureReturn *mem.l               ; Return the memory address of our datas
EndProcedure

  txt$=peeks(*mem.l)                   ; To get the String we want
  freememory(0)                        ; Free its memory again

So far. now open a server and a client and let the client do the following:

for n=0 to 1000:send(serverID,"blblabla this is a test text"):next n

You will see that not all datas are received on the other side. And now, I want to know why! :)))

Please help me stupid guy. *fg*

Cheers
Mike

Tranquilizer/ Secretly!
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

I think your recieve procedure is faulty, i.e. you've made a logical error in the repeat-loop. As you have made it now -if you recieve a tcp-packet that contains only partially of what you've send you keep overwriting this same memory again and again. I would suggest this method, or something like this to overcome your problems:

Code: Select all

Procedure receive(id)  
  *mem.l=AllocateMemory(0,4,0)         ; alloc 4 Bytes of memory for our header
  re=recv_(id,*mem.l,4,0)              ; Read our headers first 4 bytes
  packetsize.l=PeekL(*mem.l)           ; Get our Packetsize length
  length=packetsize.l-4                ; -4 couse we still have the header
  FreeMemory(0)                        ; Free our 4 Bytes again
  *mem.l=AllocateMemory(0,length,0)    ; Allocate the mem for the needed rest
  Repeat                               ; Read ALL our Datas till its complete
    re=recv_(id,*mem.l,length-recieved,0) ; <--Changed
    received+re                        ; Received charts + re
    *mem+re                            ; <--Changed!!!
  Until received=length                ; Till we have all our datas complete
  ProcedureReturn UseMemory(0)         ; <--Changed!! Return the memory address of our datas
EndProcedure
I haven't tested it but i hope you'll see what im trying to say with the changes that i've made. Maybe the code needs a little further tweaking but the logic of it is sound, at least i hope that it is;)

Good luck with your clone!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Hi Pupil, thanks for your help. You are right, there was a logical mistake in it. I tried your code, it works but still datas are missing and I dont know why. :-( Where are my data packets? I will upload my two little sourced to my webaccount this evening so that everyone can try.

Thanks a lot for your help!!!!

Tranquilizer/ Secretly!
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Here you can find both sources working on local host, would be nice if someone can give suggestion and results.

Server: (Start first!)

http://www.secretly.de/seedserver.pb

Client: (Sends the datas)

http://www.secretly.de/clienttest.pb


Thanks a lot!

Cheers
Mike

Tranquilizer/ Secretly!
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
Here you can find both sources working on local host, would be nice if someone can give suggestion and results.

Server: (Start first!)

http://www.secretly.de/seedserver.pb

Client: (Sends the datas)

http://www.secretly.de/clienttest.pb


Thanks a lot!
Hi,
your code works as intended here, no problems at all!
Tested on WIN98SE
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fweil.

Hi,

Works fine on my W2K.

...

Francois Weil
14, rue Douer
F64100 Bayonne
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Thats crazy!!

All 500 Packages arrived the server??
On my Win2k Server and Professional more then 90% of Packets get lost. :P

Mike

Tranquilizer/ Secretly!
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
Thats crazy!!

All 500 Packages arrived the server??
On my Win2k Server and Professional more then 90% of Packets get lost. :P
I only answer for my self but yes, all 501 (to be exact) packets arrived intact. I even increased the size of the string to about 3k and still everything worked ok.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

> I'm stupid or what!!??

hmmm...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Worked fine here too. 501 packet got (WinXP Pro).

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TronDoc.

I'm not sure if I got all the packets or whatever. I don't know how to test for that. I did try out the server and client. The server reported a client connected and there were a bunch of the same lines displayed and the client said it had completed...? oops! I extended the display line in the server and saw the number at the end, yes, all 501 were displayed. On two attempts to run the client though I did receive a repeated "socket error!", but on the third attempt it worked.

A question: Is this code using the normal internet protocols or the inbuilt PureBASIC stuff?

elecTRONics DOCtor
{registeredPB}P150 32Mb w98/DOS/Linux NO DirX NO IE :wink:

Edited by - TronDoc on 02 June 2002 05:32:34
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

It uses the normal TCP/IP internet protocol and winapi to send and receive the datas.

Thanks to all for their help.

Mike

Tranquilizer/ Secretly!
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Hi Tranquil,
code tested, all 501 line received.
Suppose the next step would be to test server and client connected through 2 different machines.

My test configuration:
WinXP Home with ZoneAlarm.


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Hm, its very mysterious. I tried it on local host, I tried it in my LAN and everytime I loose my datas and Recv_() returns a socket error. After that PB crashes. Need to take a closer look by using the wsagetlasterror_() or something like that. But thanks anyway´.

Tranquilizer/ Secretly!
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Branston.

Hi Tranquil.

I tried your code here on two machines. I keep getting "Socket error!" repeatedly on the client machine. OS is WIN98SE.
Post Reply