Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
tests/test_TCP_Socket.h
- Committer:
- sgodinez
- Date:
- 2013-12-20
- Revision:
- 58:408f67fa292f
- Parent:
- 39:6e94520a3217
- Child:
- 71:82205735732b
File content as of revision 58:408f67fa292f:
#ifndef _TEST_TCP_SOCKET_H_
#define _TEST_TCP_SOCKET_H_
using namespace mts;
void testTcpSocket() {
Cellular::Code code;
const int TEST_PORT = 7000;
const std::string TEST_SERVER("204.26.122.5");
printf("TCP SOCKET TESTING\r\n");
printf("Setting APN\r\n");
code = Cellular::getInstance()->setApn("b2b.tmobile.com");
if(code == Cellular::CELL_OK) {
printf("Success!\r\n");
} else {
printf("Error during APN setup [%d]\r\n", (int)code);
}
printf("Setting Socket Closeable\r\n");
code = Cellular::getInstance()->setSocketCloseable();
if(code == Cellular::CELL_OK) {
printf("Success!\r\n");
} else {
printf("Error setting socket closeable [%d]\r\n", (int)code);
}
printf("Establishing PPP Connection\r\n");
if(Cellular::getInstance()->connect()) {
printf("Success!\r\n");
} else {
printf("Error during PPP connection\r\n");
}
printf("Opening a TCP Socket\r\n");
if(Cellular::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) {
printf("Success!\r\n");
} else {
printf("Error during TCP socket open [%s:%d]\r\n", TEST_SERVER.c_str(), TEST_PORT);
}
printf("Receiving Data (timeout = 15 seconds)\r\n");
Timer tmr;
int bytesRead = 0;
const int size = 1024;
char data[size] = { 0 };
tmr.start();
do {
int status = Cellular::getInstance()->read(&data[bytesRead], size - bytesRead, 1000);
if(status != -1) {
bytesRead += status;
} else {
printf("Error reading from socket\r\n");
data[bytesRead] = '\0';
break;
}
printf("Total bytes read %d\r\n", bytesRead);
} while (tmr.read_ms() <= 15000 && bytesRead < size);
printf("READ: [%d] [%s]\r\n", bytesRead, data);
printf("Waiting 10 seconds\r\n");
wait(10);
printf("Writing to socket\r\n");
sprintf(data, "3\r\n");
int bytesWritten = Cellular::getInstance()->write(data, 4, 10000);
if(bytesWritten == 4) {
printf("Successfully wrote 'q'\r\n");
} else {
printf("Failed to write 'q'\r\n");
}
bytesRead = 0;
tmr.start();
do {
int status = Cellular::getInstance()->read(&data[bytesRead], size - bytesRead, 1000);
if(status != -1) {
bytesRead += status;
} else {
printf("Error reading from socket\r\n");
data[bytesRead] = '\0';
break;
}
printf("Total bytes read %d\r\n", bytesRead);
} while (tmr.read_ms() <= 15000 && bytesRead < size);
printf("READ: [%d] [%s]\r\n", bytesRead, data);
printf("Closing socket\r\n");
Cellular::getInstance()->close();
wait(10);
printf("Disconnecting\r\n");
Cellular::getInstance()->disconnect();
}
#endif
uIP Socket Modem Shield (Outdated - see below)