[RESOLVED] Event for a server waiting to recieve
Hello,
I have created a simple server application. It listens on a port for the client to send.
However, I have implemented a loop, so that the server will always be listening.
However, is there a better way like using a event, so when a message is received it will fire the event. Instead of using a while loop.
Many thanks,
Here is my code:
Code:
try
{
Poco::Net::SocketAddress addr("PiyaLim", 6000);
Poco::Net::DatagramSocket sock;
sock.bind(addr);
while(true)
{
char buffer[256] = {0};
sock.receiveFrom(buffer,sizeof(buffer), addr);
std::string message = buffer;
int strLength = 0;
strLength = message.length();
message = message.substr(0,strLength);
std::cout << "*********************** START ***********************************************" << std::endl;
std::cout << "The message is: " << message << " The length of the string is: " << strLength << std::endl;
std::cout << "Address: " << addr.host().toString() << std::endl;
std::cout << "Port Number: " << addr.port() << std::endl;
std::cout << "*********************** END *************************************************" << std::endl;
}
}
catch(Poco::Exception ex)
{
std::cout << ex.message();
std::cin.get();
}
Re: Event for a server waiting to recieve
No. For the server to be listening for incomming connections, it must be implemented within some infinite loop so what you have is the only way to do this.
Re: Event for a server waiting to recieve
What you are talking about is event-driven communication that could replace the polling action you discribe. Some instant messaging services use event driven communication. I'm currently looking in to this. Maybe you've heard about Jabber? an open source XML based messaging system that uses this method of communication. I think this could be integrated in many fields of programming.