1use std::io::{Read, Write};
2use std::net::TcpStream;
3use std::net::Ipv4Addr;
4use std::str::FromStr;
5use std::string::FromUtf8Error;
6
7pub const LOCATION:Ipv4Addr = Ipv4Addr::new(127,0,0,1);
8
9#[derive(Debug)]
10pub struct User{
11 account:String,
12 password:String
13
14}
15
16#[derive(Debug)]
17pub struct Link{
18 user:User,
19 ip:Ipv4Addr,
20}
21impl Link{
22 pub fn new(ip:String) -> Link {
23 Self{
24 user: User {
25 account: "".to_string(),
26 password: "".to_string(),
27 },
28 ip: Ipv4Addr::from_str(ip.as_str()).unwrap(),
29 }
30 }
31 pub fn with_user(&mut self,user:String){
32 self.user.account = user;
33 }
34 pub fn with_password(&mut self,password:String){
35 self.user.password = password;
36 }
37 pub fn socket(&self, statement:String) -> Result<String, FromUtf8Error> {
38 let mut tcp = TcpStream::connect((self.ip,3045)).unwrap();
39 let input = format!("{} {} {}",self.user.account,self.user.password,statement).as_bytes().to_vec();
40 let mut output = [0;100000];
41 tcp.write(input.as_slice()).unwrap();
42 tcp.read(output.as_mut_slice()).unwrap();
43 let mut nxs = vec![];
44 for i in output{
45 if i != 0{
46 nxs.push(i);
47 }
48 }
49 let xs = String::from_utf8(nxs);
50 return xs
51 }
52}