Solved

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

Posted on 2002-05-27
10
193 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process filename extension 3 172
windows 10 how make picture as desktop background 2 58
difference between String.subString() and String.subSequence() 6 177
NotAlone Challenge 20 80
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…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

21 Experts available now in Live!

Get 1:1 Help Now