Solved

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

Posted on 2002-05-27
10
192 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

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

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

746 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

10 Experts available now in Live!

Get 1:1 Help Now