diff options
author | Eric Day | 2014-10-03 01:59:19 +0200 |
---|---|---|
committer | Nikias Bassen | 2014-10-03 01:59:19 +0200 |
commit | 68ab9d3e0f3ba618b0ca7e2869ba0cf1fe27b691 (patch) | |
tree | b56cbb489c74291bc75cee24a7fade7fa4220ec4 /tools | |
parent | 84fc9379083069dc4c107cca83f2cfe368785f15 (diff) | |
download | libusbmuxd-68ab9d3e0f3ba618b0ca7e2869ba0cf1fe27b691.tar.gz libusbmuxd-68ab9d3e0f3ba618b0ca7e2869ba0cf1fe27b691.tar.bz2 |
iproxy: Detach accept thread to handle multiple connections
Diffstat (limited to 'tools')
-rw-r--r-- | tools/iproxy.c | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/tools/iproxy.c b/tools/iproxy.c index bd9718c..75ffa7b 100644 --- a/tools/iproxy.c +++ b/tools/iproxy.c @@ -179,6 +179,7 @@ static void *acceptor_thread(void *arg) if (cdata->fd > 0) { close(cdata->fd); } + free(cdata); return NULL; } @@ -190,6 +191,7 @@ static void *acceptor_thread(void *arg) if (cdata->fd > 0) { close(cdata->fd); } + free(cdata); return NULL; } @@ -212,6 +214,7 @@ static void *acceptor_thread(void *arg) if (cdata->fd > 0) { close(cdata->fd); } + free(cdata); return NULL; } @@ -239,6 +242,7 @@ static void *acceptor_thread(void *arg) if (cdata->sfd > 0) { close(cdata->sfd); } + free(cdata); return NULL; } @@ -282,20 +286,26 @@ int main(int argc, char **argv) #endif struct sockaddr_in c_addr; socklen_t len = sizeof(struct sockaddr_in); - struct client_data cdata; + struct client_data *cdata; int c_sock; while (1) { printf("waiting for connection\n"); c_sock = accept(mysock, (struct sockaddr*)&c_addr, &len); if (c_sock) { printf("accepted connection, fd = %d\n", c_sock); - cdata.fd = c_sock; + cdata = (struct client_data*)malloc(sizeof(struct client_data)); + if (!cdata) { + close(c_sock); + fprintf(stderr, "ERROR: Out of memory\n"); + return -1; + } + cdata->fd = c_sock; #ifdef WIN32 - acceptor = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)acceptor_thread, &cdata, 0, NULL); - WaitForSingleObject(acceptor, INFINITE); + acceptor = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)acceptor_thread, cdata, 0, NULL); + CloseHandle(acceptor); #else - pthread_create(&acceptor, NULL, acceptor_thread, &cdata); - pthread_join(acceptor, NULL); + pthread_create(&acceptor, NULL, acceptor_thread, cdata); + pthread_detach(acceptor); #endif } else { break; |