11 years, 3 months ago.

MBED TCP SERVER AND CLIENT

Hi,

Pls find the below the code for the MBED used as server and another system as client. Static IP is given to MBED(Server) and Client(another PC).

-->SERVER PROGRAM is as mentioned below:

  1. include "mbed.h"
  2. include "EthernetInterface.h"
  1. define ECHO_SERVER_PORT 7

int main (void) { EthernetInterface eth; Endpoint port;

printf("\n I am testing Now.."); eth.init("135.135.4.222","255.255.252.0","135.135.4.14"); Use static int Test = -1; unsigned char *MyIP = NULL;

Test = eth.connect(); if(Test == 0) { printf("configured IP Address!!\n"); MyIP = (unsigned char *)eth.getIPAddress(); printf("\n My IP address = %s",MyIP); } printf("\n You are getting this return value = %d",Test);

TCPSocketServer server; server.bind(ECHO_SERVER_PORT); server.listen();

while (true) { printf("\nWait for new connection...\n"); TCPSocketConnection client; server.accept(client); client.set_blocking(false, 1500); Timeout after (1.5)s

printf("Connection from: %s\n", client.get_address()); char buffer[256]; while (true) { int n = client.receive(buffer, sizeof(buffer)); if (n <= 0) break;

client.send_all(buffer, n); if (n <= 0) break; }

client.close(); } }

After executing the Server program, I am getting the statement "Wait for new connection...". What action need to be taken after this.

->The CLIENT PROGRAM is as mentioned below:

  1. include "mbed.h"
  2. include "EthernetInterface.h"

const char* ECHO_SERVER_ADDRESS = "192.168.0.51"; const int ECHO_SERVER_PORT = 7;

int main() { EthernetInterface eth; eth.init(); Use DHCP eth.connect(); printf("IP Address is %s\n", eth.getIPAddress());

TCPSocketConnection socket; while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); wait(1); }

char hello[] = "Hello World\n"; socket.send_all(hello, sizeof(hello) - 1);

char buf[256]; int n = socket.receive(buf, 256); buf[n] = '\0'; printf("%s", buf);

socket.close(); eth.disconnect();

while(true) {} }

And also pls let know where should I write the client program and execute it?. As the client is another PC so what action to be taken.

The OUTPUTS are viewed in Hyper terminal in Windows XP but how these outputs are viewed in Windows 7.

Be the first to answer this question.