TcpClient/Reactor 框架
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
#include <c_TcpClient.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "c_Test.h"
|
||||
|
||||
int main(int argc, char** argv){
|
||||
c_Socket_Init();
|
||||
const char* server_host = "127.0.0.1";
|
||||
const char* server_port = "1313"; /* Your time server daytime service port */
|
||||
|
||||
c_TcpClient_t client;
|
||||
printf("[CLIENT] Connecting to Time Server at %s:%s...\n", server_host, server_port);
|
||||
|
||||
/* 1. Establish a raw stream connection using your explicit family-agnostic constructor */
|
||||
c_err_t err = c_TcpClient_Connect(&client, AF_INET, server_host, server_port, NULL);
|
||||
if (err != C_SUCCESS) {
|
||||
fprintf(stderr, "[ERROR] Unable to connect to remote Time Server host destination.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("[CLIENT] Connection successful! Awaiting network timestamp data package...\n");
|
||||
|
||||
char recv_buffer[128];
|
||||
memset(recv_buffer, 0, sizeof(recv_buffer));
|
||||
c_size_t total_bytes_read = 0;
|
||||
char send_buffer[1024];
|
||||
c_size_t send_bytes = 0;
|
||||
/*
|
||||
* 2. Execute a raw block read operation.
|
||||
* Loop to read stream chunks until the server sends the full packet and shuts down (returns C_ERR_EOF).
|
||||
*/
|
||||
while (true) {
|
||||
c_size_t chunk_bytes = 0;
|
||||
char* current_ptr = recv_buffer + total_bytes_read;
|
||||
c_size_t available_space = sizeof(recv_buffer) - total_bytes_read - 1;
|
||||
|
||||
if (available_space == 0) break; /* Safety boundary ceiling check */
|
||||
|
||||
if (send_bytes == 0) {
|
||||
c_size_t sz = snprintf(send_buffer, sizeof(send_buffer), "GET_TIME");
|
||||
c_TcpClient_Send(&client, send_buffer, sz, &send_bytes);
|
||||
}else {
|
||||
// c_size_t sz = snprintf(send_buffer, sizeof(send_buffer), "STOP");
|
||||
// c_TcpClient_Send(&client, send_buffer, sz, &send_bytes);
|
||||
break;
|
||||
}
|
||||
|
||||
err = c_TcpClient_Recv(&client, current_ptr, available_space, &chunk_bytes);
|
||||
|
||||
if (err == C_ERR_EOF) {
|
||||
break; /* Server closed the connection gracefully, transaction complete */
|
||||
}
|
||||
if (err != C_SUCCESS) {
|
||||
fprintf(stderr, "[ERROR] Network stream read transaction failure caught.\n");
|
||||
c_TcpClient_Destroy(&client);
|
||||
return -1;
|
||||
}
|
||||
|
||||
total_bytes_read += chunk_bytes;
|
||||
}
|
||||
|
||||
/* 3. Null-terminate the raw buffer safely before string logging operations */
|
||||
recv_buffer[total_bytes_read] = '\0';
|
||||
|
||||
printf("\n " COLOR_GREEN "[SUCCESS] Network Time String Intercepted:" COLOR_RESET "\n");
|
||||
printf(" >>> %s", recv_buffer);
|
||||
|
||||
/* 4. Safely close physical socket descriptors and reclaim global system assets */
|
||||
c_TcpClient_Destroy(&client);
|
||||
printf("\n[CLIENT] Client connection resources cleanly recycled.\n");
|
||||
|
||||
c_Socket_Destroy();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user