Getting data from a server, login fails

Just starting out? Need help? Post your questions and find answers here.
Poltergeist
User
User
Posts: 26
Joined: Fri Jul 14, 2006 10:27 pm

Getting data from a server, login fails

Post 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
infratec
Always Here
Always Here
Posts: 7625
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Getting data from a server, login fails

Post by infratec »

Hi,

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

Bernd
Poltergeist
User
User
Posts: 26
Joined: Fri Jul 14, 2006 10:27 pm

Re: Getting data from a server, login fails

Post 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
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Getting data from a server, login fails

Post 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.
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
infratec
Always Here
Always Here
Posts: 7625
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Getting data from a server, login fails

Post 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
infratec
Always Here
Always Here
Posts: 7625
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Getting data from a server, login fails

Post 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
Poltergeist
User
User
Posts: 26
Joined: Fri Jul 14, 2006 10:27 pm

Re: Getting data from a server, login fails

Post 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!
Post Reply