hwelltech block chain cpp sdk
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed uniqueCPUID
Fork of bcsdk by
bcsdk 是 block chain sdk 的简写。它是由江苏恒为信息科技有限公司开发的 BlockChain 的 基于 mbed os 的 C++ 版本SDK,它能帮助开发者快速的在支持 mbed os 的芯片上开发 BlockChain 的应用。 bcsdk 的demo文件中包含了BlockChain中的 Key、Account、Asset、Transaction等方面的示例。其中: (1) Key 加密私钥是区块链上的主要授权机制。他们控制资产单位的发行和转让。 资产或帐户将定义发行或转移所需的单个密钥。 在 Key_test.cpp 中,我们实现了: 创建HSM密钥 键入密钥别名的名称(例如'gold','silver','bronze'),密钥别名是用于区分密钥的标签。
(2) Asset 资产是一种可以在区块链上发布的值类型。资产的所有单位均可互换,可以在各方之间直接交易,无需发行人参与。 在 Asset_test.cpp 中,我们实现了: 创建资产 键入资产别名的名称(例如'gold','silver','bronze'),资产别名是用于区分资产的标签。 选择“Key”键以使用现有的HSM密键,此密钥将用于此帐户中资产单位的发放和转移。
(3) Account 加帐户是恒为区块链核心平台中的一个对象,通过创建和跟踪控制程序来跟踪区块链上的资产的所有权。创建帐户时,您提供一个或多个“root”密钥和仲裁。 在 Account_test.cpp 中,我们实现了: 创建帐户 1 输入帐户别名的名称(例如'alice','bob'),帐户别名是用于区分帐户的标签。 2 键入名称以生成新的HSM密钥(例如'alice key','bob key'),此密钥将用于此帐户中资产单位的发放和转移。
(4) Transaction 交易包含一个或多个输入,以及一个或多个输出。恒为区块链核心平台的API允许您使用操作(包括发出,支出和返还)构建交易。 在 Transaction_test.cpp 中,我们实现了: 资产的交易 1 添加“账户支出”操作 2 为资产别名选择“Asset” 3 输入“100”作为金额 4 添加“使用帐户控制”操作 5 为帐户别名选择“Account” 6 为资产别名选择“Asset” 7 输入“100”作为金额 8 “提交”
main.cpp@8:f2a567ee3a46, 2017-09-21 (annotated)
- Committer:
- webmaster
- Date:
- Thu Sep 21 02:09:04 2017 +0000
- Revision:
- 8:f2a567ee3a46
- Parent:
- 7:04744a9ce2b9
- Child:
- 9:d7468574ef8f
test create asset;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsteiert | 0:28bc7ce77e20 | 1 | #include "mbed.h" |
gsteiert | 0:28bc7ce77e20 | 2 | #include "EthernetInterface.h" |
gsteiert | 0:28bc7ce77e20 | 3 | #include "SDFileSystem.h" |
gsteiert | 0:28bc7ce77e20 | 4 | #include <stdio.h> |
webmaster | 8:f2a567ee3a46 | 5 | //#include <string.h> |
webmaster | 8:f2a567ee3a46 | 6 | |
webmaster | 8:f2a567ee3a46 | 7 | #include <iostream> |
webmaster | 8:f2a567ee3a46 | 8 | #include <string> |
webmaster | 8:f2a567ee3a46 | 9 | #include "UnitTest.h" |
webmaster | 8:f2a567ee3a46 | 10 | //#include "BlockChain.h" |
webmaster | 8:f2a567ee3a46 | 11 | #include "MockHsm.h" |
webmaster | 8:f2a567ee3a46 | 12 | |
webmaster | 8:f2a567ee3a46 | 13 | using namespace std; |
gsteiert | 0:28bc7ce77e20 | 14 | |
gsteiert | 0:28bc7ce77e20 | 15 | #define HTTPD_SERVER_PORT 80 |
gsteiert | 0:28bc7ce77e20 | 16 | #define HTTPD_MAX_REQ_LENGTH 1023 |
gsteiert | 0:28bc7ce77e20 | 17 | #define HTTPD_MAX_HDR_LENGTH 255 |
gsteiert | 0:28bc7ce77e20 | 18 | #define HTTPD_MAX_FNAME_LENGTH 127 |
gsteiert | 3:4f71a37a1ed2 | 19 | #define HTTPD_MAX_DNAME_LENGTH 127 |
gsteiert | 3:4f71a37a1ed2 | 20 | |
webmaster | 8:f2a567ee3a46 | 21 | const char* ECHO_SERVER_ADDRESS = "192.168.31.185"; |
webmaster | 8:f2a567ee3a46 | 22 | const int ECHO_SERVER_PORT = 10003; |
webmaster | 8:f2a567ee3a46 | 23 | |
gsteiert | 3:4f71a37a1ed2 | 24 | Serial uart(USBTX, USBRX); |
gsteiert | 0:28bc7ce77e20 | 25 | |
gsteiert | 6:ca17ce165ebc | 26 | //SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD |
gsteiert | 5:27b09a0f3fb6 | 27 | //SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD |
gsteiert | 6:ca17ce165ebc | 28 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F |
gsteiert | 3:4f71a37a1ed2 | 29 | |
gsteiert | 0:28bc7ce77e20 | 30 | EthernetInterface eth; |
gsteiert | 0:28bc7ce77e20 | 31 | TCPSocketServer server; |
gsteiert | 0:28bc7ce77e20 | 32 | TCPSocketConnection client; |
webmaster | 8:f2a567ee3a46 | 33 | TCPSocketConnection my_client; |
gsteiert | 0:28bc7ce77e20 | 34 | |
gsteiert | 0:28bc7ce77e20 | 35 | char buffer[HTTPD_MAX_REQ_LENGTH+1]; |
gsteiert | 0:28bc7ce77e20 | 36 | char httpHeader[HTTPD_MAX_HDR_LENGTH+1]; |
gsteiert | 3:4f71a37a1ed2 | 37 | char fileName[HTTPD_MAX_FNAME_LENGTH+1]; |
gsteiert | 3:4f71a37a1ed2 | 38 | char dirName[HTTPD_MAX_DNAME_LENGTH+1]; |
gsteiert | 0:28bc7ce77e20 | 39 | char *uristr; |
gsteiert | 0:28bc7ce77e20 | 40 | char *eou; |
gsteiert | 0:28bc7ce77e20 | 41 | char *qrystr; |
gsteiert | 0:28bc7ce77e20 | 42 | |
gsteiert | 0:28bc7ce77e20 | 43 | FILE *fp; |
gsteiert | 0:28bc7ce77e20 | 44 | int rdCnt; |
gsteiert | 0:28bc7ce77e20 | 45 | |
webmaster | 8:f2a567ee3a46 | 46 | // function declation |
webmaster | 8:f2a567ee3a46 | 47 | int test_socket_client(void); |
webmaster | 8:f2a567ee3a46 | 48 | int socket_init(); |
webmaster | 8:f2a567ee3a46 | 49 | int socket_fini(); |
webmaster | 8:f2a567ee3a46 | 50 | |
webmaster | 8:f2a567ee3a46 | 51 | int test_MockHsm() |
webmaster | 8:f2a567ee3a46 | 52 | { |
webmaster | 8:f2a567ee3a46 | 53 | MockHsm mh; |
webmaster | 8:f2a567ee3a46 | 54 | mh.setAlias("cpp_key"); |
webmaster | 8:f2a567ee3a46 | 55 | //cout<<mh.getAlias()<<endl; |
webmaster | 8:f2a567ee3a46 | 56 | //cout<<mh.createKey()<<endl; |
webmaster | 8:f2a567ee3a46 | 57 | cout<< mh.listKeys() <<endl; |
webmaster | 8:f2a567ee3a46 | 58 | return 0; |
webmaster | 8:f2a567ee3a46 | 59 | } |
webmaster | 8:f2a567ee3a46 | 60 | |
webmaster | 8:f2a567ee3a46 | 61 | int main(void) |
webmaster | 8:f2a567ee3a46 | 62 | { |
webmaster | 8:f2a567ee3a46 | 63 | printf("main start\n"); |
webmaster | 8:f2a567ee3a46 | 64 | |
webmaster | 8:f2a567ee3a46 | 65 | socket_init(); |
webmaster | 8:f2a567ee3a46 | 66 | |
webmaster | 8:f2a567ee3a46 | 67 | test_MockHsm(); |
webmaster | 8:f2a567ee3a46 | 68 | |
webmaster | 8:f2a567ee3a46 | 69 | test_socket_client(); |
webmaster | 8:f2a567ee3a46 | 70 | |
webmaster | 8:f2a567ee3a46 | 71 | socket_fini(); |
webmaster | 8:f2a567ee3a46 | 72 | |
webmaster | 8:f2a567ee3a46 | 73 | printf("main end\n"); |
webmaster | 8:f2a567ee3a46 | 74 | return 0; |
webmaster | 8:f2a567ee3a46 | 75 | } |
webmaster | 8:f2a567ee3a46 | 76 | |
webmaster | 8:f2a567ee3a46 | 77 | int socket_init() |
webmaster | 8:f2a567ee3a46 | 78 | { |
webmaster | 8:f2a567ee3a46 | 79 | int ret = 0; |
webmaster | 8:f2a567ee3a46 | 80 | // EthernetInterface eth; |
webmaster | 8:f2a567ee3a46 | 81 | printf("Initializing Ethernet\n"); |
webmaster | 8:f2a567ee3a46 | 82 | ret = eth.init(); //Use DHCP |
webmaster | 8:f2a567ee3a46 | 83 | //eth.init("192.168.31.186", "255.255.255.0", "192.168.31.1"); //Use static ip |
webmaster | 8:f2a567ee3a46 | 84 | printf("Connecting\n"); |
webmaster | 8:f2a567ee3a46 | 85 | ret = eth.connect(); |
webmaster | 8:f2a567ee3a46 | 86 | printf("IP Address is %s\n", eth.getIPAddress()); |
webmaster | 8:f2a567ee3a46 | 87 | |
webmaster | 8:f2a567ee3a46 | 88 | return ret; |
webmaster | 8:f2a567ee3a46 | 89 | } |
webmaster | 8:f2a567ee3a46 | 90 | |
webmaster | 8:f2a567ee3a46 | 91 | int socket_fini() |
webmaster | 8:f2a567ee3a46 | 92 | { |
webmaster | 8:f2a567ee3a46 | 93 | return eth.disconnect(); |
webmaster | 8:f2a567ee3a46 | 94 | } |
webmaster | 8:f2a567ee3a46 | 95 | |
webmaster | 8:f2a567ee3a46 | 96 | int test_socket_client(void) |
webmaster | 8:f2a567ee3a46 | 97 | { |
webmaster | 8:f2a567ee3a46 | 98 | TCPSocketConnection socket; |
webmaster | 8:f2a567ee3a46 | 99 | while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { |
webmaster | 8:f2a567ee3a46 | 100 | printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
webmaster | 8:f2a567ee3a46 | 101 | wait(1); |
webmaster | 8:f2a567ee3a46 | 102 | } |
webmaster | 8:f2a567ee3a46 | 103 | printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS); |
webmaster | 8:f2a567ee3a46 | 104 | |
webmaster | 8:f2a567ee3a46 | 105 | // Send message to server |
webmaster | 8:f2a567ee3a46 | 106 | char hello[] = "Hello World"; |
webmaster | 8:f2a567ee3a46 | 107 | printf("Sending message to Server : '%s' \n",hello); |
webmaster | 8:f2a567ee3a46 | 108 | socket.send_all(hello, sizeof(hello) - 1); |
webmaster | 8:f2a567ee3a46 | 109 | |
webmaster | 8:f2a567ee3a46 | 110 | // Receive message from server |
webmaster | 8:f2a567ee3a46 | 111 | char buf[256]; |
webmaster | 8:f2a567ee3a46 | 112 | int n = socket.receive(buf, 256); |
webmaster | 8:f2a567ee3a46 | 113 | buf[n] = '\0'; |
webmaster | 8:f2a567ee3a46 | 114 | printf("Received message from server: '%s'\n", buf); |
webmaster | 8:f2a567ee3a46 | 115 | |
webmaster | 8:f2a567ee3a46 | 116 | // Clean up |
webmaster | 8:f2a567ee3a46 | 117 | socket.close(); |
webmaster | 8:f2a567ee3a46 | 118 | |
webmaster | 8:f2a567ee3a46 | 119 | return 0; |
webmaster | 8:f2a567ee3a46 | 120 | } |
webmaster | 8:f2a567ee3a46 | 121 | |
gsteiert | 0:28bc7ce77e20 | 122 | void get_file(char* uri) |
gsteiert | 0:28bc7ce77e20 | 123 | { |
gsteiert | 3:4f71a37a1ed2 | 124 | uart.printf("get_file %s\n", uri); |
gsteiert | 0:28bc7ce77e20 | 125 | char *lstchr = strrchr(uri, NULL) -1; |
gsteiert | 0:28bc7ce77e20 | 126 | if ('/' == *lstchr) { |
gsteiert | 3:4f71a37a1ed2 | 127 | uart.printf("Open directory /sd%s\n", uri); |
gsteiert | 0:28bc7ce77e20 | 128 | *lstchr = 0; |
gsteiert | 3:4f71a37a1ed2 | 129 | sprintf(fileName, "/sd%s", uri); |
gsteiert | 3:4f71a37a1ed2 | 130 | DIR *d = opendir(fileName); |
gsteiert | 0:28bc7ce77e20 | 131 | if (d != NULL) { |
gsteiert | 0:28bc7ce77e20 | 132 | sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); |
gsteiert | 0:28bc7ce77e20 | 133 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 3:4f71a37a1ed2 | 134 | sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri); |
gsteiert | 0:28bc7ce77e20 | 135 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 0:28bc7ce77e20 | 136 | struct dirent *p; |
gsteiert | 0:28bc7ce77e20 | 137 | while((p = readdir(d)) != NULL) { |
gsteiert | 3:4f71a37a1ed2 | 138 | sprintf(dirName, "%s/%s", fileName, p->d_name); |
gsteiert | 3:4f71a37a1ed2 | 139 | uart.printf("%s\n", dirName); |
gsteiert | 3:4f71a37a1ed2 | 140 | DIR *subDir = opendir(dirName); |
gsteiert | 3:4f71a37a1ed2 | 141 | if (subDir != NULL) { |
gsteiert | 3:4f71a37a1ed2 | 142 | sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name); |
gsteiert | 3:4f71a37a1ed2 | 143 | } else { |
gsteiert | 3:4f71a37a1ed2 | 144 | sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name); |
gsteiert | 3:4f71a37a1ed2 | 145 | } |
gsteiert | 0:28bc7ce77e20 | 146 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 0:28bc7ce77e20 | 147 | } |
gsteiert | 0:28bc7ce77e20 | 148 | } |
gsteiert | 0:28bc7ce77e20 | 149 | closedir(d); |
gsteiert | 3:4f71a37a1ed2 | 150 | uart.printf("Directory closed\n"); |
gsteiert | 0:28bc7ce77e20 | 151 | sprintf(httpHeader,"</ul></body></html>"); |
gsteiert | 0:28bc7ce77e20 | 152 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 0:28bc7ce77e20 | 153 | } else { |
gsteiert | 3:4f71a37a1ed2 | 154 | sprintf(fileName, "/sd%s", uri); |
gsteiert | 3:4f71a37a1ed2 | 155 | fp = fopen(fileName, "r"); |
gsteiert | 0:28bc7ce77e20 | 156 | if (fp == NULL) { |
gsteiert | 7:04744a9ce2b9 | 157 | uart.printf("File not found\n"); |
gsteiert | 0:28bc7ce77e20 | 158 | sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
gsteiert | 0:28bc7ce77e20 | 159 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 0:28bc7ce77e20 | 160 | client.send(uri,strlen(uri)); |
gsteiert | 0:28bc7ce77e20 | 161 | } else { |
gsteiert | 7:04744a9ce2b9 | 162 | uart.printf("Sending: header"); |
gsteiert | 0:28bc7ce77e20 | 163 | sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); |
gsteiert | 0:28bc7ce77e20 | 164 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 7:04744a9ce2b9 | 165 | uart.printf(" file"); |
gsteiert | 0:28bc7ce77e20 | 166 | while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) { |
gsteiert | 0:28bc7ce77e20 | 167 | client.send(buffer, rdCnt); |
gsteiert | 7:04744a9ce2b9 | 168 | uart.printf("."); |
gsteiert | 0:28bc7ce77e20 | 169 | } |
gsteiert | 0:28bc7ce77e20 | 170 | client.send(buffer, rdCnt); |
gsteiert | 0:28bc7ce77e20 | 171 | fclose(fp); |
gsteiert | 7:04744a9ce2b9 | 172 | uart.printf("done\n"); |
gsteiert | 0:28bc7ce77e20 | 173 | } |
gsteiert | 0:28bc7ce77e20 | 174 | } |
gsteiert | 0:28bc7ce77e20 | 175 | } |
gsteiert | 0:28bc7ce77e20 | 176 | |
webmaster | 8:f2a567ee3a46 | 177 | int main_1 (void) |
gsteiert | 0:28bc7ce77e20 | 178 | { |
gsteiert | 3:4f71a37a1ed2 | 179 | // Serial Interface eth; |
gsteiert | 3:4f71a37a1ed2 | 180 | uart.baud(115200); |
gsteiert | 5:27b09a0f3fb6 | 181 | uart.printf("Initializing\n"); |
gsteiert | 3:4f71a37a1ed2 | 182 | |
gsteiert | 3:4f71a37a1ed2 | 183 | // Check File System |
gsteiert | 5:27b09a0f3fb6 | 184 | uart.printf("Checking File System\n"); |
gsteiert | 3:4f71a37a1ed2 | 185 | DIR *d = opendir("/sd/"); |
gsteiert | 3:4f71a37a1ed2 | 186 | if (d != NULL) { |
gsteiert | 3:4f71a37a1ed2 | 187 | uart.printf("SD Card Present\n"); |
gsteiert | 3:4f71a37a1ed2 | 188 | } else { |
gsteiert | 3:4f71a37a1ed2 | 189 | uart.printf("SD Card Root Directory Not Found\n"); |
gsteiert | 3:4f71a37a1ed2 | 190 | } |
gsteiert | 3:4f71a37a1ed2 | 191 | |
gsteiert | 0:28bc7ce77e20 | 192 | // EthernetInterface eth; |
gsteiert | 5:27b09a0f3fb6 | 193 | uart.printf("Initializing Ethernet\n"); |
webmaster | 8:f2a567ee3a46 | 194 | //eth.init(); //Use DHCP |
webmaster | 8:f2a567ee3a46 | 195 | eth.init("192.168.31.186", "255.255.255.0", "192.168.31.1"); //Use |
gsteiert | 5:27b09a0f3fb6 | 196 | uart.printf("Connecting\n"); |
gsteiert | 0:28bc7ce77e20 | 197 | eth.connect(); |
gsteiert | 3:4f71a37a1ed2 | 198 | uart.printf("IP Address is %s\n", eth.getIPAddress()); |
webmaster | 8:f2a567ee3a46 | 199 | |
webmaster | 8:f2a567ee3a46 | 200 | //test socket client |
webmaster | 8:f2a567ee3a46 | 201 | //my_client.connect("192.168.31.185",10003); |
webmaster | 8:f2a567ee3a46 | 202 | //my_client.send_all("hello world",11); |
webmaster | 8:f2a567ee3a46 | 203 | |
webmaster | 8:f2a567ee3a46 | 204 | TCPSocketConnection socket; |
webmaster | 8:f2a567ee3a46 | 205 | while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { |
webmaster | 8:f2a567ee3a46 | 206 | uart.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
webmaster | 8:f2a567ee3a46 | 207 | wait(1); |
webmaster | 8:f2a567ee3a46 | 208 | } |
webmaster | 8:f2a567ee3a46 | 209 | uart.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS); |
webmaster | 8:f2a567ee3a46 | 210 | |
webmaster | 8:f2a567ee3a46 | 211 | // Send message to server |
webmaster | 8:f2a567ee3a46 | 212 | char hello[] = "Hello World"; |
webmaster | 8:f2a567ee3a46 | 213 | uart.printf("Sending message to Server : '%s' \n",hello); |
webmaster | 8:f2a567ee3a46 | 214 | socket.send_all(hello, sizeof(hello) - 1); |
webmaster | 8:f2a567ee3a46 | 215 | |
webmaster | 8:f2a567ee3a46 | 216 | // Receive message from server |
webmaster | 8:f2a567ee3a46 | 217 | char buf[256]; |
webmaster | 8:f2a567ee3a46 | 218 | int n = socket.receive(buf, 256); |
webmaster | 8:f2a567ee3a46 | 219 | buf[n] = '\0'; |
webmaster | 8:f2a567ee3a46 | 220 | uart.printf("Received message from server: '%s'\n", buf); |
webmaster | 8:f2a567ee3a46 | 221 | |
webmaster | 8:f2a567ee3a46 | 222 | // Clean up |
webmaster | 8:f2a567ee3a46 | 223 | socket.close(); |
webmaster | 8:f2a567ee3a46 | 224 | //////////////////////////////////////////////////////////////////////////// |
gsteiert | 0:28bc7ce77e20 | 225 | |
gsteiert | 0:28bc7ce77e20 | 226 | // TCPSocketServer server; |
gsteiert | 0:28bc7ce77e20 | 227 | server.bind(HTTPD_SERVER_PORT); |
gsteiert | 0:28bc7ce77e20 | 228 | server.listen(); |
gsteiert | 5:27b09a0f3fb6 | 229 | uart.printf("Server Listening\n"); |
gsteiert | 0:28bc7ce77e20 | 230 | |
gsteiert | 0:28bc7ce77e20 | 231 | while (true) { |
gsteiert | 3:4f71a37a1ed2 | 232 | uart.printf("\nWait for new connection...\r\n"); |
gsteiert | 0:28bc7ce77e20 | 233 | server.accept(client); |
gsteiert | 0:28bc7ce77e20 | 234 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
webmaster | 8:f2a567ee3a46 | 235 | |
webmaster | 8:f2a567ee3a46 | 236 | my_client.send_all("hello world",11); |
gsteiert | 0:28bc7ce77e20 | 237 | |
gsteiert | 3:4f71a37a1ed2 | 238 | uart.printf("Connection from: %s\r\n", client.get_address()); |
gsteiert | 0:28bc7ce77e20 | 239 | while (true) { |
gsteiert | 0:28bc7ce77e20 | 240 | int n = client.receive(buffer, sizeof(buffer)); |
gsteiert | 0:28bc7ce77e20 | 241 | if (n <= 0) break; |
gsteiert | 3:4f71a37a1ed2 | 242 | uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer); |
gsteiert | 0:28bc7ce77e20 | 243 | if (n >= 1024) { |
gsteiert | 0:28bc7ce77e20 | 244 | sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
gsteiert | 0:28bc7ce77e20 | 245 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 0:28bc7ce77e20 | 246 | client.send(buffer,n); |
gsteiert | 0:28bc7ce77e20 | 247 | break; |
gsteiert | 0:28bc7ce77e20 | 248 | } else { |
gsteiert | 0:28bc7ce77e20 | 249 | buffer[n]=0; |
gsteiert | 0:28bc7ce77e20 | 250 | } |
gsteiert | 0:28bc7ce77e20 | 251 | if (!strncmp(buffer, "GET ", 4)) { |
gsteiert | 0:28bc7ce77e20 | 252 | uristr = buffer + 4; |
gsteiert | 0:28bc7ce77e20 | 253 | eou = strstr(uristr, " "); |
gsteiert | 0:28bc7ce77e20 | 254 | if (eou == NULL) { |
gsteiert | 0:28bc7ce77e20 | 255 | sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
gsteiert | 0:28bc7ce77e20 | 256 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 0:28bc7ce77e20 | 257 | client.send(buffer,n); |
gsteiert | 0:28bc7ce77e20 | 258 | } else { |
gsteiert | 0:28bc7ce77e20 | 259 | *eou = 0; |
gsteiert | 3:4f71a37a1ed2 | 260 | get_file(uristr); |
gsteiert | 0:28bc7ce77e20 | 261 | } |
gsteiert | 0:28bc7ce77e20 | 262 | } |
gsteiert | 0:28bc7ce77e20 | 263 | } |
gsteiert | 0:28bc7ce77e20 | 264 | |
gsteiert | 0:28bc7ce77e20 | 265 | client.close(); |
gsteiert | 0:28bc7ce77e20 | 266 | } |
gsteiert | 0:28bc7ce77e20 | 267 | } |
webmaster | 8:f2a567ee3a46 | 268 | |
webmaster | 8:f2a567ee3a46 | 269 | |
webmaster | 8:f2a567ee3a46 | 270 | int main_2() { |
webmaster | 8:f2a567ee3a46 | 271 | // Serial Interface eth; |
webmaster | 8:f2a567ee3a46 | 272 | uart.baud(115200); |
webmaster | 8:f2a567ee3a46 | 273 | uart.printf("Initializing\n"); |
webmaster | 8:f2a567ee3a46 | 274 | |
webmaster | 8:f2a567ee3a46 | 275 | EthernetInterface eth; |
webmaster | 8:f2a567ee3a46 | 276 | eth.init(); //Use DHCP |
webmaster | 8:f2a567ee3a46 | 277 | eth.connect(); |
webmaster | 8:f2a567ee3a46 | 278 | printf("\nClient IP Address is %s\n", eth.getIPAddress()); |
webmaster | 8:f2a567ee3a46 | 279 | |
webmaster | 8:f2a567ee3a46 | 280 | // Connect to Server |
webmaster | 8:f2a567ee3a46 | 281 | TCPSocketConnection socket; |
webmaster | 8:f2a567ee3a46 | 282 | while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { |
webmaster | 8:f2a567ee3a46 | 283 | printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
webmaster | 8:f2a567ee3a46 | 284 | wait(1); |
webmaster | 8:f2a567ee3a46 | 285 | } |
webmaster | 8:f2a567ee3a46 | 286 | printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS); |
webmaster | 8:f2a567ee3a46 | 287 | |
webmaster | 8:f2a567ee3a46 | 288 | // Send message to server |
webmaster | 8:f2a567ee3a46 | 289 | char hello[] = "Hello World"; |
webmaster | 8:f2a567ee3a46 | 290 | printf("Sending message to Server : '%s' \n",hello); |
webmaster | 8:f2a567ee3a46 | 291 | socket.send_all(hello, sizeof(hello) - 1); |
webmaster | 8:f2a567ee3a46 | 292 | |
webmaster | 8:f2a567ee3a46 | 293 | // Receive message from server |
webmaster | 8:f2a567ee3a46 | 294 | char buf[256]; |
webmaster | 8:f2a567ee3a46 | 295 | int n = socket.receive(buf, 256); |
webmaster | 8:f2a567ee3a46 | 296 | buf[n] = '\0'; |
webmaster | 8:f2a567ee3a46 | 297 | printf("Received message from server: '%s'\n", buf); |
webmaster | 8:f2a567ee3a46 | 298 | |
webmaster | 8:f2a567ee3a46 | 299 | // Clean up |
webmaster | 8:f2a567ee3a46 | 300 | socket.close(); |
webmaster | 8:f2a567ee3a46 | 301 | eth.disconnect(); |
webmaster | 8:f2a567ee3a46 | 302 | |
webmaster | 8:f2a567ee3a46 | 303 | while(true) {} |
webmaster | 8:f2a567ee3a46 | 304 | } |