Solved

Threads problem

Posted on 2001-08-11
10
230 Views
Last Modified: 2010-04-02
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
Comment
Question by:wintch
10 Comments
 
LVL 6

Expert Comment

by:MichaelS
ID: 6375226
can you sow your code?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6375277
Why not release the handles before exiting the thread?

-- Dan
0
 
LVL 10

Accepted Solution

by:
makerp earned 200 total points
ID: 6375336
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 10

Expert Comment

by:makerp
ID: 6375338
undefine BROWSER_OUTPUTER
0
 
LVL 10

Expert Comment

by:makerp
ID: 6375340
the thread is closed for you automatically

should read

the thread handle is closed for you automatically
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6375425
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
 
LVL 2

Expert Comment

by:bkdc
ID: 6387964
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 6446604
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 6786557
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 6792402
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question