1 / 18

The Socket Interface

The Socket API. Application Program InterfaceA set of operations performed by an application when interacting with protocol softwareBSD SocketsIntegrated with I/OReading from a socket is conceptually similar to reading from a file. Procedures That Implement the Socket API. Socket - AllClose - A

kemal
Download Presentation

The Socket Interface

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. The Socket Interface Chapter 29

    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 final, 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.

More Related