please be so kind and use ``` before and after your code for correct code presentation.
-3004, /*!< not connected to a network */
The Enum you can found here.
Currently I am not at home so I can not try it but I have additional questions, for sure.
Your boards are connected directly without a gateway via a cross cable?
Did you tried establish connection between both boards and pc separately? (Mbed client >> PC server and PC client >> Mbed Server). For verification of both codes/boards are working.
Hi Jan.
Thank you for helping to resolve my issue.
I am trying to have a communication between 2 boards directly connected with Ethernet cable.
I did try to connect K66F to PC (with running Python script from https://os.mbed.com/handbook/Socket ). Since this did not work I assume that the problem on the client side.
When I connect to switch both mbeds it works OK (client and server on K66Fs). Is there an option to make it work connected directly without switch?
I took the code from my example (TCP echo server), which you linked above and commented out the line 6 (//#define ROUTER) because we want to set the IP ourselves and use it without a gateway.
Then I made a simple client
#include "mbed.h"
#include "NetworkInterface.h"
#define BUFFSIZE 50
#define SERVERIP "192.168.1.1" //Here place your static IP of Mbed Server
//#define ROUTER // commented = Static IP uncomented = address assigned by router
#ifndef ROUTER
#define IP "192.168.1.10" //Here place your static IP of Mbed
#define GATEWAY "0.0.0.0"
#define MASK "255.255.255.0"
#endif
#define PORT 20
DigitalOut led(LED1);
NetworkInterface *net = NetworkInterface::get_default_instance();
TCPSocket client;
SocketAddress clientAddress;
SocketAddress serverAddress(SERVERIP, PORT);
int main (void){
printf("TCP Client starting...\n");
int net_stat;
#ifndef ROUTER
net->disconnect();
net_stat = net->set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY);
printf("set IP status: %i\n",net_stat);
#endif
net_stat = net->connect();
printf("connect status: %i\n",net_stat);
SocketAddress ip;
net->get_ip_address(&ip);
const char *p_ip = ip.get_ip_address();
printf("IP address: %s and Port: %d\n", p_ip ? p_ip : "None" , PORT );
SocketAddress mask;
net->get_netmask(&mask);
const char *p_mask = mask.get_ip_address();
printf("Netmask: %s\n", p_mask ? p_mask : "None");
SocketAddress gateway;
net->get_gateway(&gateway);
const char *p_gateway = gateway.get_ip_address();
printf("Gateway: %s\n", p_gateway ? p_gateway : "None");
int scount = 0;
int rcount = 0;
int dummy = 0;
char sbuffer[100];
char rbuffer[100];
while (1) {
if(client.open(net)== NSAPI_ERROR_OK ){
if (client.connect(serverAddress) < 0) {
printf("Failed to connect with server\n\r");
}else{
printf("Connected to server\n");
int n = sprintf(sbuffer,"Test String with a dummy number - %d", dummy++);
scount = client.send(sbuffer, n);
printf("sent [%s] - %d bytes\n", sbuffer, scount);
rcount = client.recv(rbuffer, sizeof rbuffer);
printf("recv [%s] - %d bytes\n", rbuffer, rcount);
client.close();
}
}else{
printf("No Server\n");
net->disconnect();
printf("Program end\n");
break;
}
led = !led;
thread_sleep_for(5000);
}
}
I also checked and tried your code.
Your part sent the string only first time and then was received error -3015, /*!< socket is already connected */ because you not closed the socket at the end of loop. Just add socket.close(); before the delay at the end of loop.
However I never received the error -3004 like you. It seems you have badly set an IP address or something similar.
Server console:
Server bound and listening
Client connected from IP address: 192.168.1.177
Received message from Client :'GET /checkPin.php?'
Sending echo to client
Client disconnected
Server bound and listening
Client console:
socket.open 0
connect 0
recv 27 [Echo - GET /checkPin.php?]
Hi Jan.
Thank you for your help.
With your example I have similar error (3004). If I loop connect - it is getting connected from the 2-nd time however send command failed anyway. Once I place router in between 2 mbeds - it works perfect. May be it is related to signal strength ?
Short cable 1-2 m (CAT5). Can’t explain why it happening. Thank you for your support. I will post if would be able to make it work otherwise will use switch in between.
Regards,
Igor
Ok, the last chance was the cable but probably it is not the reason because modern Eth interfaces have a function called Auto MDI-X and that solve the problem around what cable user use.
According to this the board use the KSZ8081RNA which also has the Auto MDI-X and it is enabled in default.
the 100 MBit Ethernet has usually no automatic detection, you need to use a cross patch cable. Gigabit Ethernet does that all automatically, so adding a Gigabit Ethernetswitch should solve this problem also.
HP Auto MDI/MDI-X
HP Auto MDI/MDI-X configuration eliminates the need to decide whether to use a straight cable or a crossover cable
between the KSZ8081RNA/RND and its link partner. This feature allows the KSZ8081RNA/RND to use either type of
cable to connect with a link partner that is in either MDI or MDI-X mode. The auto-sense function detects transmit and
receive pairs from the link partner and assigns transmit and receive pairs to the KSZ8081RNA/RND accordingly.
HP Auto MDI/MDI-X is enabled by default. It is disabled by writing a ‘1’ t