Solved

Threads problem

Posted on 2001-08-11
10
205 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
Comment Utility
can you sow your code?
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Why not release the handles before exiting the thread?

-- Dan
0
 
LVL 10

Accepted Solution

by:
makerp earned 200 total points
Comment Utility
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
 
LVL 10

Expert Comment

by:makerp
Comment Utility
undefine BROWSER_OUTPUTER
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
the thread is closed for you automatically

should read

the thread handle is closed for you automatically
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:AssafLavie
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now