Threads communcation in network programming

Q

QQ

Hello I am a beginner for network programming.
Now I am trying to implement some function like this

I am keep listening to a port, when I get some message, I will have
some corresponding response. And the same time, I can send out message.

So I put the listening function in the child process.
My question is when I receive some packet, how can I send signal to the
main thread.
Thanks a lot!
 
M

Malcolm

QQ said:
I am keep listening to a port, when I get some message, I will have
some corresponding response. And the same time, I can send out message.

So I put the listening function in the child process.
My question is when I receive some packet, how can I send signal to the
main thread.
You go beyond ANSI C, because there is no signalling / interrupt mechanism.
These are often provided as platform-specific calls.

However ANSI C does provide "volatile". This flags a variable as being able
to be altered by an outsode process, such as your child.

So a simple scheme would be

global variables:

struct packet data;
volatile int semaphore;

/* child */

wait till packet comes.
if semaphore not "clear" you have a problem. Maybe busy idle checking it
until it goes to "clear".
set semaphore to busy.
write data to global structure
set semaphore to "data ready"
wait till next packet comes.

/* main process */
every so often, check semaphore.
if it is set to "data ready" set semaphore to "busy", other wise go back to
main loop
read data into own structure
set semaphore to clear.
go back to main loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top