http accsess
Dependencies: CyaSSL EthernetInterface mbed-rtos mbed
Fork of SimpleTCPClient by
main.cpp@6:7585e2996cd3, 2015-07-22 (annotated)
- Committer:
- thursday1024
- Date:
- Wed Jul 22 08:05:36 2015 +0000
- Revision:
- 6:7585e2996cd3
- Parent:
- 5:962734db89e5
http_access
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thursday1024 | 6:7585e2996cd3 | 1 | // http_access main.cpp |
wolfSSL | 0:0c584b87ea42 | 2 | #include "mbed.h" |
wolfSSL | 0:0c584b87ea42 | 3 | #include "EthernetInterface.h" |
wolfSSL | 0:0c584b87ea42 | 4 | |
wolfSSL | 2:29a1370416cb | 5 | #define PORT 80 /*** SSL 443 ***/ |
wolfSSL | 0:0c584b87ea42 | 6 | |
wolfSSL | 2:29a1370416cb | 7 | #define err_sys(m) puts(m) |
wolfSSL | 0:0c584b87ea42 | 8 | |
wolfSSL | 0:0c584b87ea42 | 9 | TCPSocketConnection socket; |
wolfSSL | 0:0c584b87ea42 | 10 | |
wolfSSL | 0:0c584b87ea42 | 11 | |
wolfSSL | 1:ac91b4f8d818 | 12 | EthernetInterface eth; |
wolfSSL | 1:ac91b4f8d818 | 13 | |
wolfSSL | 5:962734db89e5 | 14 | void net_main(void const *av) |
wolfSSL | 0:0c584b87ea42 | 15 | { |
wolfSSL | 1:ac91b4f8d818 | 16 | char server_ip[20] ; |
wolfSSL | 2:29a1370416cb | 17 | |
wolfSSL | 2:29a1370416cb | 18 | eth.init(); //Use DHCP |
wolfSSL | 2:29a1370416cb | 19 | printf("===== Simple TCP Client ========\n") ; |
wolfSSL | 2:29a1370416cb | 20 | |
wolfSSL | 2:29a1370416cb | 21 | while(1) { |
wolfSSL | 2:29a1370416cb | 22 | if(eth.connect()== 0)break ; |
wolfSSL | 2:29a1370416cb | 23 | wait(0.1); |
wolfSSL | 2:29a1370416cb | 24 | } |
wolfSSL | 2:29a1370416cb | 25 | printf("Client IP: %s\n", eth.getIPAddress()); |
wolfSSL | 2:29a1370416cb | 26 | |
wolfSSL | 0:0c584b87ea42 | 27 | |
wolfSSL | 0:0c584b87ea42 | 28 | socket.set_blocking(false, 300) ; |
wolfSSL | 1:ac91b4f8d818 | 29 | printf("Server IP: ") ; |
wolfSSL | 2:29a1370416cb | 30 | for(int i=0; i<sizeof(server_ip); i++) { |
wolfSSL | 2:29a1370416cb | 31 | if((server_ip[i] = getchar()) == '\r') { |
wolfSSL | 2:29a1370416cb | 32 | server_ip[i] = '\0' ; |
wolfSSL | 2:29a1370416cb | 33 | putchar('\n') ; |
wolfSSL | 2:29a1370416cb | 34 | break ; |
wolfSSL | 2:29a1370416cb | 35 | } else putchar(server_ip[i]) ; |
wolfSSL | 2:29a1370416cb | 36 | } |
wolfSSL | 2:29a1370416cb | 37 | |
wolfSSL | 1:ac91b4f8d818 | 38 | while (socket.connect(server_ip, PORT) < 0) { |
wolfSSL | 1:ac91b4f8d818 | 39 | printf("Unable to connect to (%s) on port (%d)\n", server_ip, PORT); |
wolfSSL | 0:0c584b87ea42 | 40 | wait(1); |
wolfSSL | 0:0c584b87ea42 | 41 | } |
wolfSSL | 0:0c584b87ea42 | 42 | printf("TCP Connected\n") ; |
wolfSSL | 0:0c584b87ea42 | 43 | |
wolfSSL | 5:962734db89e5 | 44 | char msg[] = "GET /congrats.html HTTP/1.0\r\nConnection: Close\r\n\r\n" ; |
wolfSSL | 0:0c584b87ea42 | 45 | // const char msg[] = "Hello World\r\n" ; |
wolfSSL | 0:0c584b87ea42 | 46 | |
wolfSSL | 2:29a1370416cb | 47 | if ( |
wolfSSL | 1:ac91b4f8d818 | 48 | socket.send(msg, sizeof(msg)-1) != (sizeof(msg)-1)) |
wolfSSL | 0:0c584b87ea42 | 49 | err_sys("CyaSSL_write failed"); |
wolfSSL | 0:0c584b87ea42 | 50 | |
wolfSSL | 0:0c584b87ea42 | 51 | char buf[1024]; |
wolfSSL | 0:0c584b87ea42 | 52 | int n ; |
wolfSSL | 1:ac91b4f8d818 | 53 | puts("Server Response:\n") ; |
wolfSSL | 1:ac91b4f8d818 | 54 | do { |
thursday1024 | 6:7585e2996cd3 | 55 | n = socket.receive(buf, sizeof(buf)-1); |
wolfSSL | 1:ac91b4f8d818 | 56 | if (n >= 0) { |
wolfSSL | 1:ac91b4f8d818 | 57 | buf[n] = 0; |
wolfSSL | 1:ac91b4f8d818 | 58 | printf("%s", buf); |
wolfSSL | 5:962734db89e5 | 59 | } else break ; |
wolfSSL | 1:ac91b4f8d818 | 60 | } while(n > 0) ; |
wolfSSL | 1:ac91b4f8d818 | 61 | puts("=== === === ===") ; |
wolfSSL | 1:ac91b4f8d818 | 62 | socket.close(); |
wolfSSL | 1:ac91b4f8d818 | 63 | eth.disconnect(); |
wolfSSL | 1:ac91b4f8d818 | 64 | } |
wolfSSL | 5:962734db89e5 | 65 | |
thursday1024 | 6:7585e2996cd3 | 66 | main(){ |
wolfSSL | 5:962734db89e5 | 67 | |
wolfSSL | 5:962734db89e5 | 68 | #define STACK_SIZE 20000 |
wolfSSL | 5:962734db89e5 | 69 | Thread t(net_main, NULL, osPriorityNormal, STACK_SIZE); |
wolfSSL | 5:962734db89e5 | 70 | |
wolfSSL | 5:962734db89e5 | 71 | while (true) { |
wolfSSL | 5:962734db89e5 | 72 | Thread::wait(1000); |
wolfSSL | 5:962734db89e5 | 73 | } |
wolfSSL | 5:962734db89e5 | 74 | } |