Tags: buti, connected, linux, login, managed, message, program, programming, server, socket, unix, user
Check if socket is still open?
On Programmer » Unix & Linux
7,342 words with 6 Comments; publish: Wed, 30 Apr 2008 20:27:00 GMT; (20046.88, « »)
Hello,
I want to try to program a little server.
I already managed it to send a login message to the connected user, but
I wonder how it works to get information about the connection itself?
With this information, I want to close sockets, which are not in use
anymore (because of quitting by the client).
What's the usual way of programming things like that? I mean, which
return value of which command is usually used for things like that?
http://unix-linux.itags.org/q_unix-linux-programming_79296.html
All Comments
Leave a comment...
- 6 Comments

- Markus Pitha <markus.unix-linux.itags.org.pithax.net> writes:
> Hello,
> I want to try to program a little server.
> I already managed it to send a login message to the connected user, but
> I wonder how it works to get information about the connection itself?
> With this information, I want to close sockets, which are not in use
> anymore (because of quitting by the client).
> What's the usual way of programming things like that? I mean, which
> return value of which command is usually used for things like that?
When the remote has closed the TCP session (or the TCP session times
out), next time you try to write or read on the socket you'll get an
error. Then you can close the socket and go on.
So there is nothing special to do, but to check for the error status
of each syscal as usual.
__Pascal Bourguignon__ http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
#1; Wed, 30 Apr 2008 20:29:00 GMT

- Pascal Bourguignon wrote:
> Markus Pitha <markus.unix-linux.itags.org.pithax.net> writes:
>
>
> When the remote has closed the TCP session (or the TCP session times
> out), next time you try to write or read on the socket you'll get an
> error. Then you can close the socket and go on.
> So there is nothing special to do, but to check for the error status
> of each syscal as usual.
>
Attempting to read a TCP socket that has disconnected from the other end
will return zero bytes read. This is an EOF condition. Attempting
to write to such a connection will generate a SIGPIPE and return an
error of EPIPE.
Fletcher Glenn
#2; Wed, 30 Apr 2008 20:30:00 GMT

- On Mon, 2 May 2005, Fletcher Glenn wrote:
> Attempting to read a TCP socket that has disconnected from the other end
> will return zero bytes read. This is an EOF condition. Attempting
> to write to such a connection will generate a SIGPIPE and return an
> error of EPIPE.
Adding to what both you guys said, the OP would be well served buying
and reading a copy of UNIX Network Programming, by Stevens et al.
Rich Teer, SCNA, SCSA, OpenSolaris CAB member
President,
Rite Online Inc.
Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
#3; Wed, 30 Apr 2008 20:31:00 GMT

- Hello,
> Pascal Bourguignon schrieb:
> When the remote has closed the TCP session (or the TCP session times
> out), next time you try to write or read on the socket you'll get an
> error. Then you can close the socket and go on.
> So there is nothing special to do, but to check for the error status
> of each syscal as usual.
Ok, I already read something similiar in a FAQ about socket programming,
but I have a little problem to turn this task. Below is the example of
my endless-loop, which should later include sending and receiving data,
as well as quitting sockets of closed connections. When I add a command
for checking if a socket is open or not, it just checks it once, (when a
connection to a new client was established). When I try it with a
'while' anywhere inside the server endless loop, I get some weird
behaviour and and no message about closed sockets.(I tried it with some
printf commands, when a socket should have been closed, but obviously
the program didn't even come these commands.) Can you give me a hint
how a program usually manage things like that? (probably a loop, but
where and how?) Programming with connections, or sockets is absolutely
new to me.
while(1) {
/*check for unneeded connections*/
printf("\n Server: accept()...");
if ((new_socket = accept(serversocket, (struct sockaddr *)&clientinfo,
&length)) == -1) {
perror("accept()");
}
printf("Connected with %s on socket %d",
inet_ntoa(clientinfo.sin_addr), new_socket);
printf("\nSending Login Message to ... %s",
inet_ntoa(clientinfo.sin_addr));
/*Sending a msg to the login user*/
if (send(new_socket, loginmsg, loginlength, 0) == -1) {
perror("send()");
}
}
close(serversocket);
#4; Wed, 30 Apr 2008 20:32:00 GMT

- Markus Pitha wrote:
> Hello,
>
>
>
> Ok, I already read something similiar in a FAQ about socket programming,
> but I have a little problem to turn this task. Below is the example of
> my endless-loop, which should later include sending and receiving data,
> as well as quitting sockets of closed connections. When I add a command
> for checking if a socket is open or not, it just checks it once, (when a
> connection to a new client was established). When I try it with a
> 'while' anywhere inside the server endless loop, I get some weird
> behaviour and and no message about closed sockets.(I tried it with some
> printf commands, when a socket should have been closed, but obviously
> the program didn't even come these commands.) Can you give me a hint
> how a program usually manage things like that? (probably a loop, but
> where and how?) Programming with connections, or sockets is absolutely
> new to me.
>
> while(1) {
> /*check for unneeded connections*/
> printf("\n Server: accept()...");
> if ((new_socket = accept(serversocket, (struct sockaddr *)&clientinfo,
> &length)) == -1) {
> perror("accept()");
> }
> printf("Connected with %s on socket %d",
> inet_ntoa(clientinfo.sin_addr), new_socket);
> printf("\nSending Login Message to ... %s",
> inet_ntoa(clientinfo.sin_addr));
> /*Sending a msg to the login user*/
> if (send(new_socket, loginmsg, loginlength, 0) == -1) {
> perror("send()");
> }
> }
> close(serversocket);
>
You need to learn about select(). This will help you with managing
multiple connections.
Fletcher Glenn
#5; Wed, 30 Apr 2008 20:33:00 GMT

- Fletcher Glenn schrieb:
> You need to learn about select(). This will help you with managing
> multiple connections.
Step by step i wonder if I misunderstood everything. Actually I imagined
my server as some kind of chat server, with permanently open
connections, but actually it isn't something like this.
Probably some kind of IRC sockets are what I'm looking for.
#6; Wed, 30 Apr 2008 20:34:00 GMT