Results 1 to 3 of 3

Thread: [RESOLVED] Event for a server waiting to recieve

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved [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();
    	}
    steve

  2. #2
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    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.

  3. #3
    New Member
    Join Date
    Oct 2007
    Location
    The Netherlands, Maastricht
    Posts
    12

    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.

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