|
당연히 문제가 됩니다.
Thread의 경우 당연히 한계가 있습니다. Thread 당 1MB의 메모리를 먹습니다.
그럴 경우에 Worker thread 모델로 코딩을 하던가
IOCP를 사용하셔야 합니다.
testcode~
역삼골 님이 쓰신 글 :
: 왜 4천건정도 접속 후에 접속이 않됩니다.
: 서버 소켓을 클로즈하고 접속하면 다시 또 접속은 되지만 또 4천 후면 접속이 않되네요.
: 왜 그런지 모르겠습니다.
:
: 테스트용 프로그램을 만들어서 계속 접속하고 해제하고 반복해서 돌리고 있습니다.
:
: //---------------------------------------------------------------------------
: //Socket Thread Create Event
: //---------------------------------------------------------------------------
: void __fastcall Tfkmsmain::AuthServerSocketGetThread(TObject *Sender,
: TServerClientWinSocket *ClientSocket,
: TServerClientThread *&SocketThread)
: {
: try
: {
: //TServerClientThread 를 생성
: SocketThread = new TServerSocketThread(false, ClientSocket);
: }
: //Excetion process
: __except(EXCEPTION_EXECUTE_HANDLER)
: {
: }
: }
:
: //---------------------------------------------------------------------------
: //Thread Start Event
: //---------------------------------------------------------------------------
: void __fastcall Tfkmsmain::AuthServerSocketThreadStart(TObject *Sender,
: TServerClientThread *Thread)
: {
: //Thread Object, Socket handle Insert
: ThreadObjectCombo->Items->AddObject(IntToStr(Thread->ThreadID), Thread);
: Label1->Caption = IntToStr(StrToInt(Label1->Caption) + 1);
: }
:
: //---------------------------------------------------------------------------
: //Socket Thread End Event
: //---------------------------------------------------------------------------
: void __fastcall Tfkmsmain::AuthServerSocketThreadEnd(TObject *Sender,
: TServerClientThread *Thread)
: {
: Thread->Terminate();
:
: //Close Socket Handle Search
: int nObjectIndex = ThreadObjectCombo->Items->IndexOf(IntToStr(Thread->ThreadID));
: if (nObjectIndex >= 0) ThreadObjectCombo->Items->Delete(nObjectIndex);
: Label2->Caption = IntToStr(StrToInt(Label2->Caption) + 2);
: }
:
: void __fastcall TServerSocketThread::ClientExecute(void)
: {
: TWinSocketStream *pStream = 0;
: BYTE bReceiveBuffer[65536] = {0x00,},
: bSendBuffer[65536] = {0x00,};
: long nReceiveBufferLen = 0,
: nSendBufferLen = 0;
: long nWaitingTime = 0;
: SYSTEM_WORK_INFO tSystemWorkInfo;
: int nWaitTimeOut = 60000; //ms
: bool bFirstMsgRcv = true;
:
: //Socket Handle save
: nSocketHandle = ClientSocket->SocketHandle;
:
: //System Work Information Structure Reset
: memset((char*)&tSystemWorkInfo, 0x00, sizeof(SYSTEM_WORK_INFO));
:
: try
: {
: //TWinSocketStream is a stream that provides services which allow
: //applications to read from or write to socket connections.
: pStream = new TWinSocketStream(ClientSocket, 60000);
:
: //Waiting condition : not Time Out, not Terminate, not Socket Disconnect
: while(!Terminated && ClientSocket->Connected) //nWaitingTime < nSocketTimeOut &&
: {
: //Waits up to TimeOut milliseconds for the socket connection to be ready to transfer data.
: //Application->ProcessMessages();
: if(pStream->WaitForData(nWaitTimeOut))
: {
: //
: //Thread Blocking mode then read(void*,int) function use
: //
: //Reads up to Count bytes from the socket connection into Buffer.
: if (Terminated || !ClientSocket->Connected) break;
:
: try
: {
: //Read Buffer
: memset(bReceiveBuffer, 0, sizeof(bReceiveBuffer));
: nReceiveBufferLen = pStream->Read(bReceiveBuffer, sizeof(bReceiveBuffer));
: }//try
: catch (Exception &E)
: {
: HandleException();
: }
: if (nReceiveBufferLen == 0) break;
: else
: {
: //CHIP Process Request
: nWaitingTime = 0;
: memset(bSendBuffer, 0x00, sizeof(bSendBuffer));
: memset(tSystemWorkInfo.cWorkResult, 0x00, sizeof(tSystemWorkInfo.cWorkResult));
: epassclass->InferfaceMain(&tSystemWorkInfo, bReceiveBuffer, nReceiveBufferLen, bSendBuffer, &nSendBufferLen);
:
: //Client Socket Send Buffer
: pStream->Write(bSendBuffer, nSendBufferLen);
: }
: }
:
: //Delay time
: Sleep(10);
: }
:
: //Client Socket Close
: ClientSocket->Close();
:
: //Stream free
: delete pStream;
: }
: //Excetion process
: __except(EXCEPTION_EXECUTE_HANDLER)
: {
: //Client Socket Disconnect
: ClientSocket->Close();
:
: //Stream free
: delete pStream;
: }
: }
|