object has no attribute 'IPPROTO_UDP'

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

object has no attribute 'IPPROTO_UDP'

Post by VladVons » Sun Feb 12, 2017 7:55 pm

import socket
# import usocket

socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_UDP)
got error:
object has no attribute 'IPPROTO_UDP'

is ESP8266 supports UDP?
how to use it in server part?

larsks
Posts: 22
Joined: Mon Feb 13, 2017 5:27 pm

Re: object has no attribute 'IPPROTO_UDP'

Post by larsks » Mon Feb 13, 2017 5:34 pm

The socket library doesn't have any of the IPPROTO_* constants. However, simply specifying a socket type of socket.SOCK_DGRAM should do the right thing...and in fact, a simple test seems to work:

On my esp8266:

>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> s.bind(('0.0.0.0', 8888))
>>> s.recvfrom(10)

And on my laptop:

$ socat udp-connect:192.168.4.1:8888 -
hello

And on the micropython board I see:

(b'hello\n', ('192.168.4.2', 37278))

VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: object has no attribute 'IPPROTO_UDP'

Post by VladVons » Mon Feb 13, 2017 9:44 pm

So with UDP i have very good result on a small but frequent packets.
1000 loops on TCP=150 sec vs UDP=6 sec
It 25 times faster than TCP !

But client often hangs randomly between 1-400 loops at line ''DataIn = sock.recvfrom(256)'

Client on Linux PC

Code: Select all

def Client_TestSpeed(aHost, aPort = 80, aCount = 1000):
    import socket

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    #sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    Start = time.time()
    for i in range(1, aCount):
        DataOut = 'Packet %d ' % (i)
        sock.sendto(DataOut, (aHost, aPort))
        print("Sent", DataOut)
        DataIn = sock.recvfrom(256)
        print("DataIn", DataIn)

        Duration = round((time.time() - Start), 2)
        print("Sec", Duration, "Tick", round(Duration / i, 2))
        #time.sleep(0.1)

Server on ESP8266

Code: Select all

def ServerRun(aBind = '0.0.0.0', aPort = 80):
    import socket

    Sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    Sock.bind( (aBind, aPort) )

    while (True):
        print("waiting for data...")

        DataIn, Addr = Sock.recvfrom(256)
        DataOut = b'Server: ' + DataIn
        SendRes = Sock.sendto(DataOut, Addr)
        print('DataIn:', DataIn, 'DataOut:', DataOut, 'SendRes:', SendRes, 'Addr:', Addr)

Post Reply