Skip to content

Commit 9226bd1

Browse files
authored
Merge pull request #11 from bcmi-labs/3-add-a-class-for-tcp-socket-clientserver
3 add a class for tcp socket clientserver
2 parents cfd44b5 + aaf220a commit 9226bd1

File tree

8 files changed

+556
-1
lines changed

8 files changed

+556
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.idea
2-
.vscode
2+
.vscode
3+
4+
CMakeLists.txt

examples/client/client.ino

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <Arduino_RouterBridge.h>
2+
3+
BridgeTCPClient<> client(Bridge);
4+
5+
void setup() {
6+
7+
if (!Bridge.begin()) {
8+
while (true) {}
9+
}
10+
11+
if (!Monitor.begin()) {
12+
while (true) {}
13+
}
14+
15+
}
16+
17+
void loop() {
18+
19+
Monitor.println("\nStarting connection to server...");
20+
/* if you get a connection, report back via serial: */
21+
if (client.connect("arduino.tips", 80) < 0) {
22+
Monitor.println("unable to connect to server");
23+
return;
24+
}
25+
26+
Monitor.println("connected to server");
27+
/* Make an HTTP request: */
28+
size_t w = client.println("GET /asciilogo.txt HTTP/1.1");
29+
w += client.println("Host: arduino.tips");
30+
w += client.println("User-Agent: Arduino");
31+
w += client.println("Connection: close");
32+
w += client.println();
33+
34+
/* if there are incoming bytes available from the server,
35+
* read them and print them:
36+
*/
37+
while (client.connected()) {
38+
size_t len = client.available();
39+
if (len) {
40+
uint8_t buff[len];
41+
client.read(buff, len);
42+
Monitor.write(buff, len);
43+
}
44+
delay(0);
45+
}
46+
47+
/* if the server's disconnected, stop the client: */
48+
Monitor.println();
49+
Monitor.println("disconnecting from server.");
50+
client.stop();
51+
delay(1000);
52+
}

examples/clientSSL/clientSSL.ino

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <Arduino_RouterBridge.h>
2+
3+
static const char ca_cert[] = {
4+
"-----BEGIN CERTIFICATE-----\n"
5+
"MIICCTCCAY6gAwIBAgINAgPlwGjvYxqccpBQUjAKBggqhkjOPQQDAzBHMQswCQYD\n"
6+
"VQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2VzIExMQzEUMBIG\n"
7+
"A1UEAxMLR1RTIFJvb3QgUjQwHhcNMTYwNjIyMDAwMDAwWhcNMzYwNjIyMDAwMDAw\n"
8+
"WjBHMQswCQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2Vz\n"
9+
"IExMQzEUMBIGA1UEAxMLR1RTIFJvb3QgUjQwdjAQBgcqhkjOPQIBBgUrgQQAIgNi\n"
10+
"AATzdHOnaItgrkO4NcWBMHtLSZ37wWHO5t5GvWvVYRg1rkDdc/eJkTBa6zzuhXyi\n"
11+
"QHY7qca4R9gq55KRanPpsXI5nymfopjTX15YhmUPoYRlBtHci8nHc8iMai/lxKvR\n"
12+
"HYqjQjBAMA4GA1UdDwEB/wQEAwIBhjAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQW\n"
13+
"BBSATNbrdP9JNqPV2Py1PsVq8JQdjDAKBggqhkjOPQQDAwNpADBmAjEA6ED/g94D\n"
14+
"9J+uHXqnLrmvT/aDHQ4thQEd0dlq7A/Cr8deVl5c1RxYIigL9zC2L7F8AjEA8GE8\n"
15+
"p/SgguMh1YQdc4acLa/KNJvxn7kjNuK8YAOdgLOaVsjh4rsUecrNIdSUtUlD\n"
16+
"-----END CERTIFICATE-----\n"
17+
};
18+
19+
BridgeTCPClient<> client(Bridge);
20+
21+
void setup() {
22+
23+
if (!Bridge.begin()) {
24+
while (true) {}
25+
}
26+
27+
if (!Monitor.begin()) {
28+
while (true) {}
29+
}
30+
31+
}
32+
33+
void loop() {
34+
Monitor.println("\nStarting connection to server...");
35+
36+
/* if you get a connection, report back via monitor: */
37+
if (client.connectSSL("arduino.tips", 443, ca_cert) < 0) {
38+
Monitor.println("unable to connect to server");
39+
return;
40+
}
41+
42+
Monitor.println("connected to server");
43+
/* Make aHTTP request: */
44+
size_t w = client.println("GET /asciilogo.txt HTTP/1.1");
45+
w += client.println("Host: arduino.tips");
46+
w += client.println("User-Agent: Arduino");
47+
w += client.println("Connection: close");
48+
w += client.println();
49+
50+
/* if there are incoming bytes available from the server,
51+
* read them and print them:
52+
*/
53+
while (client.connected()) {
54+
size_t len = client.available();
55+
if (len) {
56+
uint8_t buff[len];
57+
client.read(buff, len);
58+
Monitor.write(buff, len);
59+
}
60+
delay(0);
61+
}
62+
63+
/* if the server's disconnected, stop the client: */
64+
Monitor.println();
65+
Monitor.println("disconnecting from server.");
66+
client.stop();
67+
delay(1000);
68+
}

examples/server/python/main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# A python sketch that uses RPC Bridge to test the server.ino
2+
3+
import time
4+
from arduino.app_utils import *
5+
6+
7+
def log(msg):
8+
with open("./log.log", "a") as f:
9+
f.write(str(msg) + "\n")
10+
11+
def main():
12+
res = Bridge.call("tcp/connect", "127.0.0.1", 5678)
13+
log(f"Connection attempt id: {res}")
14+
15+
written = Bridge.call("tcp/write", res, "Hello friend")
16+
log(f"Written msg of len: {written}")
17+
18+
time.sleep(1)
19+
20+
ok = Bridge.call("tcp/close", res)
21+
log(f"Closed connection: {ok}")
22+
23+
if __name__ == "__main__":
24+
25+
while True:
26+
main()
27+
time.sleep(1)

examples/server/server.ino

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <Arduino_RouterBridge.h>
2+
3+
IPAddress localhost(127, 0, 0, 1);
4+
BridgeTCPServer<> server(Bridge, localhost, 5678);
5+
6+
void setup() {
7+
8+
if (!Bridge.begin()) {
9+
while (true) {}
10+
}
11+
12+
if (!Monitor.begin()) {
13+
while (true) {}
14+
}
15+
16+
server.begin();
17+
18+
}
19+
20+
void loop() {
21+
22+
BridgeTCPClient<> client = server.accept();
23+
24+
if (client.connected() == 1){
25+
Monitor.print("client ");
26+
Monitor.print(client.getId());
27+
Monitor.println(" connected");
28+
}
29+
30+
if (client) {
31+
Monitor.println("A client established a connection");
32+
}
33+
34+
while (client.connected()) {
35+
size_t len = client.available();
36+
if (len) {
37+
Monitor.println("Message received from client");
38+
uint8_t buff[len];
39+
client.read(buff, len);
40+
Monitor.write(buff, len);
41+
}
42+
}
43+
44+
server.disconnect(); // Disconnects the client server-side
45+
46+
}

src/Arduino_RouterBridge.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@
1515
#include "Arduino.h"
1616
#include "bridge.h"
1717
#include "monitor.h"
18+
#include "tcp_client.h"
19+
#include "tcp_server.h"
1820

1921
#endif //ARDUINO_ROUTER_BRIDGE_H

0 commit comments

Comments
 (0)