Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-11-03
5
Medium Priority
?
283 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 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

Technology Partners: 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!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Loops Section Overview
Suggested Courses

824 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