General Programming Problem

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?

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. :slight_smile:

I just am a little bit worried about integrating it with photon :confused:
Could you give me an advice how to integrate it? :slight_smile:

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

:confused:

I’m using QNX Momentics IDE Version: 4.0.1.

just take a look in the documentation at

select()
socket()

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.

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?
:slight_smile:

proxies for example, err, its pulses now ^^

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.

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!

The doc on select has an example, it’s not using file handlers that are sockets but you should get the idea.

Invoker,

beej.us/guide/bgnet/output/html/ … anced.html

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

Tim

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. 8)
whats that? I dont really know how to use select for a socket. :frowning:

Thx 8)

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 :slight_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)));

Invoker,

Glad you got it working.

The ‘select’ example I linked to was a server side of the TCP connection. That’s what I thought you wanted to do from your original post when you mentioned receiving and server.

If I had known you wanted to do the client side I would have linked to a connect example using non-blocking sockets.

Tim

Its a good thing to have as wide knowledge a possible.
I will need to do a server application in future. for now I quite happy
with non-blocking sockets.

so Thank You! :slight_smile: