Inetq.c

From Nutwiki
Revision as of 18:02, 27 October 2016 by Harald (Talk | contribs) (1 revision imported)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Das Beispiel inetq.c zeigt, wie eine URL per HTTP abgerufen werden kann und anschließend über die serielle Schnittstelle mit 115200 Baud ausgegeben wird. Das Ausgabe kann entsprechend mit einem Terminalprogramm betrachtet werden. Falls Ihr Netzwerk DHCP unterstützt wird automatisch MY_IP (IP-Adresse), MY_MASK (Netzwerkmaske) und MY_GATE (Gateway-Adresse)bezogen. Andernfalls muss die SOurce entsprechend angepasst werden. Das Beispiel lädt die URL INTERNETSERVER:INETSERVERPORT INETURL. Falls eine andere Seite geladen werden soll, kann die Adresse entsprechend abgeändert werden. Unter anderem zeigt dieses Beispiel auch die Auflösung von DNS-Adressen und die Verwendung von Standard-Gateways.

<source lang="c">

  1. define DNSSERVERIP "192.168.192.2"
  2. define INETSERVER "www.kornet.net"
  3. define INETSERVERPORT 80
  4. define INETURL "/"
  5. define MY_MAC {0x00,0x06,0x98,0x20,0x00,0x00}
  6. define MY_IP "192.168.192.100"
  7. define MY_MASK "255.255.255.0"
  8. define MY_GATE "192.168.192.3"
  1. include <string.h>
  2. include <stdio.h>
  3. include <io.h>
  1. include <dev/board.h>
  1. include <sys/heap.h>
  2. include <sys/thread.h>
  3. include <sys/timer.h>
  4. include <sys/socket.h>
  5. include <sys/confnet.h>
  1. include <arpa/inet.h>
  2. include <net/route.h>
  3. include <netdb.h>
  1. include <pro/dhcp.h>

static char buff[1024]; static u_char my_mac[] = MY_MAC;

/*

* Main application routine. 
*
*/

int main(void) {

   u_long baud = 115200;
   TCPSOCKET *sock;
   FILE *stream;
   u_long rip;
   u_long ip_addr;
   int bite;
   size_t rc;
   size_t len;
   u_long start_time;
   u_long total_bytes;
   /*
    * Initialize the uart device.
    */
   NutRegisterDevice(&DEV_DEBUG, 0, 0);
   freopen(DEV_DEBUG_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   puts("\nInetQuery 1.0");
   /*
    * Register Realtek controller at address 8300 hex and interrupt 5.
    */
   puts("Configuring Ethernet interface");
   NutRegisterDevice(&DEV_ETHER, 0, 0);
   /*
    * Try DHCP. First use MAC from EEPROM.
    */
   if (NutDhcpIfConfig("eth0", 0, 60000) && NutDhcpIfConfig("eth0", my_mac, 60000)) {
       /*
        * No DHCP server available. Use hard coded values.
        */
       ip_addr = inet_addr(MY_IP);
       NutNetIfConfig("eth0", my_mac, ip_addr, inet_addr(MY_MASK));
       NutIpRouteAdd(0, 0, inet_addr(MY_GATE), &DEV_ETHER);
       NutDnsConfig(0, 0, inet_addr(DNSSERVERIP));
   } else
       ip_addr = confnet.cdn_ip_addr;
   printf("%s ready\n", inet_ntoa(ip_addr));


   /*
    * Resolve hostname using DNS.
    */
   if ((rip = NutDnsGetHostByName(INETSERVER)) != 0) {
       /*
        * Let's try a stdio stream first.
        */
       if ((sock = NutTcpCreateSocket()) != 0) {
           /*
            * Connect a HTTP server in the Internet.
            */
           printf("Connecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT);
           if (NutTcpConnect(sock, rip, INETSERVERPORT) == 0) {
               /*
                * Assign a stream to our connected socket.
                */
               if ((stream = _fdopen((int) sock, "r+b")) != 0) {
                   /*
                    * Send HTTP request to the server.
                    */
                   fprintf(stream, "GET %s HTTP/1.0\r\n", INETURL);
                   fputs("User-Agent: Ethernut [en] (NutOS)\r\n", stream);
                   fputs("\r\n", stream);
                   fflush(stream);
                   /*
                    * Init measure values.
                    */
                   start_time = NutGetTickCount();
                   total_bytes = 0;
                   /*
                    * Read server response and send it to the UART.
                    */
                   while (fgets(buff, sizeof(buff), stream)) {
                       puts(buff);
                       total_bytes += strlen(buff);
                   }
                   printf("Transfered %lu bytes in %lu seconds\n", total_bytes, (NutGetTickCount() - start_time) / 16UL);
                   fclose(stream);
               } else
                   puts("Creating stream device failed");
           } else {
               printf("Bad news, %s refuses the connection.\n", INETSERVER);
           }
           printf("Disconnecting %s:%u\n", inet_ntoa(rip), INETSERVERPORT);
           NutTcpCloseSocket(sock);
       }
       NutSleep(5000);
       /*
        * Now let's use native calls.
        */
       if ((sock = NutTcpCreateSocket()) != 0) {
           /*
            * Connect a HTTP server in the Internet.
            */
           printf("Connecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT);
           if (NutTcpConnect(sock, rip, INETSERVERPORT) == 0) {
               /*
                * Send HTTP request to the server. NutTcpSend() doesn't
                * guarantee to send out all bytes, thus the loop.
                */
               strcpy(buff, "GET " INETURL " HTTP/1.0\r\nUser-Agent: Ethernut [en] (NutOS)\r\n\r\n");
               len = (int) strlen(buff);
               for (rc = 0; rc < len; rc += bite)
                   if ((bite = NutTcpSend(sock, buff + rc, len - rc)) <= 0)
                       break;
               /*
                * Init measure values.
                */
               start_time = NutGetTickCount();
               total_bytes = 0;
               /*
                * Read server response and send it to the UART.
                */
               while ((bite = NutTcpReceive(sock, buff, sizeof(buff) - 1)) > 0) {
                   total_bytes += bite;
                   buff[bite] = 0;
                   puts(buff);
               }
               printf("Transfered %lu bytes in %lu seconds\n", total_bytes, (NutGetTickCount() - start_time) / 16UL);
           } else {
               printf("Bad news, %s refuses the connection.\n", INETSERVER);
           }
           printf("Disconnecting %s:%u\n", inet_ntoa(rip), INETSERVERPORT);
           NutTcpCloseSocket(sock);
       }
   } else
       printf("Great news, %s has been removed!\n", INETSERVER);
   for (;;)
       NutSleep(1000);

} </source> inetq.c Copyright by egnite Software GmbH