Page 1 of 1

Problem with SendNetworkString and ReceiveNetworkData

Posted: Thu Feb 21, 2008 1:28 am
by TimeSurfer
Im having a problem using RecieveNetworkData on the serverside [so far] I send a string from the client via SendNetworkString and when i try to retrieve it using ReceiveNetworkData on the serverside it comes out all garbled except the last character. [basically it looks like null spaces to me or invalid chars cause its just a box lol] Im still new to pb too so this is a bit frustrating. I'm using pb4. [BTW the network client/server example in the pb4 help doc don't work either, i get the same effect]

Can anyone help me? I'm really confused.

Posted: Thu Feb 21, 2008 2:04 pm
by Tranquil
are you using unicode strings to be send over the network?

Posted: Thu Feb 21, 2008 3:58 pm
by Rook Zimbabwe
That is what I thought too!

Post some code for a test. Use the {code} {/code} commands (use [ for { and ] for } ) or click the Code button near the top white line and click it again when you have pasted your code. :D

We can see whats going on!

Posted: Sun Feb 24, 2008 6:10 am
by TimeSurfer
i found a way around that issue using a combination of peeks and pokes but now i have run into another problem.

my server code seems to work fine. now its this thats being a pain, it connects, sends the string but its not recieving the string thats sent back and ive checked the connectionid's over and over in both the server and client. im completely lost. --note; the code will be adapted for a dll so u might see some commented out procedurereturns.

Code: Select all

If InitNetwork() = 0
  ;ProcedureReturn "Unable to initialize the network."
EndIf

Procedure.s ReceiveNetworkString(ConnectionID.l) 
  Length.l = ReceiveNetworkData(ConnectionID, *Buffered, 1000) 
  Text$ = PeekS(*Buffered, Length)
  ProcedureReturn Text$ 
EndProcedure

*Buffered = AllocateMemory(1000)
ConnectionID = OpenNetworkConnection("127.0.0.1", 6832)
If ConnectionID
  stext.s = "regkey"
  PokeS(*Buffered,stext,1000)
  SendNetworkData(ConnectionID, *Buffered, 1000)
  Repeat
    CEvent = NetworkClientEvent(ConnectionID)
    If CEvent
      ;do something
      Select CEvent
        Case 1
          MessageRequester("PureBasic - Server", "Server "+Str(ConnectionID)+" has send a packet !", 0)
          MessageRequester("Info", ReceiveNetworkString(ConnectionID), 0)
          If Text$ = "Invalid registration key"
            CloseNetworkConnection(ConnectionID)
            End
          EndIf
      EndSelect
    EndIf
  Until Quit = 1
EndIf
End

Posted: Sun Feb 24, 2008 7:10 am
by Rook Zimbabwe
You have to clear the buffer and PARSE the buffer to sort the strings. Well you will eventually... you'll see.

Are you sending to the correct port?

Posted: Sun Feb 24, 2008 8:07 am
by Heathen
Shouldn't "Case 1" be "Case 2"?

Posted: Tue Feb 26, 2008 9:35 am
by TimeSurfer
thanks guys, got it working lol my problem was with my if statements. But you were right too heathen case 1 should be case 2.