sockets lecture #3. the socket interface funded by arpa (advanced research projects agency) in 1980....

48
SOCKETS Lecture #3

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

SOCKETS

Lecture #3

Page 2: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

The Socket Interface

• Funded by ARPA (Advanced Research Projects Agency) in 1980.

• Developed at UC Berkeley• Objective: to transport TCP/IP software to

UNIX• The socket interface has become a de facto

standard.

Page 3: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Approach

• Define functions that support network communications in general, and use parameters to make TCP/IP communication a special case.

• Socket calls refer to all TCP/IP protocols as a single protocol family.

Page 4: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

TCP/IP

• provides peer-to-peer communication.• Is a protocol that provides basic

mechanisms to transfer data• provides connectionless and connection-

oriented servers.

Page 5: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Concurrency

Main() {int I;

fork();for(I=0; I<5; I++) {

printf(“I = %d\n”, I);fflush(stdout);

}

exit(0);}

Page 6: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Fork (Cont’d)

main() {int pid;

pid = fork();if ( pid == 0 )

printf(“The child process\n”);else

printf(“The parent process\n”);exit(0);

}

Page 7: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Basic I/O Functions in UNIX

• Open• close• read• write• lseek• ioctl

Page 8: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Using I/O in UNIX

int desc;...desc = open(“file”, O_RDWR, 0);read(desc, buffer, 128);…close(desc);

Page 9: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Using UNIX I/O with TCP/IP

• They extended the conventional UNIX I/O facilities

• It became possible to use file descriptors for network communication

• Extended the read and write system calls so they work with the new network descriptors.

Page 10: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Descriptor Table

...

0

1

2

Internal data structurefor file 0

Page 11: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

...

0

1

2

Internal data structurefor file 0

Family: PF_INET

...

Service: SOCK_STREAM

Local IP:

Remote IP:

Local Port:

Remote Port:

Page 12: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Passive/Active Socket

• A passive socket is used by a server to wait for an incoming connection.

• An active socket is used by a client to initiate a connection.

Page 13: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Sockets

• When a socket is created it does not contain information about how it will be used.

• TCP/IP protocols define a communication endpoint to consist of an IP address and a protocol port number.

Page 14: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Socket Functions

• socket: create a descriptor for use in network communication.

• connect: connect to a remote peer (used by client)

• write: send outgoing data across a connection.

• read: acquire incoming data from a connection.

Page 15: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Socket Functions (Cont’d)• close: terminate communication and

deallocate a descriptor.• bind: bind a local IP address and protocol

number to a socket.• listen: place the socket in passive mode and

set the number of incoming TCP connections the system will enqueue.

• accept: accept the next incoming connection (by server).

Page 16: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Data Conversion Functions

• The functions htons, ntohs, htonl, and ntohl are used to convert binary integers between the host’s native byte order and the network standard byte order.

• This makes the source code portable to any machine, independent of the the native byte order

Page 17: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

socket()

bind()

listen()

accept()

read()

write()

procees request

get a blocked client

Server Process

TCP UDP

socket()

connect()

write()

read()

Client Process

socket()

bind()

sendto()

recvfrom()

Client Process

1

2

3

socket()

bind()

recvfrom()

sendto()

Server Process

get a blocked client

process request

Page 18: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

TCP vs. UDP

