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?
object has no attribute 'IPPROTO_UDP'
Re: object has no attribute 'IPPROTO_UDP'
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))
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))
Re: object has no attribute 'IPPROTO_UDP'
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
Server on ESP8266
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)
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)