I'm trying to create a Chat program for people to talk to each other through. So far I have it so multiple clients can connect to the server and send/receive messages to/from the server. The problem I'm having is because of the way Clients that are connected to the server are stored I can't figure out how to send text to more then just one client at a time....
This is the part of my server that finds clients connecting and stores them.
// |-----------------------| //
// | Check for Connections | //
// |-----------------------| //
while ( true ){
SOCKET hClientSocket ;
struct sockaddr_in clientAddr ;
int nSize = sizeof( clientAddr ) ;
hClientSocket = accept( hServerSocket, ( struct sockaddr *) &clientAddr, &nSize ) ;
if ( hClientSocket == INVALID_SOCKET ){
cout << "accept( ) failed" << endl ;
} else {
HANDLE hClientThread ;
struct CLIENT_INFO clientInfo ;
DWORD dwThreadId ;
clientInfo . clientAddr = clientAddr ;
clientInfo . hClientSocket = hClientSocket ;
const char* ConnectionIp;
ConnectionIp = inet_ntoa( clientAddr . sin_addr );
char dateStr [9];
char timeStr [9];
_strdate( dateStr);
_strtime( timeStr );
cout << ( "%s", dateStr) << " [" << ("%s", timeStr) << "] - (" << inet_ntoa( clientAddr . sin_addr ) << ") has logged in! " << endl ;
SaveLog(ConnectionIp, 1);
// Start the client thread
hClientThread = CreateThread( NULL, 0,
( LPTHREAD_START_ROUTINE ) ClientThread,
( LPVOID ) &clientInfo, 0, &dwThreadId ) ;
if ( hClientThread == NULL ){
cout << "Unable to create client thread" << endl ;
} else {
CloseHandle( hClientThread ) ;
}
}
This checks for clients connecting and if the client can connect it sets the client info and creates hClientSocket for that client and creates the thread. It also then displays a message on the server console indicating that the client has connected.
This next section creates the Client's thread and checks for incoming messages from the client. If there is one it displays the message to the sever and the message is stored as szClientMsg. It also checks if the Client entered "quit" and if so it disconnects the client and lets the server know. Near the very end of this is the line that sends the message received in szClientMsg back to the client. I need this message to be sent to everyone and not just the one who originally sent the message.
BOOL WINAPI ClientThread( LPVOID lpData ){
CLIENT_INFO *pClientInfo = ( CLIENT_INFO * ) lpData ;
char szClientMsg[ 9999 ] ;
int nLength ;
const char* PlayerIp;
PlayerIp = inet_ntoa( pClientInfo -> clientAddr . sin_addr );
while ( 1 ){
nLength = recv( pClientInfo -> hClientSocket, szClientMsg, sizeof( szClientMsg ), 0 ) ;
if ( nLength > 0 ){
szClientMsg[ nLength ] = '\0' ;
cout << "(" << inet_ntoa( pClientInfo -> clientAddr . sin_addr ) << ") - " << szClientMsg << endl ;
SaveMsg(PlayerIp, szClientMsg);
// Send string back, if its not QUIT
if ( strcmp( szClientMsg, "QUIT" ) == 0 || strcmp( szClientMsg, "quit" ) == 0 ){
char dateStr [9];
char timeStr [9];
_strdate( dateStr);
_strtime( timeStr );
cout << ( "%s", dateStr) << " [" << ("%s", timeStr) << "] - (" << inet_ntoa( pClientInfo -> clientAddr . sin_addr ) << ") has logged out! " << endl; // Player logged out
SaveLog(PlayerIp, 2);
closesocket( pClientInfo -> hClientSocket ) ;
return TRUE ;
}
// try sending the data in multiple requests
int nCntSend = 0 ;
char *pClientMsg = szClientMsg ;
while ( ( nCntSend = send( pClientInfo -> hClientSocket, pClientMsg, nLength, 0 ) != nLength ) ){
if ( nCntSend == -1 ){
cout << "Error sending the data to " << inet_ntoa( pClientInfo -> clientAddr . sin_addr ) << endl ;
break ;
}
if ( nCntSend == nLength )
break ;
pClientMsg += nCntSend ;
nLength -= nCntSend ;
}
// Send data to client
send(pClientInfo -> hClientSocket, szClientMsg, 999, 0);
} else {
cout << "Error reading the data from " << inet_ntoa( pClientInfo -> clientAddr . sin_addr ) << endl ;
}
}
return TRUE ;
}
The problem I'm having is since this creates a thread/socket for each Client as they connect I can't figure out how to send a message to everyone connected at once. I know I have to change the part of the send() code that is
pClientInfo -> hClientSocket
since that is setting it to just one Client, I just don't know what I need to change it to so it works properly.
Thx in advance for any help and if you need more information just tell me.