• TCP (Transmission Control Protocol– Connection-oriented– Reliability in delivery of messages– Splitting messages into datagrams– keep track of order (or sequence)– Use checksums for detecting errors

Page 19: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

TCP vs. UDP (Cont’d)

• UDP (User Datagram Protocols)– Connectionless– No attempt to fragment messages– No reassembly and synchronization– In case of error, message is retransmitted– No acknowledgment

Page 20: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Selecting UDP

• Remote procedures are idempotent• Server and client messages fit completely

within a packet.• The server handles multiple clients (UDP is

stateless)

Page 21: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Selecting TCP

• Procedures are not idempotent• Reliability is a must• Messages exceed UDP packet size

Page 22: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

OSI Layers vs. TCP/IP

Network

HardwareInterface

IP

TCP UDP

UserApplication5-7. Session

4. Transport

3. Network

1-2. Data Link/ Physical

Page 23: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Client Architecture• Simpler than servers• Most clients do not explicitly handle

concurrent interactions with multiple servers.

• Most client software executes as a conventional program.

• Clients, unlike servers, do not require special privileged ports.

• Most clients rely on OS for security.

Page 24: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Socket Address Structure

• struct sockaddr_in {• short sin_family;• u_short sin_port;• struct in_addr sin_addr;• char sin_zero[8];• };

Page 25: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Domain Name Structure

• struct hostent {• char *h_name; /* official name

of host */• char **h_aliases; /* host’s

alias names */• int h_addrtype; /* address type

*/• char **h_addr_list; • /*list of addresses from

name server */• };

Page 26: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Example:

Char *host = “eve.kean.edu”;struct hostent *hp;…hp = gethostbyname(host);

“inet_addr()” takes an ASCII stringthat contains a dotted decimal addressand returns the equivalent IP address in binary.

Page 27: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Service Structure

• struct servent {• char *s_name; /* service name

*/• char **s_aliases; /* alias list */• int s_port; /* port number */• char *s_proto; /* protocol to

use */• };

Page 28: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

Example:

struct servent *sp;..sp = getservbyname(“smtp”, “”tcp”);

sp->s_port has the port number.

Page 29: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

The Protocol Structure

struct protoent {char *p_name; /* official protocol name */char **p_aliases; /* allowed aliases */int p_proto; /* official protocol number

*/};

Example:struct protent *pp;pp = getprotobyname(“udp”);

Page 30: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

ACCEPT

• accept(s, address, len)• Used by servers to accept the next incoming

connection.• Returns the socket descriptor of the new

socket.• Used only with TCP

Page 31: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

• s, from, to: socket descriptor• address: pointer to the struct sockaddr• len, fromlen, tolen: size of sockaddr• name: character string• protocol: character string• Qlen: integer• buffer: character array• flags: integer

Page 32: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

BIND

• Bind( s, address, len )• binds a local IP and protocol number to a

socket.• Used by servers primarily • Returns 0 if successful, or -1 in case of

error.

Page 33: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

CLOSE

• close(s)• Terminates communication and deallocates

the socket.• Returns 0 or -1

Page 34: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

CONNECT

• connect( s, address, len )• Used to specify the remote end point

address• Used with TCP and UDP• Returns 0 or -1

Page 35: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

GETHOSTBYNAME

• gethostbyname( name )• translates host name to an IP address.• Returns a pointer to a hostent structure, if

successful; otherwise it returns 0

Page 36: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

GETPROTOBYNAME

• getprotobyname( name )• Translates protocol’s name to its official

integer value.• Returns a pointer to the protoent structure;

otherwise it returns 0.

Page 37: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

LISTEN

• listen( s, Qlen)• It puts the socket in a receiving mode to

accept incoming requests.• It also sets a limit on the queue size for

incoming connection requests.• Returns 0 or -1.

Page 38: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

GETSERVBYNAME

• getservbyname( name, protocol )• Used to map a service name to a protocol

port number.• Returns a pointer to a servent structure, if

successful; otherwise it returns 0.

Page 39: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

READ

• read( s, buffer, len)• Used to get input from a socket.• Returns 0 in case of error, or the number of

bytes read in case of success.

Page 40: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

RECV

• recv( s, buffer, len, flags )• gets the next incoming message from a

socket.• Flags Control bits that specify whether to

receive out-of-band data and whether to look ahead for messages.

• Returns the number of bytes in the message, or -1 in case of error.

Page 41: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

RECVFROM

• recvfrom(s, buffer, len, flags, from,fromlen)• Gets the next message that arrives at a

socket and records the sender’s address.• Returns the number of bytes in the message,

or -1 in case of error.

Page 42: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

SELECT

• select(numfds, refds, wrfds, exfds, time);• Provides asynchronous I/O by permitting a

single process to wait for the first of any file descriptors in a specified set to become ready. The caller can also specify a maximum timeout for the wait.

Page 43: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

SELECT (Cont’d)

• numfds: number of file descriptors in set• refds: address of file descriptors for input• wrfds: address of file descriptors for output• exfds: address of file descriptors for

exceptions• time: maximum time to wait (or zero)• Returns the number of ready file

descriptors, 0 if time limit reached, or -1 for error.

Page 44: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

SEND

• send( s, msg, len, flags)• To transfer a message to another machine.• Returns the number of characters sent, or -1

in case of error.

Page 45: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

SENDTO

• sendto( s, msg, len, flags, to, tolen)• To send a message using the destination

structure to .• Returns the number of bytes sent, or -1 in

case of error.

Page 46: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

SOCKET

• socket(family, type, protocol )• To create a socket.• Returns the integer descriptor for the socket,

or -1 in case of error.

Page 47: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

WRITE

• write( s, buffer, len)• write(Allows an application to transfer data

to a remote machine.• Returns the number of bytes transferred or -

1 in case of error.

Page 48: SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in 1980. Developed at UC Berkeley Objective: to transport

References

• “Internetworking with TCP/IP, VOL III, Client-Server Programming and Applications”, D. Comer and D. Stevens, Prentice Hall