Trying to make a console chat system on which to test my networking skills. I'm going with TCP for now. The chat system isn't much of a problem. The only things I'm having trouble with is server sending data to clients and shutting down the server/client.
The server checks for incomming connection and incoming packeges, like in the tutorial. The client sends packeges to the server.
This is a function with which the chat system gets messages over the internet:
while ( m_Open ) {
nw::Network::packetVec Packets = nw::NetworkAccessor::GetNetwork()->ReceiveData();
for ( nw::Network::packetVec::iterator it = Packets.begin(), itEnd = Packets.end(); it != itEnd; ++it ) {
std::string Message;
(*it) >> Message;
m_Mutex.Lock();
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Coords);
std::cout << " ";
etConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Coords);
std::cout << Message;
m_Mutex.Unlock();
if ( Coords.Y < 31 )
++Coords.Y;
else
Coords.Y = 11;
}
}
Packets is a vector of sf::Packet (in case you are the server and receive several packeges a time).
Here's the Receive function for client:
nw::Network::packetVec nw::Client::ReceiveData() {
nw::Network::packetVec Packets;
sf::Packet Packet;
if ( m_Socket.Receive(Packet) == sf::Socket::Done ) {
Packets.push_back(Packet);
}
else {
std::string Message = "An error occured.";
Packet << Message;
Packets.push_back(Packet);
}
return Packets;
}
There's only one packet because there's only one socket. (One packet at a time).
The Receive function is the function that receives packets - calling the Wait function on the selector - if server, or calling the receive function on the socket - if client (both are blocking so it's the same). And as soon as a packege (or more if you are server) arrive, it will receive it and continue with the while (m_Open) loop.
The problem is that this loop only loops if there is an incoming packege, which is fine because I don't want it to loop like crazy if there's nothing to process. But now the program only closes if - after I hit Q for close - someone sends a packet to me. Which is stupid. (I know, I did it this way.)
So I was wondering if I'm client, doing "Socket.Close()" will close the socket and the Receive funtion ends so the loop continues with Packets size 0 and break? Or that won't help at all, maybe even breaking stuff?
As for server side - clearing the selector will make the Wait function return? Maybe even closing the listening socket...
All this done with threads.
And the last part.
All the clients that are connected to the server have a socket in the selector. How can I use them to send from the server to the clients? I can't acces them in the selector. Or I need an additional socket to connect to the client, doing some sort of server-server stuff?
Ok, I think the post is long enough. Let me know if I know something wrong, or doing wrong, or just wrong.
And sorry for my bad English...
And sorry if this is already posted somewhere else. I haven't found anything specific, or I'm bad at searching.
Thanks for reading.