Page 1 of 1

Network bug

Posted: Wed Jan 07, 2004 6:28 pm
by merendo
Hi drinkingmates *rofl*

Seriously, i have a problem with my network. Look at the two codes i got for you:

Server code:

Code: Select all

messageA$="Dis is da first message"
messageB$="This is the second message"

OpenWindow(0,0,0,0,0,#PB_Window_SystemMenu,"Server")
InitNetwork()
If CreateNetworkServer(6789)
  Repeat
    event=NetworkServerEvent()
    If event = 1
      ID.l=NetworkClientID()
      SendNetworkData(ID,@messageA$,Len(messageA$)+1)
      SendNetworkData(ID,@messageB$,Len(messageB$)+1)
      MessageRequester("mesg","Data has been sent")
    EndIf
  Until WindowEvent()=#PB_EventCloseWindow
EndIf
Client code:

Code: Select all

OpenWindow(0,0,0,0,0,#PB_Window_SystemMenu,"Client")
InitNetwork()
AllocateMemory(0,1000)

Connection.l=OpenNetworkConnection("127.0.0.1",6789)
If Connection
  MessageRequester("mesg","Connected...")
  Repeat
  Delay(10)
    event = NetworkClientEvent(Connection)
      If event = 2
        Delay(2500) ;delay some time to make sure that ALL data is received...
        received.w=ReceiveNetworkData(Connection,MemoryID(),1000)
        input$=PeekS(MemoryID(),received)
        MessageRequester("input",input$)
      EndIf
  Until WindowEvent()=#PB_EventCloseWindow
EndIf
Please compile both codes into two executables. Then run first the server app and after this, run the client app.

It seems as when i send two messages directly one after the other, only the first message arrives.

At first, the client says: Okay, connected and the server says 'okay i have sent the data.' after a short moment, the client says 'i have received some data: Blah blah blah'. But for me, it shows only the first message, not the second....

Is this just some weird behaviour of my machine or does the bug also appear for you?

Bye! merendo

Posted: Wed Jan 07, 2004 7:07 pm
by ebs
merendo,

It's not a bug in the network code. If you check the value of "received", you'll see it is 51. This is the length of both messages, including the terminating zero bytes.

The problem is that the data in the buffer has a zero byte between the first and second messages. I'm pretty sure "PeekS()" will stop at a zero byte, even if the "length" parameter is bigger, so "input$" contains only the first message.

The basic issue is that PB uses null-terminated strings. You may have to manually search for and replace the zero bytes between messages.

Something like this should work:

Code: Select all

      .
      .
      .
      received.w=ReceiveNetworkData(Connection,MemoryID(),1000)
      For n.l = 0 To received
        If PeekB(MemoryID()+n) = 0
          PokeB(MemoryID()+n,13)
        EndIf
      Next
      input$=PeekS(MemoryID(),received)
      .
      .
      .
Eric

Posted: Wed Jan 07, 2004 7:26 pm
by merendo
Hmm... i am gonna try it.

Perhaps the manual should include a note about this. Fred?

Posted: Wed Jan 07, 2004 7:34 pm
by merendo
God....... This fixes a bug in my project i've been hunting for ages....
Thank you, ebs... If i could, i would kiss you! :twisted:

Please, Fred, include a note about the little 'bug' in the network (which is not a real bug). I don't want anyone else face this problem.

Posted: Wed Jan 07, 2004 7:40 pm
by Henrik
Hmm maybe some notes about it, in the string section of the manual
But it was because you used Peeks
if you look up about Peeks you'll see
PeekS()

Syntax

Text$ = PeekS(*MemoryBuffer [, Length])
Description

For advanced programmers only. Very useful to catch a string at the specified memory address. The string should be ended by a '0' character, else it will read the memory until a '0' is encounter. An optionnal length parameter can be specified.
Supported OS

Windows, AmigaOS, Linux
Best regards
Henrik

Posted: Wed Jan 07, 2004 9:21 pm
by ebs
merendo,
God....... This fixes a bug in my project i've been hunting for ages....
Thank you, ebs... If i could, i would kiss you!
Thanks for the offer (you're making me blush :oops: ), but...

Eric

Posted: Wed Jan 07, 2004 9:21 pm
by merendo
Well, that's true. But i think it should note that even if a lenght parameter is given, the command will only read until a Null is fount. I thought, that if lenght is given, the command just ignores NULL....

Posted: Wed Jan 07, 2004 10:07 pm
by Henrik
agree :wink:

Henrik

Posted: Wed Jan 07, 2004 10:36 pm
by ebs
merendo,

I've got a better answer than my first one. Instead of searching through the received data and replacing the zero bytes, try this:

Code: Select all

OpenWindow(0,0,0,0,0,#PB_Window_SystemMenu,"Client")
InitNetwork()
AllocateMemory(0,1000)

Connection.l=OpenNetworkConnection("127.0.0.1",6789)
If Connection
  MessageRequester("mesg","Connected...")
  Repeat
    Delay(10)
    event = NetworkClientEvent(Connection)
    If event = 2
      ;Delay(2500) ;delay some time to make sure that ALL data is received...
      received.w=ReceiveNetworkData(Connection,MemoryID(),1000)
      Repeat
        input$=PeekS(MemoryID()+offset.l,received)
        MessageRequester("input",input$)
        length.l = Len(input$) + 1
        offset + length
        received - length
      Until received = 0
    EndIf
  Until WindowEvent()=#PB_EventCloseWindow
EndIf
The loop should be much more efficient than examining every character.
You probably don't need the delay, but it certainly can't hurt.

So what will you do for me now??? :wink:

Posted: Thu Jan 08, 2004 1:48 pm
by merendo
Err, YOU could kiss ME... But seriously, thanks for the second code. However, i will solve this problem reading each byte from the input buffer and then add it to the input var. But sure, your solution is much more time-saving but i think that such small memory operations are not that time intensive....

Thanks again!