Docs for socket functions

Hello,
I've just begun creating simple C++ server for linux. I've based my server on example on the internet but now I can't actually find any docs for functions I am using so I cannot extend my program far from the example.
I am linking theese libraries for TCP sockets:
1
2
3
4
5
6
7
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> 

For receiving and sending data I use functions read and write. This is quite tricky to search on google.
1
2
int read(int con, char* buffer, int length)
int write(int con, char* text, int length)

So can anyone help me finding some docs for theese libraries? I have no idea how can I work with connection that is presented as integer.
Right now, what I need the most is to set max connection delay on reading and how to detect whether connection still exists.
Thank you in advance.
The functions and headers files you're referring to are part of POSIX, you can find them here:

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_socket.h.html
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/netinet_in.h.html
etc

although linux man pages are fairly detailed as well. Just be mindful of the differences and stick to POSIX if you want to have some portability.. but then if you cared about portability, you'd be using boost.asio to begin with : http://www.boost.org/doc/libs/release/doc/html/boost_asio.html )

Either way, if you're interested in TCP programming, a book on TCP, the protocol (not the functions you call to manipulate it), would be helpful too.

For your specific questions,

set max connection delay on reading

I am not sure what this means, delay between which and which events, specifically and what is expected to occur when it expires?

detect whether connection still exists.

This cannot be done passively: until you try to send a packet, you won't know that the cable was unplugged. You could enable TCP keepalive protocol (but tune the /proc settings in your Linux server -- it is uselessly slow by default), but I usually find it easier to implement your own keepalive protocol at the application level, something like IRC's ping/pong.
Last edited on
Thank you for URLs,
but unfortunatelly, I've found theese before and they didn't give me slightest idea where 'write' and 'read' functions come from and how to set delay.
With 'delay' I meant time between invoking read function and achieving to get any data.
If this delay times out I'm going to send test packet as you told me, given the connection cannot be tested any other way.
Thank you for your time
where 'write' and 'read' functions come from

Same site:
read:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html (it starts off talking about files, so skip to the line "If fildes refers to a socket"

write:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html (it starts off talking about files, so skip to the line "If fildes refers to a socket,"

I meant time between invoking read function and achieving to get any data. If this delay times out I'm going to send test packet

This timeout is configured by calling setsockopt() with SO_RCVTIMEO

http://pubs.opengroup.org/onlinepubs/9699919799/functions/setsockopt.html

http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_10_16

Be aware that it is not portable (e.g. Windows has it too, but it's set up differently, and even in POSIX it is implementation-defined)

You could also:
1. Poll the socket by calling read() with O_NONBLOCK at fixed time intervals. If it keeps returning nothing and enough time passed, do your ping

2. Use I/O multiplexing, e.g. by passing the socket descriptor and the desired timeout to select() : http://pubs.opengroup.org/onlinepubs/9699919799/functions/select.html
or poll(): http://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html or another multiplexing syscall

3. Use an actual library, such as boost.asio, which does I/O multiplexing and the reactor pattern for you.
Last edited on
Hello,
thank you very much, you've helped me to move forward. Now, timeouts work very well, but another problem showed up.
Once a client disconnect while server is trying to read/write, application often shuts down with no warning.
Is this some common problem?
For example, I've made a push loop for client that is expecting to get real time streamed data:
while(1) {
if(write(handle, "\0",1)<0) {
std::cout<<"Disconnected...\n";
break;
}
sleep(2);
}
When i close telnet window aplication turns off right after write atempt.
Same happens everywhere.
Thank you very much.
Topic archived. No new replies allowed.