A non-blocking socket reading function

M

Mikael S. H.

Hello comp.lang.c++.

My friend and I are trying to make a simple IRC client. It'll work
mostly like irssi, by having "signals", which you can "tie" a function
to. These functions get executed whenever a certain event happens, such
as "message received" or "got opped".

The problem is, that I haven't been able to do this, *and* add
"timeouts", which would execute a function after a certain piece of
time. Whenever I'm trying to read from the socket, it stops program
execution if no message is to be received, and only continues when it does.

I've thought about making a threaded version, so another process will
constantly read from the socket, putting any messages there are into a
variable, which the main program would read. This would mean, that you
would simply get NULL from the socket_read() function, if there is no
message.

That is not an easy task, I found out.

I have never seen this much frightening documentation about a subject in
my life. I have tried to read into Mutex's, threadsafe functions and
void pointer casting of arguments, but it all fails miserably to even
make the slightest sense.

Is threading the best solution to this problem?

How would some code look, just in general?

Thank you all in advance,
Mikael S. Høilund.
 
T

Thomas J. Gritzan

Mikael said:
My friend and I are trying to make a simple IRC client. It'll work
mostly like irssi, by having "signals", which you can "tie" a function
to. These functions get executed whenever a certain event happens, such
as "message received" or "got opped".

The problem is, that I haven't been able to do this, *and* add
"timeouts", which would execute a function after a certain piece of
time. Whenever I'm trying to read from the socket, it stops program
execution if no message is to be received, and only continues when it does.

I've thought about making a threaded version, so another process will
constantly read from the socket, putting any messages there are into a
variable, which the main program would read. This would mean, that you
would simply get NULL from the socket_read() function, if there is no
message.

This is offtopic here, since plain C++ doesn't know about sockets or
threads. Ask in a newsgroup about your operation system.

<OT>
You don't need threads for this. Read about the functions select() and
poll().
</OT>

Thomas
 

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

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top