Set specific IP Address/Port

Revision:
45:2d57fafd8d73
Parent:
43:91f11760b50f
--- a/main.cpp	Wed Jul 24 15:27:53 2019 +0000
+++ b/main.cpp	Fri Sep 06 07:44:03 2019 +0000
@@ -45,10 +45,13 @@
 NetworkInterface *iface;
 
 // Echo server hostname
-const char *host_name = MBED_CONF_APP_ECHO_SERVER_HOSTNAME;
+//nst char *host_name = MBED_CONF_APP_ECHO_SERVER_HOSTNAME;
+const char *server_ip_address = "222.98.xxx.xxx";
 
 // Echo server port (same for TCP and UDP)
-const int port = MBED_CONF_APP_ECHO_SERVER_PORT;
+//const int port = MBED_CONF_APP_ECHO_SERVER_PORT;
+const int port = 7878;
+
 
 static rtos::Mutex trace_mutex;
 
@@ -169,17 +172,18 @@
     }
 
     SocketAddress sock_addr;
-    retcode = iface->gethostbyname(host_name, &sock_addr);
-    if (retcode != NSAPI_ERROR_OK) {
-        print_function("Couldn't resolve remote host: %s, code: %d\n", host_name, retcode);
-        return -1;
-    }
-
+    //retcode = iface->gethostbyname(host_name, &sock_addr);
+    //if (retcode != NSAPI_ERROR_OK) {
+    //    print_function("Couldn't resolve remote host: %s, code: %d\n", host_name, retcode);
+    //   return -1;
+    //}
+    
+    sock_addr.set_ip_address(server_ip_address);
     sock_addr.set_port(port);
 
     sock.set_timeout(15000);
     int n = 0;
-    const char *echo_string = "TEST";
+    const char *msg_string = "TEST";
     char recv_buf[4];
 #if MBED_CONF_APP_SOCK_TYPE == TCP
     retcode = sock.connect(sock_addr);
@@ -187,20 +191,22 @@
         print_function("TCPSocket.connect() fails, code: %d\n", retcode);
         return -1;
     } else {
-        print_function("TCP: connected with %s server\n", host_name);
+        //int_function("TCP: connected with %s server\n", host_name);
+        //int_function("TCP: connected with %s server\n", sock_addr.get_ip_address());
     }
-    retcode = sock.send((void*) echo_string, sizeof(echo_string));
+    retcode = sock.send((void*) msg_string, sizeof(msg_string));
     if (retcode < 0) {
         print_function("TCPSocket.send() fails, code: %d\n", retcode);
         return -1;
     } else {
-        print_function("TCP: Sent %d Bytes to %s\n", retcode, host_name);
+        //print_function("TCP: Sent %d Bytes to %s\n", retcode, host_name);
+        print_function("TCP: Sent %d Bytes to %s\n", retcode, sock_addr.get_ip_address());
     }
 
     n = sock.recv((void*) recv_buf, sizeof(recv_buf));
 #else
 
-    retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string));
+    retcode = sock.sendto(sock_addr, (void*) msg_string, sizeof(msg_string));
     if (retcode < 0) {
         print_function("UDPSocket.sendto() fails, code: %d\n", retcode);
         return -1;
@@ -214,7 +220,7 @@
     sock.close();
 
     if (n > 0) {
-        print_function("Received from echo server %d Bytes\n", n);
+        print_function("Received from server %d Bytes\n", n);
         return 0;
     }