[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Threads problem

Hello,I'm writing a socket server program with multi-threads. once accept a connect, it will create a thread and process user's request, then exit. but i find thread cannot release handles after it exits from system. In windows NT4.0, when my server process 4000 requests it create 30000 handles, and make system work slowly. what's wrong with my program and how to solve this problem? please help me.

regards

wintch
0
wintch
Asked:
wintch
1 Solution
 
MichaelSCommented:
can you sow your code?
0
 
DanRollinsCommented:
Why not release the handles before exiting the thread?

-- Dan
0
 
makerpCommented:
make sure you close the socket at the end of each thread, if you use _beginthread then the handle to the thread is closed for you automatically

here is a simple example

/*
     Written By     :     Paul Maker
     Description :     a simple console app. its a echo server
                         to demonstrate stream sockets.

     NO ERROR CHECKING
*/

/* run-time stuff */
#include <stdio.h>
#include <stdlib.h>
/* _beginthread */
#include <process.h>
/* err, winsock */
#include <winsock2.h>

#include <conio.h>

#define BROWSER_OUTPUTER

/* thread controling function */
void __cdecl do_connection(void *socket)
{
     SOCKET client = (SOCKET)socket;
     char buffer[1000];

     while(true)
     {
          int r = recv(client,buffer,sizeof(buffer),0);    
          if(r == 0 || r == SOCKET_ERROR) break;
#ifdef BROWSER_OUTPUTER
          printf("SENT :\n%s\n",buffer);
#else
          send(client,buffer,sizeof(buffer),0);
#endif
     }

     closesocket(client);
}

int main(int argc, char *argv[])
{
     printf("Demo Echo server : Paul Maker %s %s\n",
             __DATE__,__TIME__);

     WSADATA wsaData;
     if(WSAStartup(0x202,&wsaData) == SOCKET_ERROR)
     {    
          printf("Error : %d\n",WSAGetLastError());
          return 0;
     }                
     /* create the socket */
     SOCKET sock = socket(AF_INET,SOCK_STREAM,0);
     struct sockaddr_in our_addr;
     /* set up our address */
     our_addr.sin_family = AF_INET;
     our_addr.sin_addr.s_addr = ADDR_ANY;
     our_addr.sin_port = htons(atoi(argv[1]));
     /* bind to it */
     if(bind(sock,(sockaddr*)&our_addr,sizeof(sockaddr_in))
          == SOCKET_ERROR)
     {
          printf("Error : %d\n",WSAGetLastError());
          return 0;
     }
     /* set it to a passive mode */
     if(listen(sock,10) == SOCKET_ERROR)
     {    
          printf("Error : %d\n",WSAGetLastError());
          return 0;
     }

     SOCKET c;
     while(true)
     {
          struct sockaddr_in client;
          int client_len = sizeof(sockaddr_in);
          /* accept the connection */
          c = accept(sock,(sockaddr*)&client,&client_len);
          /* start a new thread to deal with it  */
          _beginthread(do_connection,0,(void*)c);
     }

     closesocket(sock);

     return 1;
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
makerpCommented:
undefine BROWSER_OUTPUTER
0
 
makerpCommented:
the thread is closed for you automatically

should read

the thread handle is closed for you automatically
0
 
AssafLavieCommented:
You might want to consider implementing a thread pool for this kind of application. You wouldn't want 100 threads running for 100 clients, would you?

You can implement a thread pool by having N threads wait on a queue of actions to perform, or by using a semaphore to limit the number of threads created to service the incoming connection requests.
0
 
bkdcCommented:
are you sure you detach from the socket in the main thread prior sending it to the thread for further handling.
1. accept the connection.
2. get the socket handle
3. detach from the socket
4. pass the handle to the thread
5. (in the thread) create a socket using the handle

Hope I'm right - it's been a while
0
 
MoondancerCommented:
wintch - Date Last Logged In: 08/21/01

You have asked 6 questions and have chosen not to finalize any one of them and award the experts who helped you.  Please return to ALL your open questions to update and finalize them.  I will be posting this in all your open questions and monitoring each of them for completion:

It's time to clean up this topic area and that means taking care of this question. Your options at this point are:
 
1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.
 
2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
 
3.  Ask Community Support to help split points between participating experts.  Just comment here with details.
 
4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
 
If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.
 
PLEASE DO NOT AWARD THE POINTS TO ME.
 
____________________________________________
 
 
 
Hi Experts:
 
In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.
 
Experts, please do not add further "answer" information to this question.  I will be back in about one week to finalize this question.
 
Thank you everyone.
 
Moondancer :)
Community Support Moderator @ Experts Exchange
 
P.S.  Engineering has been advised about the error in the comment date/time sort order for year 2000 questions, they're in userID order instead.  REGARDING POINTS, Guidelines and more:  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp  
0
 
MoondancerCommented:
wintch logged in as recently as 01/29/02 and chose not to update and finalize the open questions, despite
an August request already do to do.  I will post this in them all.

EXPERTS .... please let me know what you believe to be the fair outcome here, since it appears I will be finalizing this if wintch chooses not to within the next 7 days.

Moondancer
Community Support Moderator @ Experts Exchange
0
 
MoondancerCommented:
Thanks for returning and updating this question.  Can you please tell me why you chose a "B" versus an "A" grade here?  It is always appreciated when grades are less than an A to receive comments as to why so that both experts and future PAQ readers can be advised.

If this was in error, let me know so I can correct this.  The cost to you in terms of points is unchanged, regardless of grade.  However, the expert points earned is increased by a factor of one for each grade level achieved, as noted in the HELP DESK link on the left.

Thank you.

Moondancer
Community Support Moderator @ Experts Exchange
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now