binding, listening, accepting and connecting

Upload: hariharan-venkitasubramanian

Post on 05-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    1/13

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    2/13

    Some details about the previous slides bind( int sfd, struct sockaddr *a, socklen_t al )

    a machine can have several addresses (several networkcards, loopback, )

    tells the socket on the server side which local protocol

    (i.e., IP address and port number) to listen to

    listen( int sfd, int backlog )

    prepares the server for listening to connect requests,and initializes a queue for connect requests ( passive)

    the second parameter ( SOMAXCONN) defines how longthe queue(s) should be

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    3/13

    More details sock = accept( int sfd, struct sockaddr *a, socklen_t *al )

    take the first connect request from the connect request queue wait for the connect request to arrive if the queue is empty returns a new socket that the server can use to communicate with the client a (clientaddr ) contains information about the client

    almust be initialized, so accept knows size of

    a

    connect( int sfd, struct sockaddr *serv_a, socklen_t al ) connects client socket to a server that is specified in the address structure a three-way handshake is initiated for TCP possible errors

    ETIMEDOUT no response (after several tries) and timer expired ECONNREFUSED server not running or not allowed to connect EHOSTUNREACH HOST not reachable ENETUNREACH NET not reachable

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    4/13

    Closing of Sockets

    Note that the semantics of close depends On the kind of protocol Some possible extra settings

    All data that has not been read yet may be thrown away

    /* Close the socket */close (sock);

    Client Server

    /* Close both sockets */close (sock);close (request_sock);

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    5/13

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    6/13

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    7/13

    Summary of Socket Functions for our Elementary TCP Client-Server

    socket()

    bind()

    listen()

    accept()

    read()

    write()

    close()

    write()

    read()

    close()

    socket()

    connect()

    Client

    Server

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    8/13

    Compilation of socket programs The example can be downloaded from the web pages

    (http://www.ifi.uio.no/~inf1060/programs/client-server-example)

    IFIs Linux machines gcc client1.c o client

    IFIs Solaris machines gcc client1.c o client lsocket lnsl

    Cygwin on Windows gcc client1.c o client

    Similar for server1.c

    For testing, run server on kaksi (or change the address in the

    client) and start client on another machine

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    9/13

    Complete Server...

    int main(){

    /* Declarations */...

    /* Create socket */request_sock = socket (...);

    /* Fill in the address structure */...

    /* Bind address to socket */ bind (...);

    /* Activate connect request queue */listen (...);

    Server Server ctd.

    /* Receive connection */ sock = accept (...);

    /* Process the request*/...

    /*Close sockets */close (sock);

    close (request_sock);}

    Iterative servers?

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    10/13

    Iterative Servers...

    int main(){

    /* Declarations */...

    /* Create socket */

    request_sock = socket (...);

    /* Fill in the address structure */...

    /* Bind address to socket */ bind (...);

    /* Activate connect request queue */listen (...);

    Server Server ctd.

    for (;;) {/* Receive connection */ sock = accept (...);

    /* Process the request*/...

    /*Close sockets */close (sock);

    }

    close (request_sock);}

    Concurrent servers?

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    11/13

    Concurrent Iterative Servers...

    int main(){

    /* Declarations */...

    pid_t pid;

    /* Create socket */

    request_sock = socket (...);

    /* Fill in the address structure */...

    /* Bind address to socket */ bind (...);

    /* Activate connect request queue */listen (...);

    Server Server ctd.

    for (;;) {/* Receive connection */ sock = accept (...);

    if ((pid = fork()) == 0) {close (request_sock);

    /* Process the request*/...

    /*Close sockets */close (sock);exit(0)

    }

    /*Close sockets */close (sock);

    }

    close (request_sock);}

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    12/13

  • 7/31/2019 Binding, Listening, Accepting and Connecting

    13/13