OpenQNX :: The QNX Community Portal

May 13, 2008 - 12:11 PM
Google
  Web openqnx.com   
     Create an account Home · Submit News · QNX Forums · QNX Download · Search   
_
Main Menu
Who's Online
There are 55 unlogged users and 1 registered user online.

You can log-in or register for a user account here.

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
invoker
20 Post subject: General Programming Problem  PostPosted: May 07, 2008 - 07:48 AM
Active Member


Joined: Apr 15, 2008
Posts: 19

HELP!

In short I'm doing a project which at the end will look like this:

I need to write an application which would have a following functionality:
-sending messages via tcp
-receiving messages via tcp (server)
-GUI in photon

What I dont know is how to put sending and receiving in a thread
(or in a different process ) so that it will not block the GUI.

I have tried threads but with no luck - my visual studio and C# experience
I no god - everything seems to work in a different way.


what approach should I undertake?
 
 View user's profile Send private message  
Reply with quote Back to top
MasterFX
Post subject: RE: General Programming Problem  PostPosted: May 07, 2008 - 08:15 AM
Active Member


Joined: Sep 28, 2007
Posts: 24

Just download netio. I compiled it for qnx and it works great. The Sourcecode is very simple to understand so you can get an inspiration from it.
 
 View user's profile Send private message  
Reply with quote Back to top
invoker
Post subject: Re: RE: General Programming Problem  PostPosted: May 07, 2008 - 08:53 AM
Active Member


Joined: Apr 15, 2008
Posts: 19

MasterFX wrote:
Just download netio. I compiled it for qnx and it works great. The Sourcecode is very simple to understand so you can get an inspiration from it.

thx - I will explore the code. Smile

I just am a little bit worried about integrating it with photon :/
Could you give me an advice how to integrate it? Smile
 
 View user's profile Send private message  
Reply with quote Back to top
invoker
Post subject: Re: RE: General Programming Problem  PostPosted: May 07, 2008 - 10:27 AM
Active Member


Joined: Apr 15, 2008
Posts: 19

MasterFX wrote:
Just download netio. I compiled it for qnx and it works great. The Sourcecode is very simple to understand so you can get an inspiration from it.


I'm going through the code but It seems that it uses
os2.h and os2def.h
I found a page where you can download all the header files on
the GPL license but some of them are missing like os2pm-mt.h and
os2pm-ed.h

:/

I'm using QNX Momentics IDE Version: 4.0.1.
 
 View user's profile Send private message  
Reply with quote Back to top
micro
Post subject: RE: Re: RE: General Programming Problem  PostPosted: May 07, 2008 - 01:28 PM
Senior Member


Joined: Jul 22, 2004
Posts: 315

just take a look in the documentation at

select()
socket()
 
 View user's profile Send private message  
Reply with quote Back to top
maschoen
Post subject: RE: Re: RE: General Programming Problem  PostPosted: May 07, 2008 - 03:25 PM
QNX Master


Joined: Jun 25, 2003
Posts: 974

The main structure of a Photon program is a hidden loop in which your application sits in Receive mode waiting for messages. Select is implemented in a similar way, again waiting for messages. In QNX 4, and I expect in QNX 6 there was a way to call select, but have it handle non-select messages also. So in principle it would be possible get access to the Photon loop code, and have it handle a select also. But I would not structure things that way. It's better to say away from that code if you can.

Instead I would create a thread that handles the select, and have it send a message to the main thread. You can set up Photon to start a callback when that message is received. This requires that you know how to do threaded programming to interact with the "select" thread.
 
 View user's profile Send private message Send e-mail Visit poster's website  
Reply with quote Back to top
invoker
Post subject: Re: RE: Re: RE: General Programming Problem  PostPosted: May 07, 2008 - 03:31 PM
Active Member


Joined: Apr 15, 2008
Posts: 19

maschoen wrote:
Instead I would create a thread that handles the select, and have it send a message to the main thread. You can set up Photon to start a callback when that message is received. This requires that you know how to do threaded programming to interact with the "select" thread.


thx for your replay.

I know how to create a thread - let say that it will
do smt.
How to create a call from that thread to the main thread?
Smile
 
 View user's profile Send private message  
Reply with quote Back to top
micro
Post subject: RE: Re: RE: Re: RE: General Programming Problem  PostPosted: May 07, 2008 - 03:42 PM
Senior Member


Joined: Jul 22, 2004
Posts: 315

