Elementary TCP Sockets - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Elementary TCP Sockets

Description:

Elementary TCP Sockets Unix Network Programming Ch # 4 Elementary Socket functions Socket Function To perform network I/O, first thing a process must do is call the ... – PowerPoint PPT presentation

Number of Views:661
Avg rating:3.0/5.0
Slides: 16
Provided by: facultyTa
Category:

less

Transcript and Presenter's Notes

Title: Elementary TCP Sockets


1
Elementary TCP Sockets
  • Unix Network Programming
  • Ch 4

2
Elementary Socket functions
3
Socket Function
  • To perform network I/O, first thing a process
    must do is call the socket function

include ltsys/socket.hgt int socket(int family,
int type, int protocol) - returns non-negative
descriptor if ok, -1 on error
4
Connect funciton
  • The connect function is used by a TCP client to
    establish a connection with a TCP server
  • include ltsys/socket.hgt
  • int connect(int sockfd, const struct sockaddr
    servaddr, socklen_t addrlen)
  • Returns 0 if ok, -1 on error
  • Sockfd is a socket descriptor returned by the
    socket function
  • 2nd 3rd args are the socket address structures,
    must contain the address of the server to
    communicate with
  • The client does not have to call bind
  • The kernel chooses both an ephemeral port and the
    source IP address if necessary.

5
Bind function
  • The bind funtion assigns a local protocol address
    to a socket.
  • With IP, combination of 32-bit (IPv4 or 128-bit
    for IPv6) address, along with a 16-bit TCP or UDP
    port number.
  • include ltsys/socket.hgt
  • int bind(int sockfd, const struct sockaddr
    myaddr, socklen_t addrlen)
  • Servers bind to their well-known port when they
    start
  • A process can bind a specific IP address to its
    socket
  • Normally, however, a client does not bind an IP
    address, so that client can then respond on any
    interface available on the host

6
Listen function
  • The listen function is called only by a TCP
    server and it performs 2 actions
  • Converts an unconnected (active) socket into a
    passive socket (indicates kernel should accept
    incoming connect requests directed to this socket
  • 2nd argument specifies the maximum number of
    connections kernel should queue for this socket
  • include ltsys/socket.hgt
  • int listen(int sockfd, int backlog)

7
Listen function
  • Normally called after both the socket and bind
    function, only by the server of course
  • Backlog - for a given listening socket, the
    kernel maintains 2 queues
  • An incomplete connection queue, which contains an
    entry for each SYN that has arrived from a client
    for which server is awaiting completion of the
    TCP 3-way handshake
  • A completed connection queue, entry for each
    client with whom 3-way handshake has completed.
  • Figure 4.7, pg. 105

8
(No Transcript)
9
Accept function
  • Accept is called by a TCP server to return the
    next completed connection from the front of the
    completed connection queue.
  • If completed queue is empty, the process is put
    to sleep.
  • include ltsys/socket.hgt
  • int accept(int sockfd, struct sockaddr cliaddr,
    socklen_t addrlen)
  • Returns non-negative descriptor if OK, -1 on
    error
  • The cliaddr and addrlen args are used to return
    the protocol address of the connect peer process
    (the client).

10
Fork and exec functions
  • We will look at building a concurrent server
  • Need to create a new child process to handle each
    incomming client request/transaction
  • fork function is the only way in Unix to create a
    new process
  • include ltunistd.hgt
  • pid_t fork(void)
  • Returns 0 in child, process ID of child in
    parent, -1 on error
  • Called once but returns TWICE
  • Once in the parent process (returns child process
    id),
  • and once in the child process (return of 0)

11
More Forking
  • All descriptors open in the parent before the
    call to fork() are shared with the child after
    fork returns.
  • Including the connected socket file description
    returned by accept

12
Exec function
  • Only way in which an executable program file on
    disk can be executed in Unix is for an existing
    process to call one of the 6 exec functions

13
Concurrent Servers
  • When a client request can take some time to
    service, don't want to take away time for
    handling connections to service a single client
  • Handle the communication with multiple clients at
    the same time
  • Simplest way to write a concurrent server under
    Unix is to fork a child process to handle each
    client.

14
Concurrent Servers
15
Close function
  • Close() function used to close a socket and
    terminate a TCP connection
  • include ltunistd.hgt
  • int close(int sockfd)
  • Returns 0 if ok, -1 on error
  • Default action of close with a TCP socket
    description is to mark the socket as closed and
    return tot he process immediately.
  • Socket descriptor is no longer usable to the app
    process at this point
  • But TCP will try to send any data that is already
    queued, and once flushed begin the normal TCP
    termination sequence.
Write a Comment
User Comments (0)
About PowerShow.com