Thank you xiangjun for your reply
Here is my code:
#include "mbed.h"
#include "gnss.h"
#include<string>
#include "EthernetInterface.h"
#include "rtos.h"
#include "TCPServer.h"
#include "TCPSocket.h"
// LEDs
DigitalOut led_1(P2_4);
DigitalOut led_2(P2_3);
DigitalOut led_3(P1_21);
DigitalOut led_4(P1_24);
// Static IP network variables
static const char* mbedIP = "10.5.248.45"; //IP
static const char* mbedMask = "255.255.248.0"; // Mask
static const char* mbedGateway = "10.5.248.1"; //Gateway
static const char* recvIP = "10.5.248.121";
Serial pc(USBTX, USBRX);
EthernetInterface eth;
int main()
{
pc.baud(115200);
pc.printf("TCP server example\r\n");
EthernetInterface eth;
eth.set_network(mbedIP , mbedMask, mbedGateway );// my device address
//eth.connect();
if (0 != eth.connect())
{
pc.printf("Ethernet not connected\n\r");
led_1=!led_1;
return -1;
}
else
{
pc.printf("Connected Ethernet \n\r");
led_2=!led_2;
}
//pc.printf("The Server IP address is '%s'\r\n", eth.get_ip_address());
TCPServer srv;
TCPSocket client_sock[MAXCLIENTS];
SocketAddress client_addr[MAXCLIENTS];
int connectionStatus[MAXCLIENTS];
char *buffer = new char[256];
// Open the server on ethernet stack
srv.open(ð);
// Bind the HTTP port (TCP 80) to the server
srv.bind(10110);
// Can handle x simultaneous connections
srv.listen(3);
// srv.set_blocking(false);
//client_sock[0].set_blocking(false);
int numClient = 0;
int rcount = 0;
int scount = 0;
// initialize connection Status for client
/*for(int i=0;i<MAXCLIENTS;i++)
connectionStatus[i] = 0;*/
while(1)
{
for(int i=0;i < MAXCLIENTS;i++)
{
//pc.printf("coonection : %i/n/r",connectionStatus[i]);
// if not connected accept connection
if (connectionStatus[i] == 0)
{
pc.printf("accept : %i\n\r",srv.accept(&client_sock[i], &client_addr[i]));
if(srv.accept(&client_sock[i], &client_addr[i]) == 0)
{
connectionStatus[i] = 1;
//client_sock[i].set_blocking(false);
//srv.set_blocking(false);
numClient++;
pc.printf("Client Accepted %s:%d,%d\r\n", client_addr[i].get_ip_address(),client_addr[i].get_port(),numClient);
led_3=!led_3;
}
}
// if connected
if (connectionStatus[i] == 1)
{
rcount = client_sock[i].recv(buffer, sizeof(buffer));
if(rcount > 0)
{
sprintf(buffer, "Hello Client %s:%d\r\n", client_addr[i].get_ip_address(),client_addr[i].get_port());
scount = client_sock[i].send(buffer, strlen(buffer));
led_4=!led_4;
}
if(rcount == 0) // client disconnected
{
connectionStatus[i] = 0;
pc.printf("Client Disconnected %s:%d\r\n", client_addr[i].get_ip_address(),client_addr[i].get_port());
client_sock[i].close();
numClient--;
}
if(scount < 0) // unable to send data
{
connectionStatus[i] = 0;
pc.printf("Connection lost %s:%d\r\n", client_addr[i].get_ip_address(),client_addr[i].get_port());
client_sock[i].close();
numClient--;
}
}
}
wait(0.5f);
}
}
I don't have any errors and the program compiles well but my issue is when I reached the stage when the server accept the client , there will be no acceptance and the program won't show me the IP address of the TCP client.
Looking forward to hearing from you.
Thank you in advance
Nada