Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2002-05-27
10
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 

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 300 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 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