Anmelden

Archiv verlassen und diese Seite im Standarddesign anzeigen : [Sprache C] Rausfinden ob Socketverbindung noch 'da' ist



mrsuicide
11-04-2003, 17:22
Ich habe mit socket() ein Socket erstellt und so einen Multithread Server zusammen.
Ich befine mich in der Hauptschleife.
Nun wie kann ich rausfinden ob die Verbindung zum Client unterbrochen/der Client disconnected ist??

pik7
11-04-2003, 19:04
hallo,

aus
man 7 socket:

SIGNALS
When writing onto a connection-oriented socket that has
been shut down (by the local or the remote end) SIGPIPE is
sent to the writing process and EPIPE is returned. The
signal is not sent when the write call specified the
MSG_NOSIGNAL flag.

When requested with the FIOCSETOWN fcntl or SIOCSPGRP
ioctl, SIGIO is sent when an I/O event occurs. It is pos-
sible to use poll(2) or select(2) in the signal handler to
find out which socket the event occurred on. An alterna-
tive (in Linux 2.2) is to set a realtime signal using the
F_SETSIG fcntl; the handler of the real time signal will
be called with the file descriptor in the si_fd field of
its siginfo_t. See fcntl(2) for more information.


gruß

mrsuicide
12-04-2003, 15:18
Wie mache ich das?
Muss ich da immer send(), write() oder recv() verwenden?
Was ist wenn es einfach nur eine Verbindung sein soll, die darauf hinausläuft den Server zu signalisieren, dass wenn sie getrennt ist der Client offline ist?
Geht das auch bei nonblocking sockets?

pik7
12-04-2003, 17:24
hallo,

man kann auch SO_KEEPALIVE verwenden.
Sendet auch ein SIGPIPE wenn die Verbindung unterbrochen ist.
ohne vorher ein send,write oder recv zu benutzten.
aus man socket:

The communications protocols which implement a SOCK_STREAM
ensure that data is not lost or duplicated. If a piece of
data for which the peer protocol has buffer space cannot
be successfully transmitted within a reasonable length of
time, then the connection is considered to be dead. When
SO_KEEPALIVE is enabled on the socket the protocol checks
in a protocol-specific manner if the other end is still
alive. A SIGPIPE signal is raised if a process sends or
receives on a broken stream; this causes naive processes,
which do not handle the signal, to exit.

zum setzten von SO_KEEPALIVE benutzt man dann setsockopt().

gruß