Solved

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

Posted on 2003-11-03
5
275 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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…
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…

760 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

17 Experts available now in Live!

Get 1:1 Help Now