• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

What to use for incoming requests of clients in a mulit -client chat application

Hello Friends

   I am working on an instant messeneger which has to incorporate text,audio and video chat all in one.

Presently i am working on multi-client text chat

Can somebody suggest me what shall i use for new incoming

client connection requests . I mean,do i need to allocate 1 socket for each of the connecting clients(i:e do we need to make an array of sockets)or do we  have some other efficinet method .. I think that threads may solve my problem ... But i haven't uesd threads in MFC before .

  kindly help me out as my work has got stuck at this point


waiting for a prompt reply

  india_bb
0
india_bb
Asked:
india_bb
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Roshan DavisCommented:
// THIS IS A SKELTON STRUCTURE FOR A MESSENGER SERVER
//


DWORD WINAPI ClientThread(LPVOID lpParam);
DWORD WINAPI ConnectionThread(LPVOID lpParam);

VOID CMainFrame::StartupServer()
{
     HANDLE     hThread;
     DWORD     dwThreadId;

     hThread = CreateThread(NULL, 0, ConnectionThread,
                                 (LPVOID)this, 0, &dwThreadId);

    if ( NULL == hThread )
    {
        TRACE("CreateThread() failed: %d\n", GetLastError());
    }
}

DWORD WINAPI ConnectionThread(LPVOID lpParam)
{
     CMainFrame      *pMainFrame     = (CMainFrame *)lpParam;
     ONLINE_USERS *psOnlineUsers = pMainFrame->GetOnlineUsers();
     
     struct sockaddr_in     client;

     SOCKET                    sClient;
    INT                         nAddrSize;
     HANDLE                    hThread;
     DWORD                    dwThreadId;
   
     listen(psOnlineUsers->sListen, 8);

    while( TRUE )
    {
        nAddrSize = sizeof(client);
        sClient = accept(psOnlineUsers->sListen, (struct sockaddr *)&client,
                        &nAddrSize);        

        if ( INVALID_SOCKET == sClient )
        {        
            TRACE("accept() failed: %d\n", WSAGetLastError());
            break;
        }

        TRACE("Accepted client: %s:%d\n",
            inet_ntoa(client.sin_addr), ntohs(client.sin_port));

          psOnlineUsers->sClient = sClient;

        hThread = CreateThread(NULL, 0, ClientThread,
                                      (LPVOID)pMainFrame, 0, &dwThreadId);

        if ( NULL == hThread )
        {
            TRACE("CreateThread() failed: %d\n", GetLastError());
            break;
        }

        CloseHandle(hThread);
    }
    closesocket(psOnlineUsers->sListen);
   
    WSACleanup();

     return 0;
}


DWORD WINAPI ClientThread(LPVOID lpParam)
{
    SOCKET        sock = (SOCKET)lpParam;
     CMainFrame      *pMainFrame     = (CMainFrame *)lpParam;
     ONLINE_USERS *psOnlineUsers = pMainFrame->GetOnlineUsers();
    char          szBuff[DEFAULT_BUFFER];
    int           nRet,
                  nLeft;
     CString            strMsg;
     CString            strError("");
     SOCKET            sClient          = psOnlineUsers->sClient;

     CONNECTED_USERINFO sConnectedUserInfo;

    while( TRUE )
    {
        nRet = recv(sClient, szBuff, DEFAULT_BUFFER, 0);

        if ( 0 == nRet )
            break;
        else if ( SOCKET_ERROR == nRet )
        {
            TRACE("recv() failed: %d\n", WSAGetLastError());
            break;
        }

          // PROCESS THE DATA
         
    }
    return 0;
}



/* GOOD LUCK*/
0
 
jhanceCommented:
0
 
ambienceCommented:
a full blown server would not create a thread for each connection, think about some 100 users connected to one chat server (100 is quite modest), how many threads do you have then ? 100 ? your system is going to choke with few more users.

I say you use a thread pool and IoCompletion Ports, so your threads get used optimally, no use to have a thread for a client that is not talking much. There are samples that caould get you started with highly scalable servers.

check out  http://tangentsoft.net/wskfaq/
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!

 
india_bbAuthor Commented:
hello ambience

  Thanx for the link which u sent

 do you have some other links which can deal with MFC socket programming and can be of help ot me
  thanx
india_bb
0
 
ambienceCommented:
At the moment i donot have any MFC specific links, but i guess it wont be much difficult to translate the code into MFC.

However i do think it is wiser to not use CAsyncSocket or CSocket .. the choice is yours
0
 
india_bbAuthor Commented:
hello ambience

    If u say that i shouldn't use CSocket then tell me what other options do i have
Do u suggest that i should work on SDK's

anyways thanks for quick response

 india_bb
0
 
jhanceCommented:
I certainly did not intend for you to accept my posting about your lack of question follow through as an answer to this...

I think you should ask the moderator to give the points to roshmon.
0
 
india_bbAuthor Commented:
Kindly award the points to roshmon
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.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now