Solved

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

Posted on 2002-05-27
10
194 Views
Last Modified: 2013-11-20
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
Comment
Question by:india_bb
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7038384
// 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
 
LVL 32

Expert Comment

by:jhance
ID: 7038910
0
 
LVL 22

Expert Comment

by:ambience
ID: 7040970
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:india_bb
ID: 7048771
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
 
LVL 32

Expert Comment

by:jhance
ID: 7048876
0
 
LVL 22

Expert Comment

by:ambience
ID: 7053292
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
 

Author Comment

by:india_bb
ID: 7054144
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
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 7054159
0
 
LVL 32

Expert Comment

by:jhance
ID: 7060350
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
 

Author Comment

by:india_bb
ID: 7107126
Kindly award the points to roshmon
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

778 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