The Socket Interface - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

The Socket Interface

Description:

The following s are taken from Internetworking with TCP/IP ... It will only honor TCP segments that request a new connection. IP Address Manipulation Routines ... – PowerPoint PPT presentation

Number of Views:248
Avg rating:3.0/5.0
Slides: 33
Provided by: informat1537
Category:

less

Transcript and Presenter's Notes

Title: The Socket Interface


1
The Socket Interface
  • Chapter 21

2
The Socket API
  • Application Program Interface
  • A set of operations performed by an application
    when interacting with protocol software
  • BSD Sockets
  • Integrated with I/O
  • Reading from a socket is conceptually similar to
    reading from a file

3
Procedures That Implement the Socket API
  • Socket - All
  • Close - All
  • Bind Servers
  • Listen TCP Servers
  • Accept TCP Servers
  • Connect
  • Clients
  • Different function in TCP and UDP (that is a play
    on words)
  • Send
  • Recv

4
Socket(protofamily, type, protocol)
  • Creates a socket and returns an integer
    descriptor, cf open()
  • Protofamily is always PF_INET
  • There are two types
  • SOCK_STREAM (TCP)
  • SOCK_DGRAM (UDP)
  • Protocol will correspond to either TCP or to UDP
    (getprotobyname())

5
Close(socket)
  • Just like closing a file.

6
Bind(socket, localaddr, addrlen)
  • Socket has been created but not previously bound
  • Localaddr is specified by a C struct
  • Family, ie, address type
  • Port number
  • Ip address

7
Bind (continued)
  • Specifying the IP address seems strange, and it
    is, except for multi-homed hosts, ie, hosts with
    more than one IP address, so we usually specify
    INADDR_ANY
  • Bind essentially tells the OS to contact us by
    the socket when something arrives on the port to
    which the socket is bound

8
Listen(socket, queuesize)
  • Listen registers interest in an event on the
    corresponding socket
  • Queuesize specifies the number of requests we are
    willing to handle

9
Socket Accept(socket, caddress, caddresslen)
  • Blocks until a client requests a connection
  • Fills in the address of that client
  • Returns a new socket to handle further
    communication

10
Connect(socket, saddress, saddresslen)
  • Used by clients to contact servers
  • Socket is a created socket
  • Saddress is the IP address, port number you wish
    to contact
  • Different with connection-oriented and
    connectionless protocols

11
Send(socket, data, length, flags)
  • Like writing to a file whose descriptor is socket
  • Data is the address of a buffer where you have
    stored what you wish to send
  • Flags is usually zero
  • Send assumes a connection

12
Variations on send()
  • Sendto(socket, data, length, flags, destaddress,
    addresslen)
  • Adds a struct sockaddr specifying where to send
    the data
  • Sendmsg(socket, mgsstruct, flags)
  • Fills in a struct mgsstruct with destination
    address and the address of the data

13
Recv(socket, buffer, length, flags)
  • Just like reading a file
  • Socket specifies a particular socket
  • Buffer specifies where you want the data stored
  • Length indicates the maximum number of bytes you
    are willing to receive
  • Very important to prevent buffer overflow

14
Variations on recv
  • Recvfrom corresponds to sendto
  • Recvmsg corresponds to sendmsg

15
Read(socket, buffer, length)
  • Must be used with connected sockets
  • Really is just like reading a file
  • Used for generality
  • Today Im reading a socket
  • Tomorrow Ill be reading a file
  • Write is used similarly

16
Other Socket Procedures
  • Gethostbyname is passed a string, eg, ursa and
    returns the IP address and other information
    about ursa
  • Getprotobyname is passed a string, eg tcp and
    returns the corresponding number

17
Additional items of interest
  • TCP clients and servers read data from a stream,
    hence in a loop
  • UDP clients and servers use a single read
    statement that reads a datagram
  • UDP servers
  • Do not listen
  • Do not accept
  • Must use recvfrom and sendto
  • UDP clients either
  • Connect and use send
  • Do not connect and use sendto

18
Two annoying things
  • Clients do not bind!! They allow the OS to assign
    a free port for their communication.
  • When filling in the destination information you
    usually use a struct sockaddr_in. The C functions
    almost uniformly expect a struct sockaddr. You
    end up doing a lot casts.

19
Start of New Material
  • The following slides are taken from
    Internetworking with TCP/IP

20
Socket Inheritance and Termination
  • There are OS mechanisms that can be used to
    create new processes or threads. Unix provides
    fork and exec.
  • In most systems the new process inherits access
    to the sockets. Socket inheritance is used to
    allow concurrent copies of the server to handle
    multiple requests.

21
Inheritance (continued)
  • The OS keeps a reference count associated with
    each socket so it knows how many application
    processes have access to it.
  • It is the programmers responsibility to ensure
    that multiple processes use the shared socket
    meaningfully.
  • Close doesnt exactly mean close.

22
Obtaining Local and Remoter Socket Addresses
  • Sometimes a created process needs to determine
    the destination address to which a socket
    connects.
  • A process may also wish to determine the local
    address of a socket.
  • Two functions provide this information.

23
Getpeername
  • getpeername(socket, destaddr, addrlen)
  • socket
  • destaddr is a pointer to a structure of type
    sockaddr that will receive the information
  • addrlen is a pointer to an integer that will
    receive the length
  • Only works with connected sockets

24
Getsockname
  • getsockname(socket, localaddr, addrlen)

25
Obtaining and Setting Socket Options
  • getsockopt
  • setsockopt

26
getsockopt
  • getsockopt(socket, level, optionid, optionval,
    length)
  • level does the option refer to the socket itself
    or to the underlying protocols
  • optionid which option
  • optionval and length are pointers
  • setsockopt is similar

27
How A Server Accepts Connections
  • The bind function associates the socket with a
    well-known port but does not specify a foreign
    destination (which must specify a wildcard).
  • The accept function blocks until a connection
    request arrives.

28
Server Connections (continued)
  • After accept returns,
  • Iterative approach handle the request and close
    the new socket, and calls accept again.
  • Concurrent approach fork a process to handle the
    request. The process uses the socket, closes it,
    and terminates.

29
Server Connections (continued)
  • Multiple processes will be using the same local
    protocol port number.
  • In TCP a pair of endpoints determine a
    connection. It does not matter how many processes
    use a port number as long as they connect to
    different distinations.

30
Server Connections (continued)
  • In the case of a concurrent server, there is one
    process per client and one additional process
    that accepts connections. The socket the master
    uses has a wildcard for the foreign destination.
    It will only honor TCP segments that request a
    new connection.

31
IP Address Manipulation Routines
  • inet_aton forms a 32-bit IP address from a string
    representing an address in dotted decimal
    notation
  • inet_network gets the network portion and zeros
    the host portion
  • inet_ntoa reverses inet_aton
  • internetaddr combines network and host (net and
    local reverse this op)

32
Accessing the Domain Name System
  • res_init() reads a file that contains information
    such as the domain name server and stores the
    results in a global structure, res.
  • Res_mkquery(op, dname, class, type, data,
    datalen, newrr, buffer, buflen) forms a domain
    name query and places it in a buffer
Write a Comment
User Comments (0)
About PowerShow.com