I wish connect mbed to my laptop. In the Laptop are running a python program that open a local port . But I can't make a conection sucessfully.
1 import socket
2 s = socket.socket()
3 s.bind(("localhost", 9999))
4 s.listen(1)
5 sc, addr = s.accept()
6 print "Recibo conexion de " + str(addr[0]) + ":" + str(addr[1])
7
8 while True:
9 recibido = sc.recv(1024)
10 if recibido == "by":
11 break
12 print "Recibido:", recibido
13
14 sc.send(recibido)
15
16 print "adios"
17 sc.close()
18 s.close()
<<#include "mbed.h"
#include "EthernetNetIf.h"
#include "TCPSocket.h"
EthernetNetIf eth(
IpAddr(192,168,1,2), //IP Address
IpAddr(255,255,255,0), //Network Mask
IpAddr(192,168,1,1), //Gateway
IpAddr(192,168,1,1) //DNS
);
TCPSocket tcp;
Serial pc(USBTX,USBRX);
void onTCPSocketEvent(TCPSocketEvent e)
{
switch(e)
{
case TCPSOCKET_CONNECTED:
pc.printf("Connected\n");
break;
case TCPSOCKET_ACCEPT:
pc.printf("Accepted\n");
break;
case TCPSOCKET_READABLE:
pc.printf("Readable\n");
break;
case TCPSOCKET_WRITEABLE:
pc.printf("Writeable\n");
pc.printf("Escribiendo \n\r");
break;
case TCPSOCKET_CONTIMEOUT:
pc.printf("Timeout\n");
break;
case TCPSOCKET_CONRST:
pc.printf("Reset\n");
break;
case TCPSOCKET_CONABRT:
pc.printf("Aborted %d\n",TCPSOCKET_CONABRT);
break;
case TCPSOCKET_ERROR:
pc.printf("Error\n");
break;
case TCPSOCKET_DISCONNECTED:
pc.printf("Disconnected\n");
tcp.close();
break;
}
}
int main()
{
pc.printf("Welcome to wireFUSE\n\r");
pc.printf("Setting up...\n\r");
EthernetErr ethErr = eth.setup();
if(ethErr)
{
pc.printf("Error %d in setup.\n\r", ethErr);
return -1;
}
pc.printf("Setup OK\n\r");
Host server(IpAddr(192,168,1,35),5000);
//laptop ip and port
// display IP address and port# of server
IpAddr serverIp = server.getIp();
int port = server.getPort();
pc.printf("Connecting... %d.%d.%d.%d:%d\r\n", serverIp[0],serverIp[1],serverIp[2],serverIp[3],port);
TCPSocketErr bindErr = tcp.connect(server);
pc.printf("Connected\n");
tcp.setOnEvent(&onTCPSocketEvent);
//Poll the networking stack
while (true)
{
Net::poll();
}
}
I wish connect mbed to my laptop. In the Laptop are running a python program that open a local port . But I can't make a conection sucessfully.
laptop program server written in python
mbed program
Any ideas how to solve the problem?