Android Question Please help me with Socket and AsyncStreamsText

NGUYEN TUAN ANH

Active Member
Licensed User
I have an application with the source code as follows, but always get the error:
"error: (SocketException) java.net.SocketException: Connection reset"
Please help me, I have no experience with this problem, Thank you very much.!
Test Connect to Ntrip caster Server:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private SocketNTA As Socket
    Private AStreamNTrip As AsyncStreamsText
End Sub

Public Sub Initialize

End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    SocketNTA.Initialize("SocketNTA")
End Sub

Private Sub Button1_Click
    Try
        If SocketNTA.Connected Then SocketNTA.Close
        SocketNTA.Connect("14.238.1.125", 2101, 10000) 'timeout after 10 seconds
        Wait For SocketNTA_Connected (Success As Boolean)
        Log(Success)
        If Success = False Then
            Log("Server did not respond.")
            Return
        End If
    Catch
        Log(LastException)
    End Try
    Dim msg As String
    msg = "GET / HTTP/1.1" & Chr(10) & Chr(13)
    msg = msg & "User-Agent: NTRIP LefebureNTRIPClient/20131124" & Chr(10) & Chr(13)
    msg = msg & "Ntrip-Version: Ntrip/2.0" & Chr(10) & Chr(13)
    msg = msg & "Accept: */*" & Chr(10) & Chr(13)
    msg = msg & "Connection: close" & Chr(10) & Chr(13)
    msg = msg & Chr(10) & Chr(13)
    If AStreamNTrip.IsInitialized Then AStreamNTrip.Close
    AStreamNTrip.Initialize(Me,"AStreamNTRIP", SocketNTA.InputStream, SocketNTA.OutputStream)
    AStreamNTrip.Write(msg)
    Wait For AStreamNTRIP_NewText(Text As String)
    Log(Text)
End Sub

Private Sub AStreamNTRIP_NewText(Text As String)
    Log(Text)
End Sub

Private Sub AStreamNTRIP_Terminated
    Log("Connection NTRIP terminated")
End Sub

PS. I have added this text into my Manifest
SetApplicationAttribute(android:usesCleartextTraffic, "true")
CreateResourceFromFile(Macro, Core.NetworkClearText)
 

emexes

