Solved

Threads problem

Posted on 2001-08-11
10
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

705 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