CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: control thread

  1. #1
    Join Date
    Nov 2004
    Posts
    40

    control thread

    I create a chat program. Whenever client connect to server i create a thread.
    The things is that I want Two client can talk to each other. Just like Yahoo Messenger. A client can choose a person that they want to talk.
    How i get information about a thread is running ( the ip of client that connected ???)

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: control thread

    I don't have a direct answer for you, but you might try searching for samples of source code over at www.sourceforge.net, for a good chat system. "DotChat" seems interesting at http://sourceforge.net/projects/dotchat/

    Mike

  3. #3
    Join Date
    Jun 2005
    Location
    Buenos Aires, Argentina
    Posts
    66

    Re: control thread

    Create a std::map<> to hold userID and user "thread" (actually some struct to hold thread info, socket info and user info)

    This is the client-server approach:

    When recv() function returns on the server program use the destination_userID (that should come as part of the message) in the map to get the correct socket and call send() on that socket to pass forward the message.

    If you prefer a more p2p solution, in this case the "server" program will hold just the IP addresses of the clients (in the map, of course). And on client request, the server should response with the correct client IP (Note that this is NOT the Yahoo Messenger/Normal IM Program way).

    In any case, to answer your questions:
    To get the client IP provided that you have the SOCKET handle or file descriptor use: int getpeername(int s, struct sockaddr *name, socklen_t *namelen); // in windows int s is actually SOCKET s

    To find out if a thread is running:
    In windows use
    BOOL GetExitCodeThread(
    HANDLE hThread, // handle to the thread
    LPDWORD lpExitCode // address to receive termination status
    );

    In linux I guess you'll have to use some bool var and do the hard work yourself.

    One last work of advice, if you use the map DON'T FORGET A MUTEX to avoid simultaneous access or you'll be facing some serious access violation voodoo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured