Click to See Complete Forum and Search --> : control thread


Nostradamus
May 4th, 2005, 10:04 PM
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 ???)

MikeAThon
May 5th, 2005, 02:28 PM
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

pzavolinsky
June 8th, 2005, 12:12 PM
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.