-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathmain.rs
64 lines (47 loc) · 1.69 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
extern crate chrono;
use std::net::{TcpStream};
use std::io::{Read, Write};
use chrono::{DateTime, Utc};
// Function to receive messeges from server
fn receive_mes(mut stream: TcpStream){
let mut data = [0; 1024];
match stream.read(&mut data) {
Ok(_) => {
println!("{}",String::from_utf8_lossy(&data[..]));
},
Err(e) => {
println!("Failed to receive data: {}", e);
}
}
}
// Function to register a device
fn register_dev(dev: String, time: String){
match TcpStream::connect("localhost:2552") {
Ok(mut stream) => {
println!("Successfully registered in server");
let msg = format!("SHARE #pubkey KEY @senz #time {} ^{} signature ",time,dev);
stream.write(msg.as_bytes()).unwrap();
println!("Server response:");
let mut data = [0; 1024];
match stream.read(&mut data) {
Ok(_) => {
println!("{}",String::from_utf8_lossy(&data[..]));
println!("Ready to receive a image");
receive_mes(stream);
},
Err(e) => {
println!("Failed to receive data: {}", e);
}
}
},
Err(e) => {
println!("Failed to connect: {}", e);
}
}
println!("Terminated.");
}
fn main() {
let device = "device_1"; //device name
let now: DateTime<Utc> = Utc::now(); // timestamp
register_dev(device.to_string(),now.to_string());
}