proxies for example, err, its pulses now ^^
 
 View user's profile Send private message  
Reply with quote Back to top
maschoen
Post subject: RE: Re: RE: Re: RE: General Programming Problem  PostPosted: May 08, 2008 - 04:30 PM
QNX Master


Joined: Jun 25, 2003
Posts: 974

Well, be careful here. You don't want to "call" a thread. You want to get its attention so it will do some work for you. There are a few different approaches. An approach which will work even if you are dealing with different processes is to have the thread receive messages, so you can send it a message to do work for you. It can do this asynchronously, maybe informing you when it is done, or hold you captive until it is done, returning result information.

A different, and more Posix as opposed to QNX approach would be to use mutexes and condvars. This avoids message passing by causing information exchange through memory.

1) Worker thread waits
2) Requester thread moves data into a buffer
3) Requester thread wakes up worker thread
4) Worker thread does work
5) Worker thread posts result and goes back to 1

While the worker is executing, the requester could be waiting or continuing on with other work

The documentation does a fair job of explaining this, but I also highly recommend Robert Kryten's book on QNX 6.
 
 View user's profile Send private message Send e-mail Visit poster's website  
Reply with quote Back to top
invoker
Post subject: RE: Re: RE: Re: RE: General Programming Problem  PostPosted: May 12, 2008 - 11:54 AM
Active Member


Joined: Apr 15, 2008
Posts: 19

I'm trying to find any examples for using select() for checking whether the data are avaliable
for receiving from a socket. Could anyone show me how to do that? Thanks!
 
 View user's profile Send private message  
Reply with quote Back to top
mario
Post subject: RE: Re: RE: Re: RE: General Programming Problem  PostPosted: May 12, 2008 - 01:40 PM
QNX Master


Joined: Sep 01, 2002
Posts: 2667

The doc on select has an example, it's not using file handlers that are sockets but you should get the idea.
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
Tim
Post subject:   PostPosted: May 12, 2008 - 02:07 PM
Senior Member


Joined: Mar 10, 2004
Posts: 512

Invoker,

http://beej.us/guide/bgnet/output/html/multipage/advanced.html

See the Multi-Person chat server example at this link.

Tim
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
invoker
Post subject: RE: Re: RE: Re: RE: General Programming Problem  PostPosted: May 12, 2008 - 02:11 PM
Active Member


Joined: Apr 15, 2008
Posts: 19

When I read the description of select everything seems clear and simple. It check
if something is ready for reading or writing. But I look at other parameters and I have the timeval
which controls the time as I assume ( timeouts). and smt called width - The number of descriptors to check in the given sets. Cool
whats that? I dont really know how to use select for a socket. Sad
 
 View user's profile Send private message  
Reply with quote Back to top
invoker
Post subject:   PostPosted: May 12, 2008 - 02:17 PM
Active Member


Joined: Apr 15, 2008
Posts: 19

Tim wrote:
Invoker,

http://beej.us/guide/bgnet/output/html/multipage/advanced.html

See the Multi-Person chat server example at this link.

Tim



Thx Cool
 
 View user's profile Send private message  
Reply with quote Back to top
invoker
Post subject:   PostPosted: May 13, 2008 - 08:30 AM
Active Member


Joined: Apr 15, 2008
Posts: 19

Well I dont really understand select from that example - I just dont get it how
select() knows which sockets to observe.

But..
After really a long search I found (ibm page) how to put a TCP socket into a non-blocking mode
which solves my problems of locking the GUI when launching commands like
connect (if no host is avaliable) and recv ( when nothing is to recive )
ans it works really good Smile

Here is the code:

char dest_ip1[]="192.168.11.103";
int dest_port1=1666;
struct sockaddr_in sa_in;


int dontblock;
int rc;

memset(&sa_in,0,sizeof(sa_in));
if ( (sfd = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR)
return 0;
sa_in.sin_family = AF_INET;
sa_in.sin_addr.s_addr = inet_addr(dest_ip1);
sa_in.sin_port = htons(dest_port1);

dontblock = 1;
rc = ioctl(sfd, FIONBIO, (char *) &dontblock); //end test
connect(sfd, (struct sockaddr *) &sa_in, sizeof(sa_in)));
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2007 The PNphpBB Group
Credits
All logos and trademarks in this site are property of their respective owners. The comments are property of their posters.
Powered by OpenQNX: The QNX Community Portal Site
QNX and the QNX logo are registered trademarks of QNX Software Systems.