The Socket API Prof. Chuan-Ming Liu Computer Science and - - PowerPoint PPT Presentation

the socket api
SMART_READER_LITE
LIVE PREVIEW

The Socket API Prof. Chuan-Ming Liu Computer Science and - - PowerPoint PPT Presentation

Mobile Computing & Software Engineering Lab The Socket API Prof. Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology Taipei, TAIWAN NTUT, TAIWAN 1 Mobile Computing & Software


slide-1
SLIDE 1

NTUT, TAIWAN 1

Mobile Computing & Software Engineering Lab

The Socket API

  • Prof. Chuan-Ming Liu

Computer Science and Information Engineering National Taipei University of Technology Taipei, TAIWAN

slide-2
SLIDE 2

NTUT, TAIWAN 2

Mobile Computing & Software Engineering Lab

Socket API

Specifying a Protocol Interface Socket Abstraction Specifying an Endpoint Address Address Structure Major System Calls Integer Conversion Symbolic Constants

slide-3
SLIDE 3

NTUT, TAIWAN 3

Mobile Computing & Software Engineering Lab

Specifying a Protocol Interface

Two approaches when designing functions to OS that provide applications to access TCP/IP protocols

Specific functions for TCP/IP communication Flexible functions in general with parameters to specify

slide-4
SLIDE 4

NTUT, TAIWAN 4

Mobile Computing & Software Engineering Lab

Specifying a Protocol Interface

Example:

First approach may use maketcpconnection The other may use makeconnection with a parameter to specify the TCP protocol

Second approach is preferable

To accommodate multiple sets of communication protocols TCP/IP protocols are one single family (family PF_INET)

slide-5
SLIDE 5

NTUT, TAIWAN 5

Mobile Computing & Software Engineering Lab

Specifying a Protocol Interface

Applications specify operations using a type of service instead of the protocol name

Example: using stream transfer type of service to specify a TCP connection

slide-6
SLIDE 6

NTUT, TAIWAN 6

Mobile Computing & Software Engineering Lab

Specifying a Protocol Interface

The socket API provides generalized functions that support network communication using many possible

  • protocols. Socket calls refer to all TCP/IP

protocols as a single protocol family. The calls allow the programmer to specify the type of service required rather than the name of a specific protocol.

slide-7
SLIDE 7

NTUT, TAIWAN 7

Mobile Computing & Software Engineering Lab

Socket Abstraction

File descriptors

When performing I/O on a file, open( ) function is called

  • pen( ) function will create a file descriptor

deal with the files Are stored in a file descriptor table Each process has one separate descriptor table

slide-8
SLIDE 8

NTUT, TAIWAN 8

Mobile Computing & Software Engineering Lab

File Descriptor Table

1 2 3 descriptor table (one per process) internal data structure for file 0 internal data structure for file 1 internal data structure for file 2 internal data structure for file 3

Operation System

slide-9
SLIDE 9

NTUT, TAIWAN 9

Mobile Computing & Software Engineering Lab

Socket Abstraction

Similarly, socket API adds new abstraction for network communication, the socket Each socket is identified by an small integer, socket descriptor

same with the file descriptor An application cannot have both a file descriptor and a socket descriptor with the same value Note: Windows OS using a separate table for socket descriptor

slide-10
SLIDE 10

NTUT, TAIWAN 10

Mobile Computing & Software Engineering Lab

Socket Abstraction

Socket API contains a function, socket to create a socket. On UNIX, file and socket descriptors use the same descriptor table In Windows OS, a file descriptor and a socket descriptor may have the same value.

slide-11
SLIDE 11

NTUT, TAIWAN 11

Mobile Computing & Software Engineering Lab

Socket Data Structure

When socket() is called, the OS

allocates a new data structure for socket fills in a new descriptor table entry leaves most of the fields in the structure unfilled

slide-12
SLIDE 12

NTUT, TAIWAN 12

Mobile Computing & Software Engineering Lab

Socket Descriptor Table

OS

Remote port: Local port: Remote IP: Local IP: Service: SOCK_STREAM Family: PF_INET data structure for a socket pointers to

  • ther

socket structures 1 2 3 socket descriptor table (one per process) 4

slide-13
SLIDE 13

NTUT, TAIWAN 13

Mobile Computing & Software Engineering Lab

Socket Abstraction

When a socket has been created,

The socket is completely general and can be used for arbitrary communication Application specifies how it will be used

Tell the role of a socket in

Passive Active

The only difference between an active socket and a passive one lies in how the applications use it; initially all sockets are created in the same way

slide-14
SLIDE 14

NTUT, TAIWAN 14

Mobile Computing & Software Engineering Lab

Specifying an Endpoint Address

When a socket is created

contains no information about how to use it no information about the port number and IP address, …, etc

Before using a socket, it must specify some addresses

slide-15
SLIDE 15

NTUT, TAIWAN 15

Mobile Computing & Software Engineering Lab

Specifying an Endpoint Address

Communication endpoint

In TCP/IP, consists of an IP address and a protocol port number Each protocol family has its own specification for endpoint address

slide-16
SLIDE 16

NTUT, TAIWAN 16

Mobile Computing & Software Engineering Lab

Specifying an Endpoint Address

The socket abstraction defines an address family for each type of address

Each protocol family can use one or more address families to define address representations TCP/IP protocols all use a single address representation with the symbolic constant AF_INET

slide-17
SLIDE 17

NTUT, TAIWAN 17

Mobile Computing & Software Engineering Lab

Specifying an Endpoint Address

Note:

Confusion arises between the TCP/TP protocol family, PF_INET and its address family, AF_INET Both have the same numeric value (2)

slide-18
SLIDE 18

NTUT, TAIWAN 18

Mobile Computing & Software Engineering Lab

Address Structure

Socket system defines a generalized format that all endpoint addresses used for flexibility

(address family, endpoint address in that family) Address family: one of preassigned address types The standard representation for the specified address type

slide-19
SLIDE 19

NTUT, TAIWAN 19

Mobile Computing & Software Engineering Lab

Sockadd Structure

The most general structure, sockaddr structure

struct sockaddr { u_char sa_len; /* total length */ u_short sa_family; /* type of address*/ char sa_data[14]; /* value of address*/ };

slide-20
SLIDE 20

NTUT, TAIWAN 20

Mobile Computing & Software Engineering Lab

Sockadd Structure

Does not fit for all address families

Some address families have longer endpoint address Accommodates address in the AF_INET family, so TCP/IP software works well To keep the portability and maintainable, do not use this address structure

slide-21
SLIDE 21

NTUT, TAIWAN 21

Mobile Computing & Software Engineering Lab

Sockadd_in Structure

TCP/IP protocols exclusively use structure sockaddr_in instead

struct sockaddr_in { u_char sin_len; /* total length */ u_short sin_family; /* type of address*/ u_short sin_port; /* port number*/ struct in_addr sin_addr; /* IP address*/ char sin_zero[8]; /* unused*/ };

slide-22
SLIDE 22

NTUT, TAIWAN 22

Mobile Computing & Software Engineering Lab

Sockadd_in Structure

When representign a TCP/IP communication endpoint, an application program uses structure sockaddr_in, which contains both an IP address and a protocol port number. Programmers must be careful when writing programs that use a mixture of protocols because some non-TCP/IP endpoint addresses require a larger structure

slide-23
SLIDE 23

NTUT, TAIWAN 23

Mobile Computing & Software Engineering Lab

Major System Calls in the Socket API

Two groups for socket calls:

Primary calls Utility routines

slide-24
SLIDE 24

NTUT, TAIWAN 24

Mobile Computing & Software Engineering Lab

Primary Socket Calls

Socket Connect Recv Close Bind Listen Accept Read and write

slide-25
SLIDE 25

NTUT, TAIWAN 25

Mobile Computing & Software Engineering Lab

Socket Routines

Particularly, we discuss the ones for integer conversion, including

htons ntohs htonl ntohl

slide-26
SLIDE 26

NTUT, TAIWAN 26

Mobile Computing & Software Engineering Lab

Socket Function

Create a new socket for network communication Return a descriptor for the newly created socket

slide-27
SLIDE 27

NTUT, TAIWAN 27

Mobile Computing & Software Engineering Lab

Connect Function

On client side Following the socket call Establish an active connection to a remote server

slide-28
SLIDE 28

NTUT, TAIWAN 28

Mobile Computing & Software Engineering Lab

Send Function

Clients and servers use it to transfer data across a TCP connection Client usually uses send to transmit request; server uses it to transmit replies

Which data should be sent The address of the data to be sent The length of the data

slide-29
SLIDE 29

NTUT, TAIWAN 29

Mobile Computing & Software Engineering Lab

Recv Function

Clients and serves use it to receive data from a TCP connection Usually, servers use it to receive requests; clients use if to receive replies

Which socket descriptor to use The address of a buffer The length of the buffer

The amount of data received depends on the buffer size defined

Also works for receiving message using UDP

slide-30
SLIDE 30

NTUT, TAIWAN 30

Mobile Computing & Software Engineering Lab

Close Function

To finish using a socket A socket may be used by many processes, close terminates the connection for the process issues the close call and reduces the reference count by 1 until to 0; when the counter is 0, the socket is deallocated No socket reference count for the threads in a process

slide-31
SLIDE 31

NTUT, TAIWAN 31

Mobile Computing & Software Engineering Lab

Bind Function

When socket created, no endpoint information inside Bind is used to specify the local endpoint address

slide-32
SLIDE 32

NTUT, TAIWAN 32

Mobile Computing & Software Engineering Lab

Listen Function

Connection-oriented (TCP) servers use it to place a socket in passive mode and make the server ready to accept incoming connection Set the number of incoming TCP connections the system will enqueue

slide-33
SLIDE 33

NTUT, TAIWAN 33

Mobile Computing & Software Engineering Lab

Accept Function

After using listen to place a socket in passive mode, the server calls accept to extract the next incoming connection request Accept creates a new socket for each new connection request, and returns the descriptor of the new socket to its caller Server uses the original socket to accept additional connect requests After finishing using the new socket, the server closes it

slide-34
SLIDE 34

NTUT, TAIWAN 34

Mobile Computing & Software Engineering Lab

Utility Routines – integer conversion

TCP/IP uses network byte order to represent integer with the most significant byte first To be portable, the local machine’s byte

  • rder should be consistent with the network

byte order

slide-35
SLIDE 35

NTUT, TAIWAN 35

Mobile Computing & Software Engineering Lab

Utility Routines – integer conversion

Routines: (h: host, n: network, s: short, l: long)

htons, ntohs htonl, ntohl

For safety, converting the number anyway

slide-36
SLIDE 36

NTUT, TAIWAN 36

Mobile Computing & Software Engineering Lab

Flow chart for client

socket connect send recv close

slide-37
SLIDE 37

NTUT, TAIWAN 37

Mobile Computing & Software Engineering Lab

Flow chart for server

bind accept listen socket send recv close

slide-38
SLIDE 38

NTUT, TAIWAN 38

Mobile Computing & Software Engineering Lab

Symbolic Constants

Most systems provide a set of predefined symbolic constants and data structure declarations Example: (type of service)

SOCK_DGRAM (UDP) SOCK_STREAM (TCP)

#include <sys/types.h> #include<sys/socket.h>