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@9:d7468574ef8f, 2017-09-27 (annotated)
- Committer:
- webmaster
- Date:
- Wed Sep 27 01:18:12 2017 +0000
- Revision:
- 9:d7468574ef8f
- Parent:
- 8:f2a567ee3a46
- Child:
- 10:aabd720e632c
add http,test_mockhsm
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 | 9:d7468574ef8f | 6 | #include "uniqueCPUID.h" |
webmaster | 8:f2a567ee3a46 | 7 | |
webmaster | 8:f2a567ee3a46 | 8 | #include <iostream> |
webmaster | 8:f2a567ee3a46 | 9 | #include <string> |
webmaster | 8:f2a567ee3a46 | 10 | #include "UnitTest.h" |
webmaster | 8:f2a567ee3a46 | 11 | //#include "BlockChain.h" |
webmaster | 8:f2a567ee3a46 | 12 | #include "MockHsm.h" |
webmaster | 8:f2a567ee3a46 | 13 | |
webmaster | 8:f2a567ee3a46 | 14 | using namespace std; |
gsteiert | 0:28bc7ce77e20 | 15 | |
gsteiert | 0:28bc7ce77e20 | 16 | #define HTTPD_SERVER_PORT 80 |
gsteiert | 0:28bc7ce77e20 | 17 | #define HTTPD_MAX_REQ_LENGTH 1023 |
gsteiert | 0:28bc7ce77e20 | 18 | #define HTTPD_MAX_HDR_LENGTH 255 |
gsteiert | 0:28bc7ce77e20 | 19 | #define HTTPD_MAX_FNAME_LENGTH 127 |
gsteiert | 3:4f71a37a1ed2 | 20 | #define HTTPD_MAX_DNAME_LENGTH 127 |
gsteiert | 3:4f71a37a1ed2 | 21 | |
webmaster | 8:f2a567ee3a46 | 22 | const char* ECHO_SERVER_ADDRESS = "192.168.31.185"; |
webmaster | 8:f2a567ee3a46 | 23 | const int ECHO_SERVER_PORT = 10003; |
webmaster | 8:f2a567ee3a46 | 24 | |
gsteiert | 3:4f71a37a1ed2 | 25 | Serial uart(USBTX, USBRX); |
gsteiert | 0:28bc7ce77e20 | 26 | |
gsteiert | 6:ca17ce165ebc | 27 | //SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD |
gsteiert | 5:27b09a0f3fb6 | 28 | //SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD |
gsteiert | 6:ca17ce165ebc | 29 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F |
gsteiert | 3:4f71a37a1ed2 | 30 | |
gsteiert | 0:28bc7ce77e20 | 31 | EthernetInterface eth; |
gsteiert | 0:28bc7ce77e20 | 32 | TCPSocketServer server; |
gsteiert | 0:28bc7ce77e20 | 33 | TCPSocketConnection client; |
webmaster | 8:f2a567ee3a46 | 34 | TCPSocketConnection my_client; |
gsteiert | 0:28bc7ce77e20 | 35 | |
gsteiert | 0:28bc7ce77e20 | 36 | char buffer[HTTPD_MAX_REQ_LENGTH+1]; |
gsteiert | 0:28bc7ce77e20 | 37 | char httpHeader[HTTPD_MAX_HDR_LENGTH+1]; |
gsteiert | 3:4f71a37a1ed2 | 38 | char fileName[HTTPD_MAX_FNAME_LENGTH+1]; |
gsteiert | 3:4f71a37a1ed2 | 39 | char dirName[HTTPD_MAX_DNAME_LENGTH+1]; |
gsteiert | 0:28bc7ce77e20 | 40 | char *uristr; |
gsteiert | 0:28bc7ce77e20 | 41 | char *eou; |
gsteiert | 0:28bc7ce77e20 | 42 | char *qrystr; |
gsteiert | 0:28bc7ce77e20 | 43 | |
gsteiert | 0:28bc7ce77e20 | 44 | FILE *fp; |
gsteiert | 0:28bc7ce77e20 | 45 | int rdCnt; |
gsteiert | 0:28bc7ce77e20 | 46 | |
webmaster | 8:f2a567ee3a46 | 47 | // function declation |
webmaster | 8:f2a567ee3a46 | 48 | int test_socket_client(void); |
webmaster | 8:f2a567ee3a46 | 49 | int socket_init(); |
webmaster | 8:f2a567ee3a46 | 50 | int socket_fini(); |
webmaster | 8:f2a567ee3a46 | 51 | |
webmaster | 8:f2a567ee3a46 | 52 | int test_MockHsm() |
webmaster | 8:f2a567ee3a46 | 53 | { |
webmaster | 8:f2a567ee3a46 | 54 | MockHsm mh; |
webmaster | 8:f2a567ee3a46 | 55 | mh.setAlias("cpp_key"); |
webmaster | 8:f2a567ee3a46 | 56 | //cout<<mh.getAlias()<<endl; |
webmaster | 8:f2a567ee3a46 | 57 | //cout<<mh.createKey()<<endl; |
webmaster | 8:f2a567ee3a46 | 58 | cout<< mh.listKeys() <<endl; |
webmaster | 8:f2a567ee3a46 | 59 | return 0; |
webmaster | 8:f2a567ee3a46 | 60 | } |
webmaster | 9:d7468574ef8f | 61 | void test_uuid() |
webmaster | 9:d7468574ef8f | 62 | { |
webmaster | 9:d7468574ef8f | 63 | char pUUID[128] = {0}; |
webmaster | 9:d7468574ef8f | 64 | getUniqueIDAsStr(pUUID); |
webmaster | 9:d7468574ef8f | 65 | printf("CPUID = %s \n",pUUID); |
webmaster | 9:d7468574ef8f | 66 | //printUniqueId(uart); |
webmaster | 9:d7468574ef8f | 67 | } |
webmaster | 8:f2a567ee3a46 | 68 | |
webmaster | 8:f2a567ee3a46 | 69 | int main(void) |
webmaster | 8:f2a567ee3a46 | 70 | { |
webmaster | 8:f2a567ee3a46 | 71 | printf("main start\n"); |
webmaster | 8:f2a567ee3a46 | 72 | |
webmaster | 9:d7468574ef8f | 73 | test_uuid(); |
webmaster | 9:d7468574ef8f | 74 | |
webmaster | 8:f2a567ee3a46 | 75 | socket_init(); |
webmaster | 8:f2a567ee3a46 | 76 | |
webmaster | 8:f2a567ee3a46 | 77 | test_MockHsm(); |
webmaster | 8:f2a567ee3a46 | 78 | |
webmaster | 8:f2a567ee3a46 | 79 | test_socket_client(); |
webmaster | 8:f2a567ee3a46 | 80 | |
webmaster | 8:f2a567ee3a46 | 81 | socket_fini(); |
webmaster | 8:f2a567ee3a46 | 82 | |
webmaster | 8:f2a567ee3a46 | 83 | printf("main end\n"); |
webmaster | 8:f2a567ee3a46 | 84 | return 0; |
webmaster | 8:f2a567ee3a46 | 85 | } |
webmaster | 8:f2a567ee3a46 | 86 | |
webmaster | 8:f2a567ee3a46 | 87 | int socket_init() |
webmaster | 8:f2a567ee3a46 | 88 | { |
webmaster | 8:f2a567ee3a46 | 89 | int ret = 0; |
webmaster | 8:f2a567ee3a46 | 90 | // EthernetInterface eth; |
webmaster | 8:f2a567ee3a46 | 91 | printf("Initializing Ethernet\n"); |
webmaster | 8:f2a567ee3a46 | 92 | ret = eth.init(); //Use DHCP |
webmaster | 8:f2a567ee3a46 | 93 | //eth.init("192.168.31.186", "255.255.255.0", "192.168.31.1"); //Use static ip |
webmaster | 8:f2a567ee3a46 | 94 | printf("Connecting\n"); |
webmaster | 8:f2a567ee3a46 | 95 | ret = eth.connect(); |
webmaster | 8:f2a567ee3a46 | 96 | printf("IP Address is %s\n", eth.getIPAddress()); |
webmaster | 8:f2a567ee3a46 | 97 | |
webmaster | 8:f2a567ee3a46 | 98 | return ret; |
webmaster | 8:f2a567ee3a46 | 99 | } |
webmaster | 8:f2a567ee3a46 | 100 | |
webmaster | 8:f2a567ee3a46 | 101 | int socket_fini() |
webmaster | 8:f2a567ee3a46 | 102 | { |
webmaster | 8:f2a567ee3a46 | 103 | return eth.disconnect(); |
webmaster | 8:f2a567ee3a46 | 104 | } |
webmaster | 8:f2a567ee3a46 | 105 | |
webmaster | 8:f2a567ee3a46 | 106 | int test_socket_client(void) |
webmaster | 8:f2a567ee3a46 | 107 | { |
webmaster | 8:f2a567ee3a46 | 108 | TCPSocketConnection socket; |
webmaster | 8:f2a567ee3a46 | 109 | while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { |
webmaster | 8:f2a567ee3a46 | 110 | printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
webmaster | 8:f2a567ee3a46 | 111 | wait(1); |
webmaster | 8:f2a567ee3a46 | 112 | } |
webmaster | 8:f2a567ee3a46 | 113 | printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS); |
webmaster | 8:f2a567ee3a46 | 114 | |
webmaster | 8:f2a567ee3a46 | 115 | // Send message to server |
webmaster | 8:f2a567ee3a46 | 116 | char hello[] = "Hello World"; |
webmaster | 8:f2a567ee3a46 | 117 | printf("Sending message to Server : '%s' \n",hello); |
webmaster | 8:f2a567ee3a46 | 118 | socket.send_all(hello, sizeof(hello) - 1); |
webmaster | 8:f2a567ee3a46 | 119 | |
webmaster | 8:f2a567ee3a46 | 120 | // Receive message from server |
webmaster | 8:f2a567ee3a46 | 121 | char buf[256]; |
webmaster | 8:f2a567ee3a46 | 122 | int n = socket.receive(buf, 256); |
webmaster | 8:f2a567ee3a46 | 123 | buf[n] = '\0'; |
webmaster | 8:f2a567ee3a46 | 124 | printf("Received message from server: '%s'\n", buf); |
webmaster | 8:f2a567ee3a46 | 125 | |
webmaster | 8:f2a567ee3a46 | 126 | // Clean up |
webmaster | 8:f2a567ee3a46 | 127 | socket.close(); |
webmaster | 8:f2a567ee3a46 | 128 | |
webmaster | 8:f2a567ee3a46 | 129 | return 0; |
webmaster | 8:f2a567ee3a46 | 130 | } |
webmaster | 8:f2a567ee3a46 | 131 | |
gsteiert | 0:28bc7ce77e20 | 132 | void get_file(char* uri) |
gsteiert | 0:28bc7ce77e20 | 133 | { |
gsteiert | 3:4f71a37a1ed2 | 134 | uart.printf("get_file %s\n", uri); |
gsteiert | 0:28bc7ce77e20 | 135 | char *lstchr = strrchr(uri, NULL) -1; |
gsteiert | 0:28bc7ce77e20 | 136 | if ('/' == *lstchr) { |
gsteiert | 3:4f71a37a1ed2 | 137 | uart.printf("Open directory /sd%s\n", uri); |
gsteiert | 0:28bc7ce77e20 | 138 | *lstchr = 0; |
gsteiert | 3:4f71a37a1ed2 | 139 | sprintf(fileName, "/sd%s", uri); |
gsteiert | 3:4f71a37a1ed2 | 140 | DIR *d = opendir(fileName); |
gsteiert | 0:28bc7ce77e20 | 141 | if (d != NULL) { |
gsteiert | 0:28bc7ce77e20 | 142 | sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); |
gsteiert | 0:28bc7ce77e20 | 143 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 3:4f71a37a1ed2 | 144 | sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri); |
gsteiert | 0:28bc7ce77e20 | 145 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 0:28bc7ce77e20 | 146 | struct dirent *p; |
gsteiert | 0:28bc7ce77e20 | 147 | while((p = readdir(d)) != NULL) { |
gsteiert | 3:4f71a37a1ed2 | 148 | sprintf(dirName, "%s/%s", fileName, p->d_name); |
gsteiert | 3:4f71a37a1ed2 | 149 | uart.printf("%s\n", dirName); |
gsteiert | 3:4f71a37a1ed2 | 150 | DIR *subDir = opendir(dirName); |
gsteiert | 3:4f71a37a1ed2 | 151 | if (subDir != NULL) { |
gsteiert | 3:4f71a37a1ed2 | 152 | sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name); |
gsteiert | 3:4f71a37a1ed2 | 153 | } else { |
gsteiert | 3:4f71a37a1ed2 | 154 | sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name); |
gsteiert | 3:4f71a37a1ed2 | 155 | } |
gsteiert | 0:28bc7ce77e20 | 156 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 0:28bc7ce77e20 | 157 | } |
gsteiert | 0:28bc7ce77e20 | 158 | } |
gsteiert | 0:28bc7ce77e20 | 159 | closedir(d); |
gsteiert | 3:4f71a37a1ed2 | 160 | uart.printf("Directory closed\n"); |
gsteiert | 0:28bc7ce77e20 | 161 | sprintf(httpHeader,"</ul></body></html>"); |
gsteiert | 0:28bc7ce77e20 | 162 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 0:28bc7ce77e20 | 163 | } else { |
gsteiert | 3:4f71a37a1ed2 | 164 | sprintf(fileName, "/sd%s", uri); |
gsteiert | 3:4f71a37a1ed2 | 165 | fp = fopen(fileName, "r"); |
gsteiert | 0:28bc7ce77e20 | 166 | if (fp == NULL) { |
gsteiert | 7:04744a9ce2b9 | 167 | uart.printf("File not found\n"); |
gsteiert | 0:28bc7ce77e20 | 168 | sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
gsteiert | 0:28bc7ce77e20 | 169 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 0:28bc7ce77e20 | 170 | client.send(uri,strlen(uri)); |
gsteiert | 0:28bc7ce77e20 | 171 | } else { |
gsteiert | 7:04744a9ce2b9 | 172 | uart.printf("Sending: header"); |
gsteiert | 0:28bc7ce77e20 | 173 | sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); |
gsteiert | 0:28bc7ce77e20 | 174 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 7:04744a9ce2b9 | 175 | uart.printf(" file"); |
gsteiert | 0:28bc7ce77e20 | 176 | while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) { |
gsteiert | 0:28bc7ce77e20 | 177 | client.send(buffer, rdCnt); |
gsteiert | 7:04744a9ce2b9 | 178 | uart.printf("."); |
gsteiert | 0:28bc7ce77e20 | 179 | } |
gsteiert | 0:28bc7ce77e20 | 180 | client.send(buffer, rdCnt); |
gsteiert | 0:28bc7ce77e20 | 181 | fclose(fp); |
gsteiert | 7:04744a9ce2b9 | 182 | uart.printf("done\n"); |
gsteiert | 0:28bc7ce77e20 | 183 | } |
gsteiert | 0:28bc7ce77e20 | 184 | } |
gsteiert | 0:28bc7ce77e20 | 185 | } |
gsteiert | 0:28bc7ce77e20 | 186 | |
webmaster | 8:f2a567ee3a46 | 187 | int main_1 (void) |
gsteiert | 0:28bc7ce77e20 | 188 | { |
gsteiert | 3:4f71a37a1ed2 | 189 | // Serial Interface eth; |
gsteiert | 3:4f71a37a1ed2 | 190 | uart.baud(115200); |
gsteiert | 5:27b09a0f3fb6 | 191 | uart.printf("Initializing\n"); |
gsteiert | 3:4f71a37a1ed2 | 192 | |
gsteiert | 3:4f71a37a1ed2 | 193 | // Check File System |
gsteiert | 5:27b09a0f3fb6 | 194 | uart.printf("Checking File System\n"); |
gsteiert | 3:4f71a37a1ed2 | 195 | DIR *d = opendir("/sd/"); |
gsteiert | 3:4f71a37a1ed2 | 196 | if (d != NULL) { |
gsteiert | 3:4f71a37a1ed2 | 197 | uart.printf("SD Card Present\n"); |
gsteiert | 3:4f71a37a1ed2 | 198 | } else { |
gsteiert | 3:4f71a37a1ed2 | 199 | uart.printf("SD Card Root Directory Not Found\n"); |
gsteiert | 3:4f71a37a1ed2 | 200 | } |
gsteiert | 3:4f71a37a1ed2 | 201 | |
gsteiert | 0:28bc7ce77e20 | 202 | // EthernetInterface eth; |
gsteiert | 5:27b09a0f3fb6 | 203 | uart.printf("Initializing Ethernet\n"); |
webmaster | 8:f2a567ee3a46 | 204 | //eth.init(); //Use DHCP |
webmaster | 8:f2a567ee3a46 | 205 | eth.init("192.168.31.186", "255.255.255.0", "192.168.31.1"); //Use |
gsteiert | 5:27b09a0f3fb6 | 206 | uart.printf("Connecting\n"); |
gsteiert | 0:28bc7ce77e20 | 207 | eth.connect(); |
gsteiert | 3:4f71a37a1ed2 | 208 | uart.printf("IP Address is %s\n", eth.getIPAddress()); |
webmaster | 8:f2a567ee3a46 | 209 | |
webmaster | 8:f2a567ee3a46 | 210 | //test socket client |
webmaster | 8:f2a567ee3a46 | 211 | //my_client.connect("192.168.31.185",10003); |
webmaster | 8:f2a567ee3a46 | 212 | //my_client.send_all("hello world",11); |
webmaster | 8:f2a567ee3a46 | 213 | |
webmaster | 8:f2a567ee3a46 | 214 | TCPSocketConnection socket; |
webmaster | 8:f2a567ee3a46 | 215 | while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { |
webmaster | 8:f2a567ee3a46 | 216 | uart.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
webmaster | 8:f2a567ee3a46 | 217 | wait(1); |
webmaster | 8:f2a567ee3a46 | 218 | } |
webmaster | 8:f2a567ee3a46 | 219 | uart.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS); |
webmaster | 8:f2a567ee3a46 | 220 | |
webmaster | 8:f2a567ee3a46 | 221 | // Send message to server |
webmaster | 8:f2a567ee3a46 | 222 | char hello[] = "Hello World"; |
webmaster | 8:f2a567ee3a46 | 223 | uart.printf("Sending message to Server : '%s' \n",hello); |
webmaster | 8:f2a567ee3a46 | 224 | socket.send_all(hello, sizeof(hello) - 1); |
webmaster | 8:f2a567ee3a46 | 225 | |
webmaster | 8:f2a567ee3a46 | 226 | // Receive message from server |
webmaster | 8:f2a567ee3a46 | 227 | char buf[256]; |
webmaster | 8:f2a567ee3a46 | 228 | int n = socket.receive(buf, 256); |
webmaster | 8:f2a567ee3a46 | 229 | buf[n] = '\0'; |
webmaster | 8:f2a567ee3a46 | 230 | uart.printf("Received message from server: '%s'\n", buf); |
webmaster | 8:f2a567ee3a46 | 231 | |
webmaster | 8:f2a567ee3a46 | 232 | // Clean up |
webmaster | 8:f2a567ee3a46 | 233 | socket.close(); |
webmaster | 8:f2a567ee3a46 | 234 | //////////////////////////////////////////////////////////////////////////// |
gsteiert | 0:28bc7ce77e20 | 235 | |
gsteiert | 0:28bc7ce77e20 | 236 | // TCPSocketServer server; |
gsteiert | 0:28bc7ce77e20 | 237 | server.bind(HTTPD_SERVER_PORT); |
gsteiert | 0:28bc7ce77e20 | 238 | server.listen(); |
gsteiert | 5:27b09a0f3fb6 | 239 | uart.printf("Server Listening\n"); |
gsteiert | 0:28bc7ce77e20 | 240 | |
gsteiert | 0:28bc7ce77e20 | 241 | while (true) { |
gsteiert | 3:4f71a37a1ed2 | 242 | uart.printf("\nWait for new connection...\r\n"); |
gsteiert | 0:28bc7ce77e20 | 243 | server.accept(client); |
gsteiert | 0:28bc7ce77e20 | 244 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
webmaster | 8:f2a567ee3a46 | 245 | |
webmaster | 8:f2a567ee3a46 | 246 | my_client.send_all("hello world",11); |
gsteiert | 0:28bc7ce77e20 | 247 | |
gsteiert | 3:4f71a37a1ed2 | 248 | uart.printf("Connection from: %s\r\n", client.get_address()); |
gsteiert | 0:28bc7ce77e20 | 249 | while (true) { |
gsteiert | 0:28bc7ce77e20 | 250 | int n = client.receive(buffer, sizeof(buffer)); |
gsteiert | 0:28bc7ce77e20 | 251 | if (n <= 0) break; |
gsteiert | 3:4f71a37a1ed2 | 252 | uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer); |
gsteiert | 0:28bc7ce77e20 | 253 | if (n >= 1024) { |
gsteiert | 0:28bc7ce77e20 | 254 | sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
gsteiert | 0:28bc7ce77e20 | 255 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 0:28bc7ce77e20 | 256 | client.send(buffer,n); |
gsteiert | 0:28bc7ce77e20 | 257 | break; |
gsteiert | 0:28bc7ce77e20 | 258 | } else { |
gsteiert | 0:28bc7ce77e20 | 259 | buffer[n]=0; |
gsteiert | 0:28bc7ce77e20 | 260 | } |
gsteiert | 0:28bc7ce77e20 | 261 | if (!strncmp(buffer, "GET ", 4)) { |
gsteiert | 0:28bc7ce77e20 | 262 | uristr = buffer + 4; |
gsteiert | 0:28bc7ce77e20 | 263 | eou = strstr(uristr, " "); |
gsteiert | 0:28bc7ce77e20 | 264 | if (eou == NULL) { |
gsteiert | 0:28bc7ce77e20 | 265 | sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
gsteiert | 0:28bc7ce77e20 | 266 | client.send(httpHeader,strlen(httpHeader)); |
gsteiert | 0:28bc7ce77e20 | 267 | client.send(buffer,n); |
gsteiert | 0:28bc7ce77e20 | 268 | } else { |
gsteiert | 0:28bc7ce77e20 | 269 | *eou = 0; |
gsteiert | 3:4f71a37a1ed2 | 270 | get_file(uristr); |
gsteiert | 0:28bc7ce77e20 | 271 | } |
gsteiert | 0:28bc7ce77e20 | 272 | } |
gsteiert | 0:28bc7ce77e20 | 273 | } |
gsteiert | 0:28bc7ce77e20 | 274 | |
gsteiert | 0:28bc7ce77e20 | 275 | client.close(); |
gsteiert | 0:28bc7ce77e20 | 276 | } |
gsteiert | 0:28bc7ce77e20 | 277 | } |
webmaster | 8:f2a567ee3a46 | 278 | |
webmaster | 8:f2a567ee3a46 | 279 | |
webmaster | 8:f2a567ee3a46 | 280 | int main_2() { |
webmaster | 8:f2a567ee3a46 | 281 | // Serial Interface eth; |
webmaster | 8:f2a567ee3a46 | 282 | uart.baud(115200); |
webmaster | 8:f2a567ee3a46 | 283 | uart.printf("Initializing\n"); |
webmaster | 8:f2a567ee3a46 | 284 | |
webmaster | 8:f2a567ee3a46 | 285 | EthernetInterface eth; |
webmaster | 8:f2a567ee3a46 | 286 | eth.init(); //Use DHCP |
webmaster | 8:f2a567ee3a46 | 287 | eth.connect(); |
webmaster | 8:f2a567ee3a46 | 288 | printf("\nClient IP Address is %s\n", eth.getIPAddress()); |
webmaster | 8:f2a567ee3a46 | 289 | |
webmaster | 8:f2a567ee3a46 | 290 | // Connect to Server |
webmaster | 8:f2a567ee3a46 | 291 | TCPSocketConnection socket; |
webmaster | 8:f2a567ee3a46 | 292 | while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { |
webmaster | 8:f2a567ee3a46 | 293 | printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
webmaster | 8:f2a567ee3a46 | 294 | wait(1); |
webmaster | 8:f2a567ee3a46 | 295 | } |
webmaster | 8:f2a567ee3a46 | 296 | printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS); |
webmaster | 8:f2a567ee3a46 | 297 | |
webmaster | 8:f2a567ee3a46 | 298 | // Send message to server |
webmaster | 8:f2a567ee3a46 | 299 | char hello[] = "Hello World"; |
webmaster | 8:f2a567ee3a46 | 300 | printf("Sending message to Server : '%s' \n",hello); |
webmaster | 8:f2a567ee3a46 | 301 | socket.send_all(hello, sizeof(hello) - 1); |
webmaster | 8:f2a567ee3a46 | 302 | |
webmaster | 8:f2a567ee3a46 | 303 | // Receive message from server |
webmaster | 8:f2a567ee3a46 | 304 | char buf[256]; |
webmaster | 8:f2a567ee3a46 | 305 | int n = socket.receive(buf, 256); |
webmaster | 8:f2a567ee3a46 | 306 | buf[n] = '\0'; |
webmaster | 8:f2a567ee3a46 | 307 | printf("Received message from server: '%s'\n", buf); |
webmaster | 8:f2a567ee3a46 | 308 | |
webmaster | 8:f2a567ee3a46 | 309 | // Clean up |
webmaster | 8:f2a567ee3a46 | 310 | socket.close(); |
webmaster | 8:f2a567ee3a46 | 311 | eth.disconnect(); |
webmaster | 8:f2a567ee3a46 | 312 | |
webmaster | 8:f2a567ee3a46 | 313 | while(true) {} |
webmaster | 8:f2a567ee3a46 | 314 | } |