Android Question B4A Client socket fails using host name, works using IP address

rgarnett1955

Active Member
Licensed User
Longtime User
Hi,

I am using B4A in client mode with a socket in B4A.


Connect To Server:

Connect to Server:
'

Private Sub btnWiFi_Click

If WiFiConnected = False Then

'        mcuc.Initialize(Me, "shorttclock.local", 50000, 240000, 30000, 2000)
mcuc.Initialize(Me, "tiny_shark",  51010, 240000, 30000, 2000)
'        mcuc.Initialize(Me, "192.168.1.132",  51010, 240000, 30000, 2000)

Dim host As String = GetHostName("192.168.1.132")
Log("Host = "& host)
Else
        mcuc.socketClose

End If
End Sub


Public Sub connectToServer

Private clockSetData As clockSetData_t

Do While mcuSockConnStatus = False
setConnectionStatus(False)


netErrorTimer.Enabled = False
    

If tcpStream.IsInitialized Then
            tcpStream.Close
End If
    

If mcu_socket.IsInitialized Then
            mcu_socket.Close
End If
   

If mcu_socket.IsInitialized = False Then
mcu_socket.Initialize("mcu_socket")
mcu_socket.Connect(ipAddress, servPort, 0)
End If
    
Wait for mcu_socket_Connected(Successful As Boolean)
    
If Successful Then
            #IF LOGGING_ON
            Log("Socket Event - MCU Socket OK")
            #END IF
        
If tcpStream.IsInitialized Then
                tcpStream.Close
End If
       
setConnectionStatus(True)

Sleep(500) 'Allow time for the server to ack connection on the MCU Interface
         
If tcpStream.IsInitialized Then
                tcpStream.Close
End If
       

Try
tcpStream.InitializePrefix(mcu_socket.InputStream, False, mcu_socket.OutputStream, "tcpStream")
setConnectionStatus(True)
clockSetData.clockMode = 0
clockSetData.jogValue = 0
clockSetData.jogRate = 0
CallSub2(Main,"mcucsendCmd",clockSetData)
Return
Catch
setConnectionStatus(False)
                #IF MIN_LOGGING_ON Or LOGGING_ON
                Log("TCP Stream Init Error")
                #END IF
End Try

' Set the command timer on if cmd queue is empty so we send null cmd to MCU
mcuTranInProgress = False

Else
Sleep(500)
            #IF MIN_LOGGING_ON Or LOGGING_ON
            Log("Socket Event - MCU Socket Failed")
            #END IF
End If
Loop
End Sub

The socket fails to connect. If I provide the IP Address it works fine.

When I look at the output of GetHostName("192.168.1.132") it reports the IP address not the host name.

I am using the same code in B4J in the same application and it works fine.

The host server is an Inventek WiFi module and it uses mDNS to provide the local host name to the network router.

Do I have to prefix of suffix the host name to get it to work or is there some other problem.

Regards
Rob
 

rgarnett1955

Active Member
Licensed User
Longtime User
Hi Erel

Q1: tinyshark
Q2: 192.168.132

This is what it does when I send it the IP addr.
Snag_110f0100.png



This is what happens when I send it the host name



Snag_11104c4d.png



I reckon it is the underscore that might be upsetting it as it isn't coming back with the underscore in the host name.


This is the c code for the embedded system that drives the WiFi module:

C:
#if(USE_TINY_SHARK_MDNS == 1)
    strcpy(wiFiPars.DeviceName, "tiny_shark");
    strcpy(wiFiPars.mDNS_Instance, "tiny_shark");
    strcpy(wiFiPars.mDNS_Service, "tiny_shark.local._tcp");
    strcpy(wiFiPars.localPortNo,         TINY_SHARK_LOCAL_PORT);
#else


I will remove the underscore from the host name in the c code and try the system without it.

I will get back to you with the results. I will also have a look at the packets with Wireshark.

Thanks
Rob
 
Upvote 0

rgarnett1955

Active Member
Licensed User
Longtime User
Hi Erel

Q1: tinyshark
Q2: 192.168.132

This is what it does when I send it the IP addr.
View attachment 139777


This is what happens when I send it the host name



View attachment 139778


I reckon it is the underscore that might be upsetting it as it isn't coming back with the underscore in the host name.


This is the c code for the embedded system that drives the WiFi module:

C:
#if(USE_TINY_SHARK_MDNS == 1)
    strcpy(wiFiPars.DeviceName, "tiny_shark");
    strcpy(wiFiPars.mDNS_Instance, "tiny_shark");
    strcpy(wiFiPars.mDNS_Service, "tiny_shark.local._tcp");
    strcpy(wiFiPars.localPortNo,         TINY_SHARK_LOCAL_PORT);
#else


I will remove the underscore from the host name in the c code and try the system without it.

I will get back to you with the results. I will also have a look at the packets with Wireshark.

Thanks
Rob
Hi Erel,

It was that naughty underscore that was the problem.

Doing this to the embedded system:

Embedded System:
    /* Set Common pars */
#if(USE_TINY_SHARK_MDNS == 1)
    strcpy(wiFiPars.DeviceName, "tinyshark");
    strcpy(wiFiPars.mDNS_Instance, "tinyshark");
    strcpy(wiFiPars.mDNS_Service, "tinyshark.local._tcp");
    strcpy(wiFiPars.localPortNo,         TINY_SHARK_LOCAL_PORT);
#else

And doing this:

Socket Connect Initialisation:
'============================================================================================
Private Sub btnWiFi_Click
    If WiFiConnected = False Then
'        mcuc.Initialize(Me, "shorttclock.local", 50000, 240000, 30000, 2000)
'        mcuc.Initialize(Me, "192.168.1.131",  50000, 240000, 30000, 2000)
        mcuc.Initialize(Me, "tinyshark",  51010, 240000, 30000, 2000)
'         mcuc.Initialize(Me, "192.168.1.132",  51010, 240000, 30000, 2000)
        Dim host As String = GetHostName("192.168.1.132")
        Log("Host = "& host)
    Else
        mcuc.socketClose
    End If
End Sub


Fixed the problem.

I had a look at the standard, hyphens are allowed, underscores are not.

Best regards
Rob
 
Upvote 0
Top