Page 1 of 1

How to read incoming tcp data packet - string not empty but no data ?

Posted: Monday 13th September 2021 9:59am
by dinge
Dear Gambassers,
wrote a small program to communicate with a android tablet. The program consist of label,textarea and buttons,serversocket. The tablet has a small porgram written in B4A which send a code over wifi to the ethernet connected pc where the gambas program runs.
I have a connection as gambas sees the ip addres and the selected port which is shown in the textarea. Connect to : <ipaddres> port:<port where serversocket listens>
The progam is inspired from the blog Capain Bodgit,( thanks mr SteveDee)
I have the connection and port listening though i cannot read the message send from the tablet( confirmed by the B4A packet send). the message consist of a single coordinate like "01" . In a terminal tcdpdump is see a packet with lenght 6.
In the gambas program when i look at the length of the strmsg from the Socket_read() sub from mysocket it is also 6 but i don't find a way to show it here in message.
The tables send the message string as bytes i suppose. Do i have a data conversion problem or has it a diffrent cause.
Thanks for your help.
Gambas class file

Code: Select all

' Gambas class file

Public Sub Form_Open()
  ServerSocket1.port = 12345
  ServerSocket1.type = Net.Internet
End

Public Sub Form_Close()
  If ServerSocket1.Status Then  
   ServerSocket1.Close
  Endif
End

Public Sub btlisten_Click()
  ServerSocket1.Listen(1)
  If ServerSocket1.status = Net.active Then 
    btlisten.Enabled = False
    lbIpstatus.text = "Connected - Listen to port: " & ServerSocket1.Port
  Endif
End

Public Sub btQuit_Click()
  If ServerSocket1.Status Then 
    ServerSocket1.close
  Endif
  Quit 
End

Public Sub btverbose_Click()
  Dim shellvar As String
  taInfo.text = gb.crlf & ServerSocket1.status & gb.crlf & "------------------------------------------------------------------" & gb.CrLf
  Shell "netstat -at | grep 'LISTEN'" To shellvar
  taInfo.text &= shellvar
End

Public Sub ServerSocket1_Connection(RemoteHostIP As String)
Dim strmsg As String
Dim mysocket As Socket
  If ServerSocket1.Status > Net.Inactive Then 
    mysocket = ServerSocket1.Accept()
    mysocket.Blocking = False
    If mysocket.Status = Net.Connected Then 
       taInfo.text &= gb.crlf & "Connected: " & mysocket.RemoteHost & "  Port:" & mysocket.RemotePort & gb.CrLf
    Else 
      taInfo.text &= gb.crlf & "SocketStatus: " & mysocket.Status & " not Net.connected " & gb.CrLf
    Endif
  Endif
End

Public Sub Socket_read()
  Dim strMsg As String
  Dim convmsg As String
  If Last.status <> Net.Connected Then 
    taInfo.Text &= "no incoming message."
    Return 
  Else 
    Read #Last, strMsg, Lof(Last)
    If strmsg = "" Then 
          strmsg = "empty" 
    Endif 
    Message("Lenght: " & String.Len(strmsg) & gb.CRLF & " strmsg: " & Str$(strmsg))
  Endif
End

Public Sub Socket_closed()
   taInfo.text &= gb.crlf & "Connection with socket closed"
End

Public Sub ServerSocket1_Error()
   Message.Error("Unable to bind socket")
End

Public Sub btCloseServer_Click()
  If ServerSocket1.Status Then 
    ServerSocket1.Close
    btlisten.Enabled = True
    lbIpstatus.text = "Port closed  - Disconnected"
  
  Endif
End
 

Re: How to read incoming tcp data packet - string not empty but no data ?

Posted: Monday 13th September 2021 12:55pm
by BruceSteers
At 1st glance i'd say your call to Socket_Read() should be ServerSocket1_Read()
You seem to have a mix of Socket_EventName() and ServerSocket1_EventName() methods.

The object you added to the Form Designer called ServerSocket1 will use ServerSocket1_EventName() for all events
Socket_Closed() will not trigger either for ServerSocket1

Re: How to read incoming tcp data packet - string not empty but no data ?

Posted: Monday 13th September 2021 4:23pm
by BruceSteers
Possibly using Quote could help see what characters need trimming

Message("Lenght: " & String.Len(strmsg) & gb.LF & " strmsg: " & Quote(strmsg))

Also i'd only use gb.Lf not gb.CrLf in a message
A message box will turn a gb.LF \n into <br> but the CR may have unwanted results.


Str() is for converting a Value to a string and strmsg is probably already a String the tcp message is probably text so Str() is not wanted.

Re:Solved- How to read incoming tcp data packet - string not empty but no data ?

Posted: Tuesday 14th September 2021 7:44am
by dinge
Dear Mr Brucesteers,
thanks for the swift replys. The code provided by Mr SteveDee on his Captain Bodgit website is correct.
Tried to change the Socket_read sub to ServerSocket1_read but did not work.
The Socket_read is used and gives me the messagbox information as test.
Thanks for the 'Quote' tip , did not know this function.
Data from android tablet send : "01"
Data received in Quote(strmsg) = "\x00\x00\x00\x0201"
Data from android tablet send : "12"
Data received in Quote(strmsg) = "\x00\x00\x00\x0212"
I do not know what I see ,how to understand what these "\x00\x00\x00\x02" signs are.
But as suggested what to trim solved it with a simple trim function
Trim(strmsg) = "01"
Trim(strmsg)= "12"
Runs now fine and data is presented in the textarea object.
Thanks Mr BruceSteers

Code: Select all

Public Sub Socket_Read()
  Dim strMsg As String
  If Last.Status <> Net.Connected Then
    TextArea1.Text &= "No incoming message: " & Trim(strMsg) & gb.Lf
    Return
  Else 
    Read #Last, strMsg, Lof(Last)
    TextArea1.Text &= "Incoming message: " & Trim(strMsg) & gb.Lf
  Endif
End

Re: Re:Solved- How to read incoming tcp data packet - string not empty but no data ?

Posted: Tuesday 14th September 2021 11:36am
by BruceSteers
dinge wrote: Tuesday 14th September 2021 7:44am Dear Mr Brucesteers,
thanks for the swift replys. The code provided by Mr SteveDee on his Captain Bodgit website is correct.
Tried to change the Socket_read sub to ServerSocket1_read but did not work.
The Socket_read is used and gives me the messagbox information as test.
Thanks for the 'Quote' tip , did not know this function.
Data from android tablet send : "01"
Data received in Quote(strmsg) = "\x00\x00\x00\x0201"
Data from android tablet send : "12"
Data received in Quote(strmsg) = "\x00\x00\x00\x0212"
I do not know what I see ,how to understand what these "\x00\x00\x00\x02" signs are.
But as suggested what to trim solved it with a simple trim function
Trim(strmsg) = "01"
Trim(strmsg)= "12"
Runs now fine and data is presented in the textarea object.
Thanks Mr BruceSteers

Code: Select all

Public Sub Socket_Read()
  Dim strMsg As String
  If Last.Status <> Net.Connected Then
    TextArea1.Text &= "No incoming message: " & Trim(strMsg) & gb.Lf
    Return
  Else 
    Read #Last, strMsg, Lof(Last)
    TextArea1.Text &= "Incoming message: " & Trim(strMsg) & gb.Lf
  Endif
End
cool glad i got it right in amongst all that getting it wrong i did lol :)
Nice that Trim() works :)
Other option (if trim did not work) would be..

If InStr(strMsg, "\x02") then strMsg = Split(strMsg, "\x02")[1] ' Get the text to the right of the \x02

Quote() can be very handy in figuring out what's happening.

Here's another tip
Using TextArea1.Text &= "more text" will cause textarea to jump around a bit (it basically erases the controls entire text then re-writes it all each time, it's just a bit ugly and noticeable on slow machines)
Using TextArea1.Insert("more text" & gb.Lf) does a nice clean append to the textarea :)

Re: Re:Solved- How to read incoming tcp data packet - string not empty but no data ?

Posted: Friday 17th September 2021 12:08pm
by stevedee
dinge wrote: Tuesday 14th September 2021 7:44am ...Data from android tablet send : "01"
Data received in Quote(strmsg) = "\x00\x00\x00\x0201"
Data from android tablet send : "12"
Data received in Quote(strmsg) = "\x00\x00\x00\x0212"
I do not know what I see ,how to understand what these "\x00\x00\x00\x02" signs are...
Hi dinge, I've just returned from a short holiday, so only just read your post.

Your problem is that the B4A program is not formatting the transmitted data in the format expected by your Gambas code.

You might find it interesting to make a Gambas Client Socket to run alongside your Gambas Server Socket and do a few simple tests. If your Client sends text like this:-
Write #ClientSocket, strMsg, Len(strMsg)
...the Server Socket will receive the text correctly.

But if you send a 4 byte integer like this:-
  intNumber = 11
  Write #ClientSocket, intNumber As Integer
...the server will receive: \x00\x00\x00\x0B

And if you send text like this:-
  strMsg = "11"
  Write #ClientSocket, strMsg As String
...the Server will show: \x0211
which means; Length = 2, Text = 11

So I think you should check the B4A documentation and see if there is a format you can use that suits Gambas. (i.e. It looks like B4A is sending Integer data, but you are expecting Strings.)

Also, you can change the Gambas Server Read to suit other data types. For example:-
Read #theSocket, strMsg, Lof(theSocket)
...could be replaced with something like this:-
Read #theSocket, intData as Integer

I hope this helps.

Re: How to read incoming tcp data packet - string not empty but no data ?

Posted: Friday 15th October 2021 7:18pm
by dinge
Dear Mr SteveDee,
thanks for your information. Indeed B4A sends the strings as raw data = bytes packets. As you suggested I looked into the B4A code and there is a way to send the string data otherwise. Will have to continue to work on that.
Apologize for the late reply , I work on this only in freetime.
Thanks again and greetings
Dusselier Francis