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
Solved

..:: CMap to take a pointer to a socket and a CString ::..

Posted on 2003-11-03
5
278 Views
Last Modified: 2013-11-20
Hi,

I am trying to use a CMap or a CTypedPtrList or any other suggestion,
to construct a list of open socket connections and associated with it a username of the user connected at that socket.

so when a request comes in to talk to 'bob' bob is looked up in the list found the socket it it connected to and starts a chat session.

what i have done so far only works for a socket list, I cant seem to associate a username to it as well so the above functionality does not
yet work.

any help please, code snippets pls.

many thanks
0
Comment
Question by:nikon70
  • 3
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
martynjpearson earned 40 total points
ID: 9670279
You could use CMapStringToOb, which maps a string to an MFC object derived from CObject (which CAsyncSocket and CSocket are). Alternatively, you could use CMapStringToPtr which maps strings to pointers.

So, you could do something like this :

CMapStringToOb mapConnections;
CString strName;
CAsyncSocket * pSocket; // Set this accordingly

strName = "Bob";

mapConnection.SetAt(strName, pSocket);

Then to get it back again

if (mapConnections.Lookup(strName, (CObject &*)pSocket))
{
   // pSocket now points to to the socket associated with the name in strName
}

Hope this helps
Martyn
0
 

Author Comment

by:nikon70
ID: 9670449
Hi,

Thanks got your code working, I think this is what i need.

but a question, this sets up a name 'bob' initally, can this be changed as i want to be able for the user to change the nickname.

Cheers
0
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9670598
Sure, just set strName to the appropriate username before you do SetAt() or Lookup(). This could come from a dialog box, some setting or indeed from data sent to via your sockets.

All the best
Martyn

0
 

Author Comment

by:nikon70
ID: 9670683
Cheers really helpful code.

but the way i have set my accept to work:

void CSockServerDlg::OnAccept()
{
      MessageBox("Now accepting...", "Accepting", MB_OK);
      CConnUser *user = new CConnUser();
      CConnSocket *socket = new CConnSocket();
      user->setSocket(socket);
      socket->SetParent(this);

      m_ConnUserList.AddTail(user);
      m_sListenSocket.Accept(*socket);

      /////////////

      strName = "Bob";
      mapConnections.SetAt(strName, socket);

      if (mapConnections.Lookup(strName, (CObject *&)socket))
            {
                  socket->Send(strName, strName.GetLength());
            }

      /////////////

}

It sets the nickname before any data has been sent through. hence wanted to change the nickname in the map after its been set.
0
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9671296
If the nickname is coming over the socket, you could do something like this :

Set nickname to some unique value and place in the map
Send a message to the socket, requesting the username, passing the unique value as a parameter
The other party then responds, sending the unique value and the actual username
When the message is received lookup the socket via the unique value, and then remove the entry, adding a new entry for the socket and the correct username

Hope this helps
Martyn
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.

856 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