Expert
Licensed User
This is the blind leading the blind here, but I think I am getting somewhere... I just got back this reply:
(next step: dinner, and then I'll get stuck back into it 🍻)

Log output:
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
true

... my debugging message mayhem ...

HTTP/1.1 200 OK
Ntrip-Version: Ntrip/2.0
Ntrip-Flags:
Cache-Control: no-store, no-cache, max-age=0
Pragma: no-cache
Server: NTRIP Spider/7.7.1.9072
Date: Wed, 08 Jan 2025 09:42:53 GMT
Connection: close
Content-Type: gnss/sourcetable
Content-Length: 2322
STR;VRS.105M6;VRS.105M6;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.105M3;VRS.105M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.103M3;VRS.103M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.104M3;VRS.104M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.104_30M3;VRS.104_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.104_45M3;VRS.104_45M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.105_30M3;VRS.105_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.105_45M3;VRS.105_45M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.106M3;VRS.106M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.106_15M3;VRS.106_15M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.106_30M3;VRS.106_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.107M3;VRS.107M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.107_15M3;VRS.107_15M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.107_30M3;VRS.107_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.107_45M3;VRS.107_45M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.108M3;VRS.108M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.108_15M3;VRS.108_15M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.108_30M3;VRS.108_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.111M6;VRS.111M6;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.WGS84;VRS.WGS84;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
ENDSOURCETABLE
Connection NTRIP terminated
 
Upvote 0

NGUYEN TUAN ANH

Active Member
Licensed User
Đây là người mù dẫn đường cho người mù, nhưng tôi nghĩ mình đã đạt được điều gì đó... Tôi vừa nhận được phản hồi này:
(bước tiếp theo: ăn tối, và sau đó tôi sẽ lại tiếp tục làm việc 🍻)

Log output:
CẢNH BÁO: gói com.sun.javafx.embed.swing.oldimpl không có trong javafx.swing
Đang chờ trình gỡ lỗi kết nối...
Chương trình đã bắt đầu.
Gọi B4XPages.GetManager.LogEvents = True để bật chức năng ghi nhật ký sự kiện B4XPages.
ĐÚNG VẬY

... tin nhắn gỡ lỗi của tôi thật hỗn loạn ...

HTTP/1.1 200 Đồng ý
Phiên bản Ntrip: Ntrip/2.0
Cờ Ntrip:
Cache-Control: không lưu trữ, không lưu trữ đệm, max-age=0
Pragma: không có bộ nhớ đệm
Máy chủ: NTRIP Spider/7.7.1.9072
Ngày: Thứ tư, 08 tháng 01 năm 2025 09:42:53 GMT
Kết nối: đóng
Loại nội dung: gnss/sourcetable
Nội dung-Độ dài: 2322
STR;VRS.105M6;VRS.105M6;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.105M3;VRS.105M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.103M3;VRS.103M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.104M3;VRS.104M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.104_30M3;VRS.104_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.104_45M3;VRS.104_45M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.105_30M3;VRS.105_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.105_45M3;VRS.105_45M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.106M3;VRS.106M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.106_15M3;VRS.106_15M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.106_30M3;VRS.106_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.107M3;VRS.107M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.107_15M3;VRS.107_15M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.107_30M3;VRS.107_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.107_45M3;VRS.107_45M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.108M3;VRS.108M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.108_15M3;VRS.108_15M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.108_30M3;VRS.108_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.111M6;VRS.111M6;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
STR;VRS.WGS84;VRS.WGS84;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20,67;105,53;1;1;Leica GNSS Spider;không có;B;Y;9600;
BẢNG NGUỒN CUNG CẤP CUỐI CÙNG
Kết nối NTRIP đã kết thúc
Have a nice dinner 🍻
Very good, thank you somuch
Could you please send me your code
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
I thought the only change I made was to ask Grok what an NTRIP header would look like, and then use that, but it didn't work straight away.

It was only after I'd added a heap of debugging messages to check that each step looked ok, that I started getting responses from the NTRIP server.

Maybe it needs a delay in there somewhere, and one of the debugging messages fluked doing that.

B4X:
'Dim msg As String
'msg = "GET / HTTP/1.1" & Chr(10) & Chr(13)
'msg = msg & "User-Agent: NTRIP LefebureNTRIPClient/20131124" & Chr(10) & Chr(13)
'msg = msg & "Ntrip-Version: Ntrip/2.0" & Chr(10) & Chr(13)
'msg = msg & "Accept: */*" & Chr(10) & Chr(13)
'msg = msg & "Connection: close" & Chr(10) & Chr(13)
'msg = msg & Chr(10) & Chr(13)

'https://x.com/i/grok/share/DzMMAY256zwd4UaQIEm54WSYZ

'GET /mountPt HTTP/1.1<cr><lf>
'Host: 14.238.1.125:2101<cr><lf>
'Ntrip-Version: Ntrip/2.0<cr><lf>
'User-Agent: NTRIP theSoftware/theRevision<cr><lf>

Dim EOL As String = Chr(13) & Chr(10)

Dim msg As String = ""
msg = msg & "GET / HTTP/1.1" & EOL
msg = msg & "Host: 14.238.1.125:2101" & EOL
msg = msg & "Ntrip-Version: Ntrip/2.0" & EOL
msg = msg & "User-Agent: NTRIP theSoftware/theRevision" & EOL
msg = msg & EOL
 
Upvote 0

emexes

Expert
Licensed User
Also, only the first button click works. All subsequent button clicks fail.

The if I close the program and restart it, the first button click works again, and the subsequent button clicks fail again.
 
Upvote 0

emexes

Expert
Licensed User
Righto, removed all debugging messages, one by one so's to find out which one fixed the issue, and... naturally, it now works when it didn't work before. WTF?

Anyway, this is the one Sub that I changed and now works:
(the only change was the http request, and yes, we should change the User-Agent to something more realistic than Grok's example)

B4X:
Private Sub Button1_Click
    Try
        If SocketNTA.Connected Then SocketNTA.Close
        SocketNTA.Connect("14.238.1.125", 2101, 10000) 'timeout after 10 seconds
        Wait For SocketNTA_Connected (Success As Boolean)
        Log(Success)
        If Success = False Then
            Log("Server did not respond.")
            Return
        End If
    Catch
        Log(LastException)
    End Try

    'Dim msg As String
    'msg = "GET / HTTP/1.1" & Chr(10) & Chr(13)
    'msg = msg & "User-Agent: NTRIP LefebureNTRIPClient/20131124" & Chr(10) & Chr(13)
    'msg = msg & "Ntrip-Version: Ntrip/2.0" & Chr(10) & Chr(13)
    'msg = msg & "Accept: */*" & Chr(10) & Chr(13)
    'msg = msg & "Connection: close" & Chr(10) & Chr(13)
    'msg = msg & Chr(10) & Chr(13)

    'https://x.com/i/grok/share/DzMMAY256zwd4UaQIEm54WSYZ
    
    'GET /mountPt HTTP/1.1<cr><lf>
    'Host: 14.238.1.125:2101<cr><lf>
    'Ntrip-Version: Ntrip/2.0<cr><lf>
    'User-Agent: NTRIP theSoftware/theRevision<cr><lf>

    Dim EOL As String = Chr(13) & Chr(10)
    
    Dim msg As String = ""
    msg = msg & "GET / HTTP/1.1" & EOL
    msg = msg & "Host: 14.238.1.125:2101" & EOL
    msg = msg & "Ntrip-Version: Ntrip/2.0" & EOL
    msg = msg & "User-Agent: NTRIP theSoftware/theRevision" & EOL
    msg = msg & EOL
    
    If AStreamNTrip.IsInitialized Then AStreamNTrip.Close
    AStreamNTrip.Initialize(Me,"AStreamNTRIP", SocketNTA.InputStream, SocketNTA.OutputStream)
    AStreamNTrip.Write(msg)
    Wait For AStreamNTRIP_NewText(Text As String)
    Log(Text)
End Sub
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
WGET is using this http request, and it works great:
HTML:
---request begin---
GET / HTTP/1.1
User-Agent: Wget/1.20.3 (mingw32)
Accept: */*
Accept-Encoding: identity
Host: 14.238.1.125:2101
Connection: Keep-Alive

---request end---
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Command Line WGET:
E:\> wget "http://14.238.1.125:2101/" --debug
DEBUG output created by Wget 1.20.3 on mingw32.

Reading HSTS entries from E:\UTIL/.wget-hsts
URI encoding = 'CP1252'
iconv UTF-8 -> CP1252
iconv outlen=50 inlen=25
converted 'http://14.238.1.125:2101/' (CP1252) -> 'http://14.238.1.125:2101/' (UTF-8)
Converted file name 'index.html' (UTF-8) -> 'index.html' (CP1252)
--2025-01-08 21:15:16--  http://14.238.1.125:2101/
seconds 0.00, Connecting to 14.238.1.125:2101... seconds 0.00, connected.
Created socket 3.
Releasing 0x0000000000b88a10 (new refcount 0).
Deleting unused 0x0000000000b88a10.

---request begin---
GET / HTTP/1.1
User-Agent: Wget/1.20.3 (mingw32)
Accept: */*
Accept-Encoding: identity
Host: 14.238.1.125:2101
Connection: Keep-Alive

---request end---
HTTP request sent, awaiting response...
---response begin---
---response end---
200 No headers, assuming HTTP/0.9
Registered socket 3 for persistent reuse.
Length: unspecified
Saving to: 'index.html.4'

index.html.4                                  [  <=>                                                                               ]   2.41K  4.32KB/s    in 0.6s

2025-01-08 21:15:19 (4.32 KB/s) - 'index.html.4' saved [2465]


E:\> type index.html.4
SOURCETABLE 200 OK
Server: GNSS Spider 7.7.1.9072/1.0
Date: Wed, 08 Jan 2025 10:15:10 GMT
Content-Type: text/plain
Content-Length: 2322

STR;VRS.105M6;VRS.105M6;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.105M3;VRS.105M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.103M3;VRS.103M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.104M3;VRS.104M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.104_30M3;VRS.104_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.104_45M3;VRS.104_45M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.105_30M3;VRS.105_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.105_45M3;VRS.105_45M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.106M3;VRS.106M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.106_15M3;VRS.106_15M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.106_30M3;VRS.106_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.107M3;VRS.107M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.107_15M3;VRS.107_15M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.107_30M3;VRS.107_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.107_45M3;VRS.107_45M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.108M3;VRS.108M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.108_15M3;VRS.108_15M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.108_30M3;VRS.108_30M3;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.111M6;VRS.111M6;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
STR;VRS.WGS84;VRS.WGS84;RTCM 3;;2;GPS+GLO+GAL+BDS+QZSS;DoSM;;20.67;105.53;1;1;Leica GNSS Spider;none;B;Y;9600;
ENDSOURCETABLE

E:\>
 
Upvote 0
Top