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












SourceForge.net Logo



Home » U++ Library support » U++ MT-multithreading and servers » How to create sockets that don't block the GUI?
How to create sockets that don't block the GUI? [message #38225] Fri, 07 December 2012 13:38 Go to next message
lectus is currently offline  lectus
Messages: 327
Registered: September 2006
Location: Brazil
Senior Member
So far I was able to communicate between sockets, but my problem is that when there's intensive processing the GUI locks and I can't interact with it.

I'd like to have a socket in a while(1) loop while having the GUI fully functional.

Any ideas?
Re: How to create sockets that don't block the GUI? [message #38226 is a reply to message #38225] Fri, 07 December 2012 13:48 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 250
Registered: July 2009
Experienced Member
Move all communication to second thread.
Re: How to create sockets that don't block the GUI? [message #38227 is a reply to message #38226] Fri, 07 December 2012 13:53 Go to previous messageGo to next message
lectus is currently offline  lectus
Messages: 327
Registered: September 2006
Location: Brazil
Senior Member
That's what I'm doing but it requires a GuiLock to be able to update the GUI inside the thread, then the GUI locks.

If I don't use the GuiLock the application crashes.

[Updated on: Fri, 07 December 2012 13:54]

Report message to a moderator

Re: How to create sockets that don't block the GUI? [message #38228 is a reply to message #38227] Fri, 07 December 2012 14:06 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 250
Registered: July 2009
Experienced Member
Try to use PostCallback, to pass information to GUI from communication thread and of course avoid executing callbacks to often (>50Hz).
Re: How to create sockets that don't block the GUI? [message #38229 is a reply to message #38225] Fri, 07 December 2012 14:06 Go to previous messageGo to next message
lectus is currently offline  lectus
Messages: 327
Registered: September 2006
Location: Brazil
Senior Member
I think I got it to work.

I need this line:
GuiLock __;


ONLY right before the GUI gets updated, otherwise my GUI locks the whole time.
Re: How to create sockets that don't block the GUI? [message #38230 is a reply to message #38229] Fri, 07 December 2012 15:01 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 612
Registered: January 2010
Location: U.S. California. Mojave &...
Veteran
Also Zbych is correct that the transfer of data should be limited for transfer per second. Put a Sleep(10); between each transfer if necessary, slower computers take more time.

Remember each transfer is a line ending with \0, I transfer 50 'lines' at a time each second without a problem.
It's the make/break of the connection that takes time as the actual transfer is very fast.

The socket is used to transfer data from a C++ app to a Java GUI app with less that 5% CPU usage.

This may not directly relate to your GUI lock but the concepts may help.
Re: How to create sockets that don't block the GUI? [message #38231 is a reply to message #38230] Fri, 07 December 2012 16:00 Go to previous messageGo to next message
lectus is currently offline  lectus
Messages: 327
Registered: September 2006
Location: Brazil
Senior Member
nlneilson wrote on Fri, 07 December 2012 09:01

Also Zbych is correct that the transfer of data should be limited for transfer per second. Put a Sleep(10); between each transfer if necessary, slower computers take more time.

Remember each transfer is a line ending with \0, I transfer 50 'lines' at a time each second without a problem.
It's the make/break of the connection that takes time as the actual transfer is very fast.

The socket is used to transfer data from a C++ app to a Java GUI app with less that 5% CPU usage.

This may not directly relate to your GUI lock but the concepts may help.


Yes, every detail is great help. Thanks.

What do you mean that line ending is \0. Isn't it \n?
Re: How to create sockets that don't block the GUI? [message #38234 is a reply to message #38231] Fri, 07 December 2012 17:30 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 612
Registered: January 2010
Location: U.S. California. Mojave &...
Veteran
Actually what is being sent is a string of char not a line but it is often referred to as "sending a line".
A String has \0 at the end, NUL terminated.
Note in my previous post I have 'lines' as they were separate lines before adding all the char to the buf to be sent/received.

I have a char buf of 3000
I just add the char and only at the end is there \0

