Page 1 of 1

Problem using Sockets

Posted: Sat Nov 05, 2016 5:07 pm
by Twistx77
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.

Re: Problem using Sockets

Posted: Sat Nov 05, 2016 5:53 pm
by Roberthh
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)

Re: Problem using Sockets

Posted: Sat Nov 05, 2016 5:57 pm
by Twistx77
Hi Roberthh

Yes, they are.

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

Re: Problem using Sockets

Posted: Sat Nov 05, 2016 6:27 pm
by Roberthh
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

Re: Problem using Sockets

Posted: Sat Nov 05, 2016 8:31 pm
by Twistx77
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.

Re: Problem using Sockets

Posted: Sat Nov 05, 2016 8:46 pm
by Roberthh
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.

Re: Problem using Sockets

Posted: Sat Nov 05, 2016 9:10 pm
by Lysenko
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.

Re: Problem using Sockets

Posted: Sat Nov 05, 2016 10:13 pm
by Twistx77
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.