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   |
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
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Wed, 26 September 2018 22:47] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Mon Jun 09 14:17:57 CEST 2025
Total time taken to generate the page: 0.04159 seconds
|