Re: advices on sockets
- From: mast4as <mast4as@xxxxxxxxx>
- Date: Thu, 13 Mar 2008 14:34:30 -0700 (PDT)
Thank you very much for your answer.
Yes. You could write it as a subroutine in your program, and use fork()
to start a new process. In the new process you would call this
subroutine, while the original process would go back to trying to
connect.
ok but wouldn't the child process (the x-window/server) die when the
parent (graphics app/client) process dies too ?
That was my understanding of fork() behavior.
You might also want to arrange some additional communication between the
parent and child process, so that the child can tell the parent when it
has started the server. Otherwise, if the parent tries to connect too
soon, it might think it needs to start the server again. You could use
a semaphore for this.
hum i never studied semaphore ;-( but will have a look.
You need to use select() to listen for activity on multiple sockets at
once. Or you can use threads, where one thread waits for new
connections, and other threads process existing connections. If you
only expect a small number of clients this can be an easier way to
program it.
but isn't select() a blocking call too if the socket is non-blocking ?
I tried that approach as a matter of fact and did a call to select
within the loop that check for x-events as well... something like
that:
while(1) {
checkForXevents(&c);
if (c=='q')
break;
// hangs there... wait for something to happen ?
select(fdmax+1...);
}
if I don't make the socket non-blocking the loop hangs on select. If I
make it non-blocking and set a timeout value, you need to wait for a
timeout before the loop continues anyway... which is not so cool when
you want no delay in the user interaction with the window (keyboard,
mouse...). I looked at this problem on different angle and really
can't seem to figure it out. Maybe threading as you said would be an
option but that means a thread to catch x-events and treat them, and
other threads to catch new connections and read from those
connections.
When you say using thread are you thinking of pthread or fork ?
I guess what I could do would be the following:
1/ open the display
2/ create a thread to catch x-event and manipulate the framebuffer
accordingly (zoom in...)
3/ create a thread that "accept" new connections
4/ each connection starts a thread that listen to what's coming in on
that connection
It would be ok but I was hoping for something simpler... I don't want
that x-window/server app to use too much memory and CPU cycles to
leave as much processing power for the graphics app...
Thanks for more of your feedbacks/ideas if you have some
-c
.
- Follow-Ups:
- Re: advices on sockets
- From: Barry Margolin
- Re: advices on sockets
- From: Brice Rebsamen
- Re: advices on sockets
- References:
- advices on sockets
- From: mast4as
- Re: advices on sockets
- From: Barry Margolin
- advices on sockets
- Prev by Date: Re: popen: how to know the child has exited?
- Next by Date: Re: advices on sockets
- Previous by thread: Re: advices on sockets
- Next by thread: Re: advices on sockets
- Index(es):
Relevant Pages
|