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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
| #include <mysql.h> #include <ctime> #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <sys/shm.h>
#include <iostream> #include <list> #include <thread> #include <vector> #include <string>
#define PORT 7000 #define IP "192.168.10.232" using namespace std;
int sockfd; struct sockaddr_in servaddr; socklen_t len; list<int> li; string sendbuf; void detection_message(char *buf); void sendMess(); class DataToSql { public: DataToSql(string server = "localhost", string user = "root", string password = "qwert15460", string database = "3B", size_t post = 0) : post(post) { message.emplace_back(server); message.emplace_back(user); message.emplace_back(password); message.emplace_back(database); mysql_init(&this->conn); if (!mysql_real_connect(&conn, message[0].c_str(), message[1].c_str(), message[2].c_str(), message[3].c_str(), post, NULL, 0)) { fprintf(stderr, "%s\n", mysql_error(&conn)); exit(0); } } ~DataToSql() { mysql_close(&conn); }
void recv_data(string tmp) { raw.emplace_back(tmp); int left = 0, right = 0; vector<string> tailor; while (right < tmp.size()) { if (tmp[right] != ' ') right++; else { tailor.emplace_back(tmp.substr(left, right)); right++; left = right; } } tailor.emplace_back(left, right); if (tailor.size() < 4) { for (auto i : tailor) cout << i << "\t"; cout << endl; printf("data missing\n"); time_t now = time(0); char *dt = ctime(&now); cout << "本地日期和时间:" << dt << endl; tm *gmtm = gmtime(&now); dt = asctime(gmtm); return; } get_data(tailor[0], tailor[1], tailor[2], tailor[3]); }
private: vector<string> data; MYSQL conn; size_t post; vector<string> message; vector<string> raw; void insert_sql() { for (auto &i : this->data) { auto tmp = mysql_query(&conn, i.c_str()); if (tmp != 0) printf("insert error\n"); } cout << "insert_sql execute\n" << endl; this->data.clear(); Rediect(); } void get_data(const string &temper, const string &humid, const string &press, const string &light) { string tmp = "insert into data values('" + temper + "','" + humid + "','" + press + "','" + light + "')"; this->data.emplace_back(tmp); if (this->data.size() == 10) { cout << "insert_sql()" << endl; insert_sql(); } } void Rediect() { cout << "file input to file.txt" << endl; freopen("file.txt", "w", stdout); for (auto i : raw) cout << i << "\n"; cout << endl; freopen("/dev/tty", "w", stdout); raw.clear(); } };
DataToSql sql;
void getConn() { while (1) { int conn = accept(sockfd, (struct sockaddr *)&servaddr, &len); li.push_back(conn); printf("%d\n", conn); } }
void getData() { struct timeval tv; tv.tv_sec = 10; tv.tv_usec = 0; while (1) { for (auto it = li.begin(); it != li.end(); ++it) { fd_set rfds; FD_ZERO(&rfds); int maxfd = 0; int retval = 0; FD_SET(*it, &rfds); if (maxfd < *it) { maxfd = *it; } retval = select(maxfd + 1, &rfds, NULL, NULL, &tv); if (retval == -1) { printf("select error\n"); } else if (retval == 0) { } else { char buf[1024]; memset(buf, 0, sizeof(buf)); int len = recv(*it, buf, sizeof(buf), 0); detection_message(buf); memset(buf, 0, 1024); printf("%s", buf); } } sleep(1); } }
void detection_message(char *buf) { string tmp = buf; printf("%s\n", tmp.c_str()); if (tmp.empty()) return; if (tmp[0] >= '0' && tmp[0] <= '9') {
sql.recv_data(tmp); } else { sendbuf = tmp; sendMess(); } }
void sendMess() { if (!sendbuf.empty()) { for (auto it = li.begin(); it != li.end(); ++it) { send(*it, sendbuf.c_str(), sendbuf.size(), 0); sendbuf.clear(); } } }
void set_server() { sockfd = socket(AF_INET, SOCK_STREAM, 0); memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); servaddr.sin_addr.s_addr = inet_addr(IP); if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) { perror("bind"); exit(1); } if (listen(sockfd, 20) == -1) { perror("listen"); exit(1); } len = sizeof(servaddr);
std::thread t(getConn); t.detach();
std::thread t2(getData); t2.detach(); while (1) { } }
int main() { set_server(); return 0; }
|