Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-11-03
5
Medium Priority
?
282 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
[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
  • 3
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
martynjpearson earned 160 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 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