本文整理汇总了C++中EthernetInterface::get_ip_address方法的典型用法代码示例。如果您正苦于以下问题:C++ EthernetInterface::get_ip_address方法的具体用法?C++ EthernetInterface::get_ip_address怎么用?C++ EthernetInterface::get_ip_address使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EthernetInterface
的用法示例。
在下文中一共展示了EthernetInterface::get_ip_address方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: net_bringup
void net_bringup() {
int err = net.connect();
TEST_ASSERT_EQUAL(0, err);
printf("MBED: Connected to network\n");
printf("MBED: IP Address: %s\n", net.get_ip_address());
ip_literal = net.get_ip_address();
ip_pref = SocketAddress(ip_literal).get_ip_version();
ip_pref_repr = (ip_pref == NSAPI_IPv4) ? "ipv4" :
(ip_pref == NSAPI_IPv6) ? "ipv6" : "unspec";
}
示例2: main
int main() {
GREENTEA_SETUP(20, "udp_echo");
EthernetInterface eth;
eth.connect();
printf("UDP client IP Address is %s\n", eth.get_ip_address());
greentea_send_kv("target_ip", eth.get_ip_address());
char recv_key[] = "host_port";
char ipbuf[60] = {0};
char portbuf[16] = {0};
unsigned int port = 0;
UDPSocket sock;
sock.open(ð);
sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
greentea_send_kv("host_ip", " ");
greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
greentea_send_kv("host_port", " ");
greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
sscanf(portbuf, "%u", &port);
printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
SocketAddress udp_addr(ipbuf, port);
int success = 0;
for (int i=0; i < ECHO_LOOPS; ++i) {
prep_buffer(tx_buffer, sizeof(tx_buffer));
const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
printf("[%02d] sent...%d Bytes \n", i, ret);
SocketAddress temp_addr;
const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
printf("[%02d] recv...%d Bytes \n", i, n);
if ((temp_addr == udp_addr &&
n == sizeof(tx_buffer) &&
memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) {
success += 1;
}
}
bool result = (success > 3*ECHO_LOOPS/4);
sock.close();
eth.disconnect();
GREENTEA_TESTSUITE_RESULT(result);
}
示例3: main
int main() {
GREENTEA_SETUP(20, "tcp_echo_client");
EthernetInterface eth;
eth.connect();
printf("MBED: TCPClient IP address is '%s'\n", eth.get_ip_address());
printf("MBED: TCPClient waiting for server IP and port...\n");
greentea_send_kv("target_ip", eth.get_ip_address());
bool result = false;
char recv_key[] = "host_port";
char ipbuf[60] = {0};
char portbuf[16] = {0};
unsigned int port = 0;
greentea_send_kv("host_ip", " ");
greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
greentea_send_kv("host_port", " ");
greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
sscanf(portbuf, "%u", &port);
printf("MBED: Server IP address received: %s:%d \n", ipbuf, port);
TCPSocket sock(ð);
SocketAddress tcp_addr(ipbuf, port);
if (sock.connect(tcp_addr) == 0) {
printf("HTTP: Connected to %s:%d\r\n", ipbuf, port);
printf("tx_buffer buffer size: %u\r\n", sizeof(tx_buffer));
printf("rx_buffer buffer size: %u\r\n", sizeof(rx_buffer));
prep_buffer(tx_buffer, sizeof(tx_buffer));
sock.send(tx_buffer, sizeof(tx_buffer));
// Server will respond with HTTP GET's success code
const int ret = sock.recv(rx_buffer, sizeof(rx_buffer));
result = !memcmp(tx_buffer, rx_buffer, sizeof(tx_buffer));
TEST_ASSERT_EQUAL(ret, sizeof(rx_buffer));
TEST_ASSERT_EQUAL(true, result);
}
sock.close();
eth.disconnect();
GREENTEA_TESTSUITE_RESULT(result);
}
示例4: main
int main() {
GREENTEA_SETUP(20, "udp_echo_client");
EthernetInterface eth;
eth.connect();
printf("UDP client IP Address is %s\n", eth.get_ip_address());
greentea_send_kv("target_ip", eth.get_ip_address());
bool result = true;
char recv_key[] = "host_port";
char ipbuf[60] = {0};
char portbuf[16] = {0};
unsigned int port = 0;
UDPSocket sock;
sock.open(ð);
greentea_send_kv("host_ip", " ");
greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
greentea_send_kv("host_port", " ");
greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
sscanf(portbuf, "%u", &port);
printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
SocketAddress addr(ipbuf, port);
for (int i=0; i < ECHO_LOOPS; ++i) {
prep_buffer(tx_buffer, sizeof(tx_buffer));
const int ret = sock.sendto(addr, tx_buffer, sizeof(tx_buffer));
printf("[%02d] sent...%d Bytes \n", i, ret);
const int n = sock.recvfrom(&addr, rx_buffer, sizeof(rx_buffer));
printf("[%02d] recv...%d Bytes \n", i, n);
if (memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer))) {
result = false;
break;
}
}
sock.close();
eth.disconnect();
GREENTEA_TESTSUITE_RESULT(result);
}
示例5: main
int main() {
GREENTEA_SETUP(60, "default_auto");
bool result = false;
const time_t TIME1970 = 2208988800L;
int ntp_send_values[12] = {0};
int ntp_recv_values[12] = {0};
EthernetInterface eth;
eth.connect();
printf("UDP client IP Address is %s\n", eth.get_ip_address());
UDPSocket sock;
sock.open(ð);
sock.set_timeout(15000);
SocketAddress nist(ð, HTTP_SERVER_NAME, HTTP_SERVER_PORT);
printf("UDP: NIST server %s address: %s on port %d\r\n", HTTP_SERVER_NAME, nist.get_ip_address(), nist.get_port());
memset(ntp_send_values, 0x00, sizeof(ntp_send_values));
ntp_send_values[0] = '\x1b';
while(1) {
memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values));
int ret_send = sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values));
printf("UDP: Sent %d Bytes to NTP server \n", ret_send);
SocketAddress source;
const int n = sock.recvfrom(&source, (void*)ntp_recv_values, sizeof(ntp_recv_values));
printf("UDP: Recved from NTP server %d Bytes \n", n);
if (n > 0 && strcmp(source.get_ip_address(), nist.get_ip_address()) == 0) {
result = true;
printf("UDP: Values returned by NTP server: \n");
for (size_t i=0; i < sizeof(ntp_recv_values) / sizeof(ntp_recv_values[0]); ++i) {
printf("\t[%02d] 0x%X", i, ntohl(ntp_recv_values[i]));
if (i == 10) {
time_t timestamp = ntohl(ntp_recv_values[i]) - TIME1970;
printf("\tNTP timestamp is %s", ctime(×tamp));
} else {
printf("\n");
}
}
break;
}
printf("Failed to receive data, retrying in 5 seconds...\n");
wait(5);
}
sock.close();
eth.disconnect();
GREENTEA_TESTSUITE_RESULT(result);
}
示例6: test_udp_dtls_handshake
void test_udp_dtls_handshake() {
EthernetInterface eth;
int err = eth.connect();
TEST_ASSERT_EQUAL(0, err);
printf("MBED: UDPClient IP address is '%s'\n", eth.get_ip_address());
printf("MBED: UDPClient waiting for server IP and port...\n");
greentea_send_kv("target_ip", eth.get_ip_address());
bool result = false;
char recv_key[] = "host_port";
char ipbuf[60] = {0};
char portbuf[16] = {0};
unsigned int port = 0;
greentea_send_kv("host_ip", " ");
greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
greentea_send_kv("host_port", " ");
greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
sscanf(portbuf, "%u", &port);
printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
// align each size to 4-bits
for (int i = 0; i < udp_dtls_handshake_count; i++) {
udp_dtls_handshake_pattern[i] = (~0xf & udp_dtls_handshake_pattern[i]) + 0x10;
}
printf("MBED: DTLS pattern [");
for (int i = 0; i < udp_dtls_handshake_count; i++) {
printf("%d", udp_dtls_handshake_pattern[i]);
if (i != udp_dtls_handshake_count-1) {
printf(", ");
}
}
printf("]\r\n");
UDPSocket sock;
SocketAddress udp_addr(ipbuf, port);
sock.set_timeout(MBED_CFG_UDP_DTLS_HANDSHAKE_TIMEOUT);
for (int attempt = 0; attempt < MBED_CFG_UDP_DTLS_HANDSHAKE_RETRIES; attempt++) {
err = sock.open(ð);
TEST_ASSERT_EQUAL(0, err);
for (int i = 0; i < udp_dtls_handshake_count; i++) {
buffer[i] = udp_dtls_handshake_pattern[i] >> 4;
}
err = sock.sendto(udp_addr, buffer, udp_dtls_handshake_count);
printf("UDP: tx -> %d\r\n", err);
TEST_ASSERT_EQUAL(udp_dtls_handshake_count, err);
int step = 0;
while (step < udp_dtls_handshake_count) {
err = sock.recvfrom(NULL, buffer, sizeof(buffer));
printf("UDP: rx <- %d ", err);
// check length
if (err != udp_dtls_handshake_pattern[step]) {
printf("x (expected %d)\r\n", udp_dtls_handshake_pattern[step]);
break;
}
// check quick xor of packet
uint8_t check = 0;
for (int j = 0; j < udp_dtls_handshake_pattern[step]; j++) {
check ^= buffer[j];
}
if (check != 0) {
printf("x (checksum 0x%02x)\r\n", check);
break;
}
// successfully got a packet
printf("\r\n");
step += 1;
}
err = sock.close();
TEST_ASSERT_EQUAL(0, err);
// got through all steps, test passed
if (step == udp_dtls_handshake_count) {
result = true;
break;
}
}
eth.disconnect();
TEST_ASSERT(result);
}