Sometime the only thing sent from the client is "+" which is actually '+' \0 or 43 0 or 0x2B 0x0
The server returns the latitude and longitude of the center in the Java app.

For tracking up to 50 objects with lat, lon, alt, ID, etc then all that data for all 50 is added to the char buf then ended with \0.
On the server the \0 means just that, the end.
So if the buf contains 2000 char and then just + \0 is sent the rest of the buf is ignored and not even sent as \0 means THE END.

A better definition would be a C string.
http://stackoverflow.com/questions/10943033/why-are-strings- in-c-usually-terminated-with-0

(Line feed, '\n', 0x0A, 10 in decimal)
So '\n' is just ch = 10;
When that is sent through a socket that is all it is.
How it is dealt with on receiving can be handled.

NULL is same as '\0' which is same as 0x0 or 0

[Updated on: Fri, 07 December 2012 17:53]

Report message to a moderator

Re: How to create sockets that don't block the GUI? [message #38241 is a reply to message #38225] Fri, 07 December 2012 19:28 Go to previous messageGo to next message
lectus is currently offline  lectus
Messages: 327
Registered: September 2006
Location: Brazil
Senior Member
I'm confused. The documentation says this:

String GetLine(int maxlen = 65536)
Reads single line (ended with '\n', '\r' is ignored). If the whole line cannot be read within timeout or line length is longer than maxlen sets error and returns String::GetVoid().
Re: How to create sockets that don't block the GUI? [message #38441 is a reply to message #38241] Sat, 15 December 2012 18:40 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 612
Registered: January 2010
Location: U.S. California. Mojave &...
Veteran
What is being sent through a socket is a bunch of character ending with \0.

After it is received it can be parsed into separate lines with \n

Do all of the addition of characters before passing the data to the client to be sent and on the server just pass the whole buffer out to be parsed.

For small amounts of data it can be added and parsed in the client and server.

The \n or \r are just like any other character as far as what is in the buffer or packet sent. If the actual packet size limit is 65536 then there could be many \n or whatever.
The only character that has real significance is \0 which indicates the END of the packet.

Parse what is in the packet outside the server code unless the amount of data is small.

[Updated on: Sat, 15 December 2012 18:50]

Report message to a moderator

Re: How to create sockets that don't block the GUI? [message #39480 is a reply to message #38225] Wed, 20 March 2013 21:28 Go to previous messageGo to next message
Alexander_Ag is currently offline  Alexander_Ag
Messages: 4
Registered: March 2013
Location: Georgia
Junior Member
lectus wrote on Fri, 07 December 2012 16:38

So far I was able to communicate between sockets, but my problem is that when there's intensive processing the GUI locks and I can't interact with it.

I'd like to have a socket in a while(1) loop while having the GUI fully functional.

Any ideas?


Very interesting topic - can anyone give a sample code with GUI that use TcpSocket as server, i just begin work around sockets.
For example http://www.ultimatepp.org/reference$SocketServer$en-us.html but with GUI.

[Updated on: Wed, 20 March 2013 21:33]

Report message to a moderator

Re: How to create sockets that don't block the GUI? [message #39482 is a reply to message #39480] Thu, 21 March 2013 06:57 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1730
Registered: August 2008
Location: Czech Republic
Senior Veteran

Alexander_Ag wrote on Wed, 20 March 2013 21:28

lectus wrote on Fri, 07 December 2012 16:38

So far I was able to communicate between sockets, but my problem is that when there's intensive processing the GUI locks and I can't interact with it.

I'd like to have a socket in a while(1) loop while having the GUI fully functional.

Any ideas?


Very interesting topic - can anyone give a sample code with GUI that use TcpSocket as server, i just begin work around sockets.
For example http://www.ultimatepp.org/reference$SocketServer$en-us.html but with GUI.

It works the same way as any other time consuming process. You just have to make sure that you call Ctrl::ProcessEvents() from time to time to update the GUI. In case of sockets you might want to lower the timeout, so the loop executes faster and call ProcessEvents in each iteration.

Alternatively, use two separate threads...

Best regards,
Honza

Re: How to create sockets that don't block the GUI? [message #42080 is a reply to message #39480] Mon, 17 February 2014 09:51 Go to previous messageGo to next message
jlh67 is currently offline  jlh67
Messages: 4
Registered: February 2014
Junior Member
look my code ...
Re: How to create sockets that don't block the GUI? [message #42081 is a reply to message #42080] Mon, 17 February 2014 17:31 Go to previous messageGo to next message
ManfredHerr is currently offline  ManfredHerr
Messages: 67
Registered: February 2013
Location: Germany
Member
I would like to advise rethinking the concept with regard to the client server model. Normally, a server serves requests from a number of clients and the clients are there for GUI not the server. OK?
Re: How to create sockets that don't block the GUI? [message #42082 is a reply to message #42081] Mon, 17 February 2014 17:52 Go to previous messageGo to next message
jlh67 is currently offline  jlh67
Messages: 4
Registered: February 2014
Junior Member
ManfredHerr wrote on Mon, 17 February 2014 17:31

I would like to advise rethinking the concept with regard to the client server model. Normally, a server serves requests from a number of clients and the clients are there for GUI not the server. OK?


I agree with you.But the TCP protocol can be used to transmit data to a GUI application. In this case, we are not talking client-server model: It is a simple transmission of data via the TCP protocol.

excuse me for my english.
Re: How to create sockets that don't block the GUI? [message #42085 is a reply to message #42082] Tue, 18 February 2014 12:00 Go to previous messageGo to next message
ManfredHerr is currently offline  ManfredHerr
Messages: 67
Registered: February 2013
Location: Germany
Member
If we´re not talking client server model then why do you try to use the server concept for data transfer? Sockets can be read like a file. If you try to read a huge file in a GUI application then your GUI is inresponsive as long as you read the file. For your intensive data stream the same happens. As long as data is coming in the process is busy; too busy to process User Input. One option is to control the data stream by something like XON/XOFF. So you can handle the User Input with equal or even higher priority than data transfer.
Re: How to create sockets that don't block the GUI? [message #42093 is a reply to message #42085] Tue, 18 February 2014 21:19 Go to previous messageGo to next message
jlh67 is currently offline  jlh67
Messages: 4
Registered: February 2014
Junior Member
Correct! But if the data stream is low, there is no problem. it's a simple solution without flow control.

[Updated on: Tue, 18 February 2014 21:23]

Report message to a moderator

Re: How to create sockets that don't block the GUI? [message #42094 is a reply to message #42093] Tue, 18 February 2014 22:52 Go to previous messageGo to next message
ManfredHerr is currently offline  ManfredHerr
Messages: 67
Registered: February 2013
Location: Germany
Member
So, what is the problem then? Confused
Re: How to create sockets that don't block the GUI? [message #42103 is a reply to message #42094] Wed, 19 February 2014 18:07 Go to previous messageGo to next message
jlh67 is currently offline  jlh67
Messages: 4
Registered: February 2014
Junior Member
ManfredHerr wrote on Tue, 18 February 2014 22:52

So, what is the problem then? Confused


There is no problem! I just gave my solution to the question
"How to create sockets that don't block the GUI?"
[message #39480]

[Updated on: Wed, 19 February 2014 18:14]

Report message to a moderator

Re: How to create sockets that don't block the GUI? [message #47141 is a reply to message #38225] Tue, 20 December 2016 13:02 Go to previous message
MrSarup
Messages: 30
Registered: December 2016
Member
Hello!

Well, I am working on a Server<--->Client model. I have created a new package and saved the codes published above in cpp, h and lay files. I used the nightly build available today. It works fine.

I wish the Ultimate Dev. team could have implemented more examples of TCPServer, in particular on Server/Client with GUI.

[Updated on: Tue, 20 December 2016 20:31]

Report message to a moderator

Previous Topic: WebSockets non blocking mode?
Next Topic: SKYLARK js: I offer that we expanded the UxGet function a little with parameter callback
Goto Forum:
  


Current Time: Thu Mar 23 01:20:16 CET 2017

Total time taken to generate the page: 0.03357 seconds