Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Host to IP 7 78
tripleUp challenge 7 78
ShiftLeft challenge 21 80
Bartender label printing - switch on and off graphics 3 38
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…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 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