Page 1 of 1

Getting data from a server, login fails

Posted: Mon Aug 04, 2014 1:19 pm
by Poltergeist
For some time now I've been trying to setup a connection to a certain server. In C#, this works, but I cannot get it to work in Purebasic.

Code: Select all

            TcpClient client = new TcpClient();
            client.Connect("servername",port);

            char chr0 = (char)0;
            char chr1 = (char)1;
            string input = String.Format("{0}username{1}password{2}", chr1, chr0, chr0);

            // Stream string to server
            NetworkStream s = client.GetStream();
            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes(input);
            s.Write(ba, 0, ba.Length);

            // Read response from server.
            byte[] buffer = new byte[1024];
I've tried several ways of sending the data, but I think it might fail on the 0 character in the string. Is there anyway to get this working?

Code: Select all

logondata=chr(1)+username+chr(0)+password+chr(0)
If Initnetwork()
  ConnectionID=OpenNetworkConnection(address,port)
  if ConnectionID
    Debug SendNetworktring(ConnectionID,logondata,len(logondata)
    Repeat
      Select NetworkClientEvent(connectionID)
        Case #PB_NetworkEvent_Disconnect
          Debug "disconnected":End
        Case #PB_NetworkEvent_Data
          Debug ReceiveNetworkData(ConnectionID,*buffer,1024)
          Debug PeekS(*buffer)
        Case #PB_NetworkEvent_None
          Debug "no data"
      EndSelect
      Delay(100)
    Until Quit=1

  EndIf
EndIf

Re: Getting data from a server, login fails

Posted: Mon Aug 04, 2014 3:54 pm
by infratec
Hi,

if you want (or need) to send something with 0 included,
you need SendNetworkData() and not SendNetworkString().

Bernd

Re: Getting data from a server, login fails

Posted: Mon Aug 04, 2014 10:21 pm
by Poltergeist
I tried that also, but the effect is the same. Does not work, unfortunatly...

Code: Select all

logondata.s=Chr(1)+"username"+Chr(0)+"password"+Chr(0)
*buffer=AllocateMemory(Len(logondata))
PokeS(*buffer,logondata,Len(logondata),#PB_UTF8)
If InitNetwork()
  ConnectionID=OpenNetworkConnection("datafeed.aislive.com",7889)
  If ConnectionID
    Debug SendNetworkData(ConnectionID,*buffer,Len(logondata))
    Repeat
      Select NetworkClientEvent(connectionID)
        Case #PB_NetworkEvent_Disconnect
          Debug "disconnected":End
        Case #PB_NetworkEvent_Data
          Debug ReceiveNetworkData(ConnectionID,*buffer,1024)
          Debug "received data:"+PeekS(*buffer)
        Case #PB_NetworkEvent_None
          Debug "no data"
      EndSelect
      Delay(100)
    Until Quit=1

  EndIf
EndIf

Re: Getting data from a server, login fails

Posted: Tue Aug 05, 2014 6:15 am
by Bisonte
PeekS() only "peek" it to the first 0 too... so you have to show it the length...

Code: Select all

PeekS(*Memory, Length, Format)
To send strings with 0 in it, have have to send the length also or you have to use the returnvalue of ReceiveNetworkData(), but the first way
is more safe.

Or you use another char, that is forbidden for usernames or passwords, like :;", etc.

Re: Getting data from a server, login fails

Posted: Tue Aug 05, 2014 6:27 am
by infratec
Hi,

I thought it was clear now, that you can not use string procedures (in the way you did) if you have a 0 included.

Your

Code: Select all

*buffer=AllocateMemory(Len(logondata))
does not what you want.
Len(longdata) counts only up to the first 0.

Bernd

Re: Getting data from a server, login fails

Posted: Tue Aug 05, 2014 8:09 am
by infratec
Try this:

Code: Select all

EnableExplicit

Define.i Ptr, ConnectionID, RcvCount, Quit
Define UserID$, Password$
Define *Buffer


UserID$ = "user"
Password$ = "pwd"

*Buffer = AllocateMemory(1024)
If *Buffer
  PokeA(*Buffer + Ptr, 1)
  Ptr + 1
  Ptr + PokeS(*Buffer + Ptr, UserID$, 31, #PB_Ascii)
  Ptr + 1 ; for the trailing 0
  Ptr + PokeS(*Buffer + Ptr, Password$, 63, #PB_Ascii)
  Ptr + 1 ; for the trailing 0
 
  ShowMemoryViewer(*Buffer, Ptr)
 
  If InitNetwork()
    ConnectionID = OpenNetworkConnection("datafeed.aislive.com", 7889)
    If ConnectionID
      SendNetworkData(ConnectionID, *Buffer, Ptr)
     
      Repeat
        Select NetworkClientEvent(connectionID)
          Case #PB_NetworkEvent_Disconnect
            Debug "disconnected"
            Quit = #True
          Case #PB_NetworkEvent_Data
            RcvCount = ReceiveNetworkData(ConnectionID, *Buffer, 1024)
            ShowMemoryViewer(*Buffer, RcvCount)
          Default
            Delay(10)
        EndSelect
      Until Quit
      CloseNetworkConnection(ConnectionID)
    EndIf
  EndIf
  FreeMemory(*Buffer)
EndIf
Bernd

Re: Getting data from a server, login fails

Posted: Tue Aug 05, 2014 10:51 am
by Poltergeist
Commands don't count up to the first 0. The simply seem to ignore the zero's. If I do a PokeS with that logonstring, the zeros are gone. The effect is the same: it doesn't work, but the 0 is not a terminator, apparently. That ShowMemoryViewer function is certainly easy...

I will try your code when I get the chance. Thanks in advance!


Edit:

Tried it, working great!

Thank you so much!