From 0d8b5de537438f6493b3eee62c60f7705fa65198 Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Thu, 19 Dec 2024 10:59:04 +0100 Subject: [PATCH] simulator: clean shutdown of socket --- test/simulator/simulator.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/simulator/simulator.c b/test/simulator/simulator.c index 13296c5f8..aa9c8ab94 100644 --- a/test/simulator/simulator.c +++ b/test/simulator/simulator.c @@ -32,6 +32,7 @@ #include #include #include +#include #include static const char* _simulator_version = "1.0.0"; @@ -41,6 +42,8 @@ static const char* _simulator_version = "1.0.0"; int data_len; int commfd; +static int sockfd; + int get_usb_message_socket(uint8_t* input) { return read(commfd, input, USB_HID_REPORT_OUT_SIZE); @@ -67,8 +70,15 @@ void simulate_firmware_execution(const uint8_t* input) usb_processing_process(usb_processing_hww()); } +static void _int_handler(int _signum) +{ + printf("\n\nGot Ctrl-C, exiting\n\n"); + close(sockfd); +} + int main(int argc, char* argv[]) { + signal(SIGINT, _int_handler); // Default port number int portno = 15423; @@ -124,7 +134,7 @@ int main(int argc, char* argv[]) idle_workflow_blocking(); // Establish socket connection with client - int sockfd = socket(AF_INET, SOCK_STREAM, 0); + sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); return 1;