Cannot connect to the server over TCP (116 error)

Hi Nordic,

I am coding a program (TCP client) sending an array of bytes to a server over TCP. There is no error during the building process. And, there is also no error with modem initialization. But, I always receive the error 116 when I connect to the TCP server.

For the TCP server, I run a TCP server script in Python to receive the data from the TCP client, I tested the TCP server program by using the Netcat tool, and it works. So the problem comes from the code for nRF9160. 

I consulted a lot of credits on the Devzone, but I cannot find the problem. 

Could you please help me have a look at it? (I am using nRF SDK v2.5.1)

Best,

wu

Code_Nordic.zip

  • Hello, 

    Difficult to tell. What server are you trying to connect? Could you please provide a modem trace and logs as well?

    Here is what I get:

    *** Booting nRF Connect SDK v2.5.1 ***
    I: Starting bootloader
    I: Primary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
    I: Secondary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
    I: Boot source: none
    I: Image index: 0, Swap type: none
    I: Bootloader chainload address offset: 0x10000
    *** Booting nRF Connect SDK v2.5.1 ***
    Connecting
    Modem initialization...
    LTE cell changed: Cell ID: 21627653, Tracking area: 33129
    RRC mode: Connected
    Network registration status: Connected - roaming
    
    src PSM parameter update: TAU: 9000, Active time: -1
    does not contain a character string representing a valid network address
    Not able to initialize TCP server connection
    
    IP address 2a05:d014:.............., Port number 7073
    
    *********************************************
    Data to send over LTE-M (as integers, with IMEI embedded as ASCII):
    0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       15      16      17      18      1920      21      22      23      1
    *********************************************
    Failed to transmit TCP packet, 9 Bad file number
    RRC mode: Idle

    Thanks.

    Kind regards,
    Øyvind

  • Hi Øyvind,

     Thank you for your reply.

    That is quite weird, I do not know the reason. I programmed the board to work as a UDP client to send data to a UDP server that is run on an AWS server, and it works well. But, when I tried to program it to work as a TCP client to send data to a TCP server run on AWS, the connection failed. The problem is that I do not where I am wrong.

    - This is the log file: (for safety reason, I delete the IP address in the log fine )

    *** Booting nRF Connect SDK v2.5.1 ***
    Connecting
    Modem initialization...
    LTE cell changed: Cell ID: 103026701, Tracking area: 20100
    RRC mode: Connected
    Network registration status: Connected - home network
    PSM parameter update: TAU: 3240, Active time: -1
    RRC mode: Idle
    
    RRC mode: Connected
    Connect failed : 116
    Closed the socket
    Not able to initialize TCP server connection
    
    IP address 2a05:d014:185:b700:b242....., Port number 7073
    
    *********************************************
    Data to send over LTE-M (as integers, with IMEI embedded as ASCII):
    0       0       0       0       0       0       0       0       0       0       0       0       0       0  015      16      17      18      19      20      21      22      23      1
    *********************************************
    Failed to transmit TCP packet, 9 Bad file number
    RRC mode: Idle

    Everything is fine except error 116 (see line 11 in the Log file "Connect failed : 116"), when I try to connect to the TCP server.

    - For the server: It is just a simple TCP server that is run on AWS to test. I tested the TCP server code by using the Netcat tool, there is no problem with the TCP server code.

    import time                                                  
    import socket
    import struct                                              
    from ctypes import *                                         
    import sys                                                 
    from datetime import datetime                               
                         
    import signal                                               
    import os                                                   
    import json                                                 
    import logging                                              
    
    
    
    
    logging.basicConfig(                                        
        filename="TCP_RX_Log.log",                                
        filemode='a',                                           
        format='%(asctime)s %(levelname)-8s %(message)s',       
        level=logging.INFO,                                     
        datefmt='%Y-%m-%d %H:%M:%S')                            
    
    # Declare IP address + Port
    localIP = "2a05:d014:185:b700..........................."          # IPv6 of AWS where we run UDP code on
    recvPort = 7073                                             
    buffersize = 4096                                                                               
    host = ''                                                   
    
    # Create a TCP/IP socket  
    try:
        r = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)      
        r.bind((host, recvPort))                                    
        print ("Binding successful!")
    except:                                                         
        logging.critical("Failed to bind port")                     
        print("Failed to bind the port")
        sys.exit(1)
    
    
    # In đường dẫn nơi chứa file Log (được tạo trên AWS)
    logFile = os.getcwd() + "/TCP_RX_Log.log"                   
                                                                
    
    #recvMsg, addr = r.recvfrom(buffersize)                     
    def receiveMessage(connection, addr):                       
        # recvMsg, addr = r.recvfrom(buffersize)                
        recvMsg = connection.recv(buffersize)                    
        
        devIP = addr[0]                                                        
        devPort = addr[1]                                       
       
        try: 
            devIMEI = recvMsg[0:15].decode(encoding='UTF-8')    
        except:
            logging.error("Couldn't parse devIMEI")
            devIMEI = 0
        
        return devIP, devPort, devIMEI, recvMsg                
    
    
    if __name__ == "__main__":
        logging.info("TCP server Started!")
        while 1:
            # Listen for incoming connections 
            r.listen()
            print ("waiting for a connection")
            connection, addr = r.accept()                            
            if connection:                                                            
                print (f"Connected to client: {addr}")
                devIP, devPort, devIMEI, recvMsg = receiveMessage(connection, addr)     
                if recvMsg:
                    print ("Receive from client with IP: ", devIP, " and Port Number: ", devPort)
                    print ("IMEI: ", devIMEI)
    
                    for x in recvMsg:
                        print("- ", (x))
    
                    # Close the connection 
                    connection.close()

    Thanks for your help!

    Best,

    wu

Related