user2467899
user2467899

Reputation: 575

Three questions in order to create a TCP server - socket, Linux with C

I'm going to write a TCP server (Socket - Linux - C). I have three questions:

1) For know if the network (or connectivity) is down, have I use the option SO_KEEPALIVE?

For instance:

int optval = 1;
socklen_t optlen = sizeof(optval); 

if (setsockopt(file_descriptor,SOL_SOCKET,SO_KEEPALIVE,&optval,optlen)<0) {    
Close(file_descriptor);
Print_error("setsockopt() failed");

2) I want that my server contacts other server. How my server know if the remote server shutdown?

3) I want write an concurrent server and so I use fork() in order to create child:

3.1) I have to handle concurrent access to shared variables even if they are only for read purpose?

3.2) I won't have zombie in my process list...Can this code be right?

void sigchld_h (int signum);

int main(int argc, char *argv[]){
...;
}

void sigchld_h (int signum){
    pid_t pid;
    int status;
    while ( (pid = waitpid(-1,&status,WNOHANG)) > 0)
        printf("(%s) INFO - child %d terminated with exit status %d", nome_programma, pid, status);
}

Thank you very much for the consulting.

Upvotes: 1

Views: 388

Answers (2)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84159

  1. SO_KEEPALIVE helps you determine that given TCP connection is dead, meaning the other side had no activity for given time (usually 2 hours). It also helps maintain idle TCP connections through intermediate routers that might just time out connection state due to inactivity. See, for example, here for lengthier explanation.

    Much more useful and convenient way of knowing state of your connected peer is building periodic heartbeat exchange into your application-level protocol.

  2. In this case your server acts as a client. Connect to the remote end and exchange data. You will know the other side is down if you a) cannot connect, or b) receive zero bytes (graceful closing of the socket by the peer), or c) get an error from send(2) saying the pipe is broken. Again, remote server crash is the trickiest to detect. SO_KEEPALIVE would eventually tell you there's a problem, but application heartbeats will tell you much sooner.

  3. fork(2) is the heaviest of the concurrency tools available on modern Unix. Designs based on child process per client connection (and even thread per connection) quickly lead to resource problems, and are generally slow. Explore thread pools and non-blocking sockets along with polling mechanisms like select(2), poll(2), and epoll(7).

Upvotes: 1

user405725
user405725

Reputation:

For know if the network (or connectivity) is down, have I use the option SO_KEEPALIVE?

You can do that. Alternatively, you may implement your own “heartbeat” mechanism on top of TCP/IP.

I want that my server contacts other server. How my server know if the remote server shutdown?

A server cannot connect to another server. The one who connects is a client. The one who accepts is a server. At any rate, you have to use a heartbeat mechanism and handle errors to determine when server goes down (unexpectedly).

I want write an concurrent server and so I use fork() in order to create child

This is the worst idea ever in nearly every case. The best way to do this is to use asynchronous I/O with considerably low number of threads.

I have to handle concurrent access to shared variables even if they are only for read purpose?

If you use fork(), each process receives its own copy of memory, which includes variables, so no synchronization is necessary. In case of threads, you do not need to lock when accessing read-only memory.

I won't have zombie in my process list...Can this code be right?

No, you are not allowed to call printf() from a signal handler. Only async/reentrance safe functions are permitted.

Upvotes: 3

Related Questions