B4J Question B4J telnet client / socket client

victormedranop

Well-Known Member
Licensed User
Longtime User
Hi, I just finished an socket apps with MySQL backend. to received data from some devices like
GPS and Android phones. But I'm having some troubles when I try to create a client with B4J.

if anyone have a sample client socket TCP.

thanks,

Victor
 

victormedranop

Well-Known Member
Licensed User
Longtime User
Hi Erel, try the example. but get estrange chars in front of the data.

Sending DATA �NB3,imei=

I m able to connect and send data but the server refuse because that char

any work around?

victor
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I guess you have to specify the char format in the parameter so look that up.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upload a small project which shoes the issue.
I fear that you are using a Textfile which is not UTF8 encoded or maybe it have an BOM.

Without seeing your code it is hard to help
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
it's true donManfred. simple code here. Still Learning.

B4X:
Sub Class_Globals
 Private astream_client As AsyncStreams
 Public String2Send As String
End Sub

Private Sub ConnectToServer(Host As String)
 Log("Trying to connect to: " & Host)
 Dim clienttelnet As Socket
 clienttelnet.Initialize("client")
 clienttelnet.Connect(Host, 12345, 10000)
 Wait For Client_Connected (Successful As Boolean)
 If Successful Then
  Log("Connect Sucesfully")
  astream_client.InitializePrefix(clienttelnet.InputStream, False, clienttelnet.OutputStream, "astream_client")
  SendData(String2Send)
 Else
  Log("Failed to connect: " & LastException)
 End If
End Sub


Public Sub SendData (data As String )
 Log("Sending DATA " & String2Send)
 astream.Write(data.GetBytes("UTF8"))
 astream.Close
End Sub

Private Sub AStream_client_Terminated
 LogError("Closing Client Connection")
End Sub

Private Sub AStream_Client_Error
 LogError("Astream Error")
End Sub
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
sorry
B4X:
Dim data As String = "POP,imei=356496043991264&rmc=$GPRMC,140223.00,A,1827.50471,N,06954.75904,W,0.041,,140518,,,A*62,AUTO,G00001,,,,9999,M21,36,4017mV,2.07"
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
This is the full project.
the idea is receive some info, sabe in MySQL and connect to other server and send the same data received.
I made some server examples but I always receive some trash like this.

�

Thanks.

Victor
 
Last edited:
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
I traced the connection with tcpdump and this is the result.

10:46:55.464525 IP (tos 0x0, ttl 64, id 39028, offset 0, flags [DF], proto TCP (6), length 60)
67.205.187.213.47578 > 148.103.7.40.12345: Flags , cksum 0x9b60 (incorrect -> 0x591c), seq 2543739037, win 29200, options [mss 1460,sackOK,TS val 107542761 ecr 0,nop,wscale 7], length 0
0x0000: 4500 003c 9874 4000 4006 0716 43cd bbd5 E..<.t@[email protected]...
0x0010: 9467 0728 b9da 3039 979e 609d 0000 0000 .g.(..09..`.....
0x0020: a002 7210 9b60 0000 0204 05b4 0402 080a ..r..`..........
0x0030: 0668 f8e9 0000 0000 0103 0307 .h..........
10:46:55.528525 IP (tos 0x0, ttl 64, id 39029, offset 0, flags [DF], proto TCP (6), length 52)
67.205.187.213.47578 > 148.103.7.40.12345: Flags [.], cksum 0x9b58 (incorrect -> 0x368c), seq 2543739038, ack 2845112230, win 229, options [nop,nop,TS val 107542777 ecr 74027], length 0
0x0000: 4500 0034 9875 4000 4006 071d 43cd bbd5 E..4.u@[email protected]...
0x0010: 9467 0728 b9da 3039 979e 609e a994 f7a6 .g.(..09..`.....
0x0020: 8010 00e5 9b58 0000 0101 080a 0668 f8f9 .....X.......h..
0x0030: 0001 212b ..!+
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
my mistake. in function

B4X:
Public Sub SendData (data As String )
 Log("Sending DATA " & String2Send)
 astream.Write(data.GetBytes("UTF8"))
 astream.Close
End Sub

I was using the server astream. just changed to

B4X:
Public Sub SendData (data As String )
 Log("Sending DATA " & String2Send)
 astream_client.Write(data.GetBytes("UTF8"))
 astream.Close
End Sub

thanks donManfred. always appreciated you help.


Victor
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
This may just be a typo on this board, but
B4X:
Public Sub SendData (data As String )
 Log("Sending DATA " & String2Send)
 astream_client.Write(data.GetBytes("UTF8"))
 astream.Close
End Sub
it looks like you are closing the wrong stream (astream instead of astream_client). Unless you meant to close the other stream.
 
Upvote 0
Top