Socket errors



  • i'm trying to use sockets to connect two pycom via wifi, in the server socket the server binds, listens and waits for accepts in 127.0.0.1 port 21. The problem is the client socket, I connect the client pycom to the server pycom (it works) and then i get the information via wlan.getaddrinfo(), then i create the socket and i use socket.connect((gateway, port)), gateway = given gateway by getaddrinfo(), port = 21. The error that appears is type OSError and the argument that gives is "-1".
    Also, sometimes the ifconfig() that the clients uses returns 0.0.0.0 ip.
    Any idea of what i am doing wrong?
    client code:

    
    import pycom
    
    import network
    from network import WLAN
    import socket
    import utime
    
    
    
    def exists(name):
        try:
            fich = open(name)
            fich.close()
            return True
        except:
            return False
    
    def sendInfo(ssidN):
        try:
            if(exists("registro.txt")):
                print("______START______")
                wlan = WLAN(mode=WLAN.STA)
                nets = wlan.scan()
    
                for net in nets:
                    if(net.ssid == ssidN):
                        wlan.connect(ssid=net[0], auth=(net[2],"witeklab@2018"),bssid=net[1])
                        pycom.rgbled(0x0F0F0F)
                        break
                else:
                    raise Exception("La red", ssidName, "no ha sido encontrada")
                
                while not wlan.isconnected():
                    pass
    
                print("Conexion establecida")
                
                ip, subnet, gateway, dns = wlan.ifconfig()
                print(wlan.ifconfig())
                ##PUERTO DE CONEXION
                port = 80
                pycom.rgbled(0x0f0f00)
                fam, type1, proto, oem, asdf = socket.getaddrinfo(gateway, port)[0]
                sock = socket.socket()
                
                print("conectando socket a -> (",gateway, ",", port,")")
                sock.connect((gateway,port))
    
                utime.sleep(1)
    
                print("socket conectado")
    
                pycom.rgbled(0x000f05)
                op = "registro.txt"
                with open(op,"rb") as f:
                    datos = f.read()
                    sock.write(datos)
                    sock.close()
                    wlan.disconnect()
                    print("Datos enviados")
        except Exception as e:
            if isinstance(e,OSError) and sock:
                sock.close()
            print("Error en initWlan: ", type(e), e)
    
    pycom.heartbeat(False)
    sendInfo("Gateway 001")
    pycom.heartbeat(True)
    
    

Log in to reply
 

Pycom on Twitter