Skip to content

Commit c93367d

Browse files
authored
Merge pull request #1345 from NickeZ/nickez/simulator-clean-shutdown
simulator: clean shutdown of socket
2 parents e6251ca + 0d8b5de commit c93367d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

test/simulator/simulator.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <getopt.h>
3333
#include <netdb.h>
3434
#include <netinet/in.h>
35+
#include <signal.h>
3536
#include <sys/socket.h>
3637

3738
static const char* _simulator_version = "1.0.0";
@@ -41,6 +42,8 @@ static const char* _simulator_version = "1.0.0";
4142
int data_len;
4243
int commfd;
4344

45+
static int sockfd;
46+
4447
int get_usb_message_socket(uint8_t* input)
4548
{
4649
return read(commfd, input, USB_HID_REPORT_OUT_SIZE);
@@ -67,8 +70,15 @@ void simulate_firmware_execution(const uint8_t* input)
6770
usb_processing_process(usb_processing_hww());
6871
}
6972

73+
static void _int_handler(int _signum)
74+
{
75+
printf("\n\nGot Ctrl-C, exiting\n\n");
76+
close(sockfd);
77+
}
78+
7079
int main(int argc, char* argv[])
7180
{
81+
signal(SIGINT, _int_handler);
7282
// Default port number
7383
int portno = 15423;
7484

@@ -124,7 +134,7 @@ int main(int argc, char* argv[])
124134
idle_workflow_blocking();
125135

126136
// Establish socket connection with client
127-
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
137+
sockfd = socket(AF_INET, SOCK_STREAM, 0);
128138
if (sockfd < 0) {
129139
perror("ERROR opening socket");
130140
return 1;

0 commit comments

Comments
 (0)