Re: socket problem
Re: socket problem
- Subject: Re: socket problem
- From: p3consulting <email@hidden>
- Date: Tue, 17 Jun 2003 12:03:35 +0200
Reading (read(), recv(), recvfrom(), ) from the server on the client
connection will return 0 when the client closes the connection, -1 if
there is an error, and N > 0 the number of bytes read in case of
success
So detecting if client is still alive will depend on your server
architecture:
if you create a listening thread for each client when a client connects
for the first time, there is no problem: the blocking read in this
thread will return 0 when the client dies,
if you have only one thread waiting for client connections and
immediately answering to client request, you need a different strategy
that will depend on your application: you know a client is dead if you
try to simulate "ping" and the write(), sendto(), returns an error;
you will have to implement a polling mechanism (maybe in another
thread) to do that. Be careful to detect client down and up between
polling interval (same ip address but different port) and decide you to
handle several clients from the same ip address if your application
allows this (you will receive several connect request from the same ip
address but each one from different client port, and they will be alive
at the same time) .
So technically the answer to your question is no too complicate:
the server reads from the client: result = 0: client has closed the
connection, result < 0 another error. In case of timeout (if you decide
to handle it) : the client socket is alive but the client process
doesn't send any data to the server
the server writes to the client, if result < 0 : error occurred, client
(or network path to client) is probably dead
But implementing the solution depends a lot on your application, the
protocol you are using (TCP or UDP ?)
Some doc/example:
http://www.ecst.csuchico.edu/~beej/guide/net/
http://www-users.cs.umn.edu/~bentlema/unix/advipc/ipc.html
Omni Networking Framework and other Cocoa network framework you will
find on the net
This one is for Java but technical background applies to your question
http://www.davidreilly.com/java/java_network_programming/
Pascal Pochet
P3 Consulting
email@hidden
http://www.p3-consulting.net
Le lundi, 16 jun 2003, ` 20:26 Europe/Brussels,
email@hidden a icrit :
De: Morphine <email@hidden>
Date: Lun 16 jun 2003 19:26:21 Europe/Brussels
@: email@hidden
Objet: socket problem
Hello,
I have a problem with socket !!!
I develop a application (client/server) that use RendezVous and
NSNetService. For connect, write and read no problem ; if I close the
NSFileHandle that handle the socket with the function
[socketFileHandle closeFile] on the client side, I don't know how to
check if connection is alive from the server ?
Same question, if I don't close the client's socket and force to quit
the client (if client bug), how to handle this event from the server ?
I generally know that it's the server that close the socket, but I
want the errors under control.
Thank you very much.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.