cancel
Showing results for 
Search instead for 
Did you mean: 

TCP Server and Client

Nada Ali
Associate II
Posted on May 21, 2018 at 08:58

Hi

I'm using STM32F429ZI nucleo board and I'm trying this example to create a TCP Server:

#include 'mbed.h'

#include 'EthernetInterface.h'

#include 'NetworkAPI/buffer.hpp'

#include 'NetworkAPI/tcp/socket.hpp'

using namespace network;

#define PORT 1234

#define MAX_PENDING 1

EthernetInterface interface;

int

main()

{

interface.init();

interface.connect();

printf('IP Address is %s\n\r', interface.getIPAddress());

tcp::Socket server;

tcp::Socket client;

Buffer buffer(256);

if (server.open() < 0) {

printf('Could not open server socket.\n\r');

return -1;

}

if (server.bind(PORT) < 0) {

printf('Could not bind server socket to port '%d'.\n\r', PORT);

return -1;

}

if (server.listen(MAX_PENDING) < 0) {

printf('Could not put server socket into listening mode.\n\r');

return -1;

}

while (server.getStatus() == Socket::Listening) {

if (server.accept(client) < 0) {

printf('Warning: failed to accept connection.\n\r');

continue;

}

printf('Client connected '%s:%d'.\n\r',

client.getRemoteEndpoint().getAddress().toString().c_str(),

client.getRemoteEndpoint().getPort());

while (client.getStatus() == tcp::Socket::Connected) {

buffer.flush();

switch (client.read(buffer)) {

case -1:

printf('Warning: failed to read data from client.\n\r');

break;

case 0:

printf('Connection closed.\n\r');

break;

default:

printf('Received %d bytes.\n\r%s\r', buffer.length(), (char *)buffer.data());

continue;

}

client.shutdown();

client.close();

}

}

}

I have some issue with the EthernetInterface library. The error is in in the socket.h and endpoint.cpp. 

How can I solve this problem?

Thank you

Nada

0 REPLIES 0