Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Adding a socket to a GUI application
Re: Adding a socket to a GUI application [message #50334 is a reply to message #50333] Wed, 26 September 2018 22:03 Go to previous messageGo to previous message
Oblivion is currently offline  Oblivion
Messages: 1210
Registered: August 2007
Senior Contributor
Hello Giorgio,

I'm afraid (as it'll make things somewhat complicated) what you seem to need is a socket in non-blocking mode.
Yet, there might be a simple solution for the test code you've provided:

void tagidSocket()
{
	TcpSocket server;
	if(!server.Listen(23456, 5)) {
		RLOG("Unable to initialize server socket");
		return;
	}
	RLOG("Socket started, waiting for requests...");
	try {
		while(!Thread::IsShutdownThreads()) {
			TcpSocket s;
			s.WhenWait = [=]
			{
				if(Thread::IsShutdownThreads())
					throw Exc("Thread is shut down.");
			};
			if(s.Accept(server)) {
				String w = s.GetLine();
				RLOG("Request: " + w + " from: " + s.GetPeerAddr() + '\n');
				if(w == "time")
					s.Put(AsString(GetSysTime()));
				else
					s.Put(AsString(3 * atoi(~w)));
				s.Put("\n");
				
			}
		}
	}
	catch(const Exc& e) {
		RLOG(e);
	}
}


Now, the above code should work. But I can't guarantee it will continue to work in a complex code. That's why you need to get yourself familiar with non-blocking operations.

Best regards,
Oblivion


[Updated on: Wed, 26 September 2018 22:47]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: print the Http Request before sending it
Next Topic: I don't understand Moveable
Goto Forum:
  


Current Time: Mon Jun 09 14:17:57 CEST 2025

Total time taken to generate the page: 0.04159 seconds