Merge pull request '优化controler逻辑,优化代码结构' (#9) from yuankaifeng_branch into master
	
		
	
				
					
				
			
						commit
						7919cc1304
					
				
											
												Binary file not shown.
											
										
									
								| @ -1,51 +0,0 @@ | ||||
| #include <iostream> | ||||
| #include <sys/socket.h> | ||||
| #include <netinet/in.h> | ||||
| #include <arpa/inet.h> | ||||
| #include <unistd.h> | ||||
| #include <cstring> | ||||
| #define PORT 8888 | ||||
| 
 | ||||
| int main() { | ||||
|     int sock = 0; | ||||
|     struct sockaddr_in server_addr; | ||||
|     const char *message = "Hello Server"; | ||||
|     char buffer[1024] = {0}; | ||||
|     int valread; | ||||
| 
 | ||||
|     // Creating socket
 | ||||
|     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | ||||
|         std::cerr << "Socket creation failed" << std::endl; | ||||
|         return -1; | ||||
|     } | ||||
| 
 | ||||
|     server_addr.sin_family = AF_INET; | ||||
|     server_addr.sin_port = htons(PORT); | ||||
| 
 | ||||
|     // Get server address
 | ||||
|     if (inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) <= 0) { | ||||
|         std::cerr << "Invalid address or Address not supported" << std::endl; | ||||
|         return -1; | ||||
|     } | ||||
| 
 | ||||
|     // Connect the client socket to the server
 | ||||
|     if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { | ||||
|         std::cerr << "Connection with the server failed" << std::endl; | ||||
|         return -1; | ||||
|     } | ||||
| 
 | ||||
|     // Send some data
 | ||||
|     if (send(sock, message, strlen(message), 0) < 0) { | ||||
|         std::cerr << "Send failed" << std::endl; | ||||
|         return -1; | ||||
|     } | ||||
| 
 | ||||
|     printf("Message sent\n"); | ||||
| 
 | ||||
|     // Receive a reply from the server
 | ||||
|     valread = read(sock, buffer, 1024); | ||||
|     printf("Message from server : %s\n", buffer); | ||||
| 
 | ||||
|     close(sock); | ||||
|     return 0; | ||||
| } | ||||
| @ -1,57 +0,0 @@ | ||||
| #include <iostream> | ||||
| #include <cstring> | ||||
| #include <sys/socket.h> | ||||
| #include <netinet/in.h> | ||||
| #include <unistd.h> | ||||
| 
 | ||||
| #define PORT 8888 | ||||
| 
 | ||||
| int main() { | ||||
|     int server_fd, new_socket; | ||||
|     struct sockaddr_in address; | ||||
|     int opt = 1; | ||||
|     int addrlen = sizeof(address); | ||||
|     char buffer[1024] = {0}; | ||||
|     const char *response = "Hello Client, I have received your data"; | ||||
| 
 | ||||
|     // Creating socket
 | ||||
|     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { | ||||
|         perror("Socket creation failed"); | ||||
|         exit(EXIT_FAILURE); | ||||
|     } | ||||
| 
 | ||||
|     // Forcefully attaching socket to the port 8080
 | ||||
|     if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { | ||||
|         perror("Setsockopt failed"); | ||||
|         exit(EXIT_FAILURE); | ||||
|     } | ||||
|     address.sin_family = AF_INET; | ||||
|     address.sin_addr.s_addr = INADDR_ANY; | ||||
|     address.sin_port = htons(PORT); | ||||
| 
 | ||||
|     // Forcefully attaching socket to the port 8080
 | ||||
|     if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { | ||||
|         perror("Bind failed"); | ||||
|         exit(EXIT_FAILURE); | ||||
|     } | ||||
|     if (listen(server_fd, 3) < 0) { | ||||
|         perror("Listen failed"); | ||||
|         exit(EXIT_FAILURE); | ||||
|     } | ||||
| 
 | ||||
|     if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { | ||||
|         perror("Accept failed"); | ||||
|         exit(EXIT_FAILURE); | ||||
|     } | ||||
| 
 | ||||
|     read(new_socket, buffer, 1024); | ||||
|     std::cout << "Message from client: " << buffer << std::endl; | ||||
| 
 | ||||
|     // send the message back to the client
 | ||||
|     send(new_socket, response, strlen(response), 0); | ||||
|     printf("Hello message sent\n"); | ||||
| 
 | ||||
|     close(new_socket); | ||||
|     close(server_fd); | ||||
|     return 0; | ||||
| } | ||||
											
												Binary file not shown.
											
										
									
								| @ -0,0 +1,22 @@ | ||||
| #ifndef CONNECTOR_CONTROLLER_H | ||||
| #define CONNECTOR_CONTROLLER_H | ||||
| 
 | ||||
| #include <iostream> | ||||
| #include <thread> | ||||
| #include <vector> | ||||
| #include <map> | ||||
| #include <mutex> | ||||
| #include <atomic> | ||||
| #include <sys/socket.h> | ||||
| #include <netinet/in.h> | ||||
| #include <unistd.h> | ||||
| #include <arpa/inet.h> | ||||
| #include <cstring> | ||||
| 
 | ||||
| // 函数声明
 | ||||
| void StartHttpServer(); | ||||
| void ConnectToGateway(const std::string& ip, int port); | ||||
| void KeepConnection(int sock); | ||||
| void HandleHttpRequest(int client_socket); | ||||
| 
 | ||||
| #endif // CONNECTOR_CONTROLLER_H
 | ||||
| @ -0,0 +1,10 @@ | ||||
| #ifndef CONNECTOR_DATABASE_H | ||||
| #define CONNECTOR_DATABASE_H | ||||
| 
 | ||||
| #include <iostream> | ||||
| #include <mysql/mysql.h> | ||||
| 
 | ||||
| // 函数声明
 | ||||
| void DatabaseOperation(); | ||||
| 
 | ||||
| #endif // CONNECTOR_DATABASE_H
 | ||||
| @ -0,0 +1,35 @@ | ||||
| #include <iostream> | ||||
| #include <string> | ||||
| #include "connector_controller.h" | ||||
| #include "connector_database.h" | ||||
| 
 | ||||
| int main() { | ||||
|     std::string command; | ||||
|     bool running = true; | ||||
| 
 | ||||
|     while (running) { | ||||
|         std::cout << "Enter command (start_http_server/connect_gateway/database_operation/exit): "; | ||||
|         std::cin >> command; | ||||
| 
 | ||||
|         if (command == "start_http_server") { | ||||
|             StartHttpServer(); | ||||
|         } else if (command == "connect_gateway") { | ||||
|             std::string ip; | ||||
|             int port; | ||||
|             std::cout << "Enter the gateway IP address: "<<std::endl; | ||||
|             std::cin>>ip; // 使用getline以获取包含空格的IP地址
 | ||||
|             std::cout << "Enter the gateway port: "<<std::endl; | ||||
|             std::cin >> port; | ||||
|             ConnectToGateway(ip, port); | ||||
|         } else if (command == "database_operation") { | ||||
|             DatabaseOperation(); | ||||
|         } else if (command == "exit") { | ||||
|             running = false; | ||||
|         } else { | ||||
|             std::cout << "Unknown command" << std::endl; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     std::cout << "Exiting program." << std::endl; | ||||
|     return 0; | ||||
| } | ||||
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								
					Loading…
					
					
				
		Reference in new issue