Monday, 15 July 2013

c - Linux Socket: How to detect disconnected network in a client program? -



c - Linux Socket: How to detect disconnected network in a client program? -

i debugging c based linux socket program. examples available in websites, applied next structure:

sockfd= socket(af_inet, sock_stream, 0); connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); send_bytes = send(sockfd, sock_buff, (size_t)buff_bytes, msg_dontwait);

i can observe disconnection when remove server closes server program. if unplug ethernet cable, send function still homecoming positive values rather -1.

how can check network connection in client programme assuming can not alter server side?

but if unplug ethernet cable, send function still homecoming positive values rather -1.

first of should know send doesn't send anything, it's memory-copying function/system call. copies info process kernel - sometime later kernel fetch info , send other side after packaging in segments , packets. hence send can homecoming error if:

the socket invalid (for illustration bogus file descriptor) the connection invalid, illustration hasn't been established or has been terminated in way (fin, rst, timeout - see below) there's no more room re-create data

the main point send doesn't send , hence its homecoming code doesn't tell info reaching other side.

back question, when tcp sends info expects valid acknowledgement in reasonable amount of time. if doesn't one, resends. how resend ? each tcp stack things differently, norm utilize exponential backoffs. is, first wait 1 second, 2, 4 , on. on stacks process can take minutes.

the main point in case of interruption tcp declare connection dead only after big period of silence (on linux 15 retries - more 5 minutes).

one way solve implement acknowledgement mechanism in application. illustration send request server "reply within 5 seconds or i'll declare connection dead" , recv timeout.

c linux sockets send disconnection

No comments:

Post a Comment