Problem using Sockets

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Twistx77
Posts: 6
Joined: Sat Nov 05, 2016 4:59 pm

Problem using Sockets

Post by Twistx77 » Sat Nov 05, 2016 5:07 pm

Hi,

I'm new to MicroPython and a bit new to Python too. I'm trying to send data using sockets to an ESP8266. I want the ESP8266 to be the server and my pc to be the client but I get an error when trying to receive data and I can't find any helpfull documentation to help me in this case.

This is my code:
[code]
import machine
import network
import socket

p = machine.Pin(2, machine.Pin.OUT)

p.low()

print('connecting to network...')

sta_if = network.WLAN(network.STA_IF)

ap_if = network.WLAN(network.AP_IF)

if (ap_if.active()):
ap_if.active(False)

sta_if.active(True)
sta_if.connect('MY_AP','PASSWORD')

while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
ips = sta_if.ifconfig()


def server_init():
s = socket.socket()
s.bind( ('', 3266) )
s.listen(1)
c, a = s.accept()
time.sleep(5)
while True:
data= s.recv()
print(dato)


server_init()

[/code]


It connects to the ap perfectly and when I try to connect using nc 192.168.1.133 3266, it accepts and then it fails. in the data = s.recv() line.


I try to reproduce the error using repl and when I get to the s.recv() line I get:


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: 0

It just fails.

Can anyone give me any hints?

Thank you.

User avatar
Roberthh
Posts: 2281
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problem using Sockets

Post by Roberthh » Sat Nov 05, 2016 5:53 pm

Are you sure that these two lines are in your code?

Code: Select all

data= s.recv()
print(dato)
The second line should fire an error, unless your codes says:

Code: Select all

print (data)

Twistx77
Posts: 6
Joined: Sat Nov 05, 2016 4:59 pm

Re: Problem using Sockets

Post by Twistx77 » Sat Nov 05, 2016 5:57 pm

Hi Roberthh

Yes, they are.

It even throws an error when I try it via repl directly.

User avatar
Roberthh
Posts: 2281
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problem using Sockets

Post by Roberthh » Sat Nov 05, 2016 6:27 pm

So that's how my test code looks like:

Code: Select all

import socket
import time


def server_init():
    s = socket.socket()
    s.bind( ("", 3266) )
    s.listen(1)
    c, a = s.accept()
    time.sleep(1)

    while True:
        data= c.recv(1)
        print(data)
        if not data: 
            break


server_init()
Edit: Simplified to you base code

Twistx77
Posts: 6
Joined: Sat Nov 05, 2016 4:59 pm

Re: Problem using Sockets

Post by Twistx77 » Sat Nov 05, 2016 8:31 pm

Hi,

Thank you very much. My problem was that I was using the socket (s) with the recv method instead of the client (c).

Now it works but I think documentation is not right then:

https://docs.micropython.org/en/latest/ ... k_tcp.html
http://docs.micropython.org/en/latest/w ... ocket.send

Right?

Or am I missing something?

Thank you very much.

User avatar
Roberthh
Posts: 2281
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problem using Sockets

Post by Roberthh » Sat Nov 05, 2016 8:46 pm

Hi @Twistx77. The example you point at shows a client, which opens a connection to a server, while you code creates a server which accepts connections opened by a client.

Lysenko
Posts: 62
Joined: Wed Aug 17, 2016 1:21 pm

Re: Problem using Sockets

Post by Lysenko » Sat Nov 05, 2016 9:10 pm

Twistx77 wrote:Hi,

Thank you very much. My problem was that I was using the socket (s) with the recv method instead of the client (c).

Now it works but I think documentation is not right then:

https://docs.micropython.org/en/latest/ ... k_tcp.html
http://docs.micropython.org/en/latest/w ... ocket.send

Right?

Or am I missing something?

Thank you very much.
You can only call send and recv against a connected socket and a listening socket is not connected to anything.

If a server listens on 192.168.1.1:5000 and a client connects to that address/port combination the server will create a new socket on 192.168.1.1:<random_port_number> and connect the client to it. This is transparent to the client which sees it as a connection on port 5000 but from a server perspective the port (and therefore socket) the client connected to is not the one that actually gets connected and carries the data.

Twistx77
Posts: 6
Joined: Sat Nov 05, 2016 4:59 pm

Re: Problem using Sockets

Post by Twistx77 » Sat Nov 05, 2016 10:13 pm

Thanks to both for the clarification.

I understand now what my problem is.

I couldn't find an example of my case so I hope this helps someone else :).

Once again thanks again to both.

Post Reply