Socket Interface - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Socket Interface

Description:

The output data is looped back internally and travels back up ... Perform the same operation as sendto, but abbreviates the arguments by defining a structure ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 24
Provided by: long55
Category:

less

Transcript and Presenter's Notes

Title: Socket Interface


1
Socket Interface
2
Client-Server Architecture
  • The client is the one who speaks first
  • Typical client-server situations
  • Client and server on the same host
  • The output data is looped back internally and
    travels back up the stack as input
  • Raw performance of the client and server
    application
  • No network latency involved
  • No dropped, delayed, or out of order
  • Client and server on the same LAN
  • still nearly ideal
  • Packets are rarely lost and virtually never
    arrive out of order
  • Client and server on different LANs
  • Incur router routing and queuing problem

3
Client Server
(a) Client and server on the same host
Client
Server
(b) Client and server on the same LAN
Client
Server
router
router
router
router
WAN
(C) Client and server on different LAN
4
Basic Sockets API Review
DHCP, Mail, WWW, TELNET, FTP...
Application
Socket Library
TCP
UDP
Layer 4 / Transport
IP
ARP
RARP
ICMP
Layer 3 / Network
Ethernet
PPP
Layer 2 / Data Link
Network card
Com
Layer 1 / Physical
5
Sockets
  • process sends/receives messages to/from its
    socket
  • socket analogous to door
  • sending process shoves message out door
  • sending process relies on transport
    infrastructure on other side of door which brings
    message to socket at receiving process

controlled by app developer
Internet
6
Interacting With Protocol Software
  • Client or server uses transport protocols
  • Protocol software inside OS
  • Applications outside OS
  • Mechanism needed to bridge the two
  • Called Application Program Interface (API)
  • Part of operating system
  • Permits application to use protocols
  • Defines
  • Operations allowed
  • Arguments for each operation

7
Socket API
  • Originally designed
  • For BSD UNIX
  • To use with TCP/IP protocols
  • Now
  • Industry standard
  • Available on many operating systems

8
  • Socket
  • OS Abstraction (not hardware)
  • Created dynamically
  • Persists only while application runs
  • Referenced by a descriptor
  • Descriptor
  • Small integer
  • One per active socket
  • Used in all operations on socket
  • Generated by OS when socket created
  • Only meaningful to application that owns socket
  • In UNIX, integrated with file descriptors

9
Basic socket calls for a client
Socket()
sockaddr_in
bind()
Local addr
sockaddr_in
connect()
peer addr
recv()
send()
close()
10
Socket( ) system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ SOCKET socket(int
domain, int type, int protocol) Returns
socket descriptor on success, INVALID_SOCKET on
failure
  • The socket API is protocol independent
  • It can support several different communication
    domains
  • domain parameter
  • AF_INET (internet)
  • AF_LOCAL (or AF_UNIX) domain
  • type parameter indicates the type of socket to be
    created
  • SOCK_STREAM
  • SOCK_DGRAM
  • SOCK_RAW (access IP packet)
  • protocol field indicates which protocol should be
    used with the socket
  • In the TCP/IP, the parameter is set to zero

11
Demultiplexing
application
application
application
application
ICMP
IGMP
TCP
UDP
ARP
IP
RARP
Ethernet
incoming frame
12
bind( ) system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ int bind(SOCKET s,
const struct sockaddr name, int namelen)
Return 0 on success, SOCKET_ERROR on error
  • Specify local IP address and local port for a
    socket
  • Can use INADDR_ANY for any IP address when the
    host is multi-home host
  • s parameter
  • Socket descriptor
  • name and namelen parameters are used to supply
    the port and IP address of the local AP
  • name points to the socket address data structure
  • namelen indicate the length of socket address
    data structure

13
Sockaddr data abstraction
  • The sockaddr interface uses data abstraction.
    Thus, while the protocol domain may change, the
    interface remain the same

Struct sockaddr unsigned short int
sa_family unsigned char sa_data14
Struct sockaddr_in sa_family_t sin_family
unsigned short int sin_port
struct in_addr sin_addr unsigned
char _pad
  • Sa_family and sin_family are common between the
    two structures
  • The domain type in the socket() function must be
    the same value as the family.

14
Connect( ) system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ int connect(SOCKET s,
const struct sockaddr peer, int peer_len)
Return 0 on success, nonzero on failure
  • Used to establish the connection
  • peer parameter specifies servers address and
    port number
  • Used by client
  • Used in connection-oriented TCP Forms a TCP
    connection, server uses accept to receive the
    call
  • Used in connectionless UDP record the servers
    address in the socket.

15
send(), sendto(), sendmsg() system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ int send(SOCKET s,
void buf, size_t len, int flags) Int sendto
(SOCKET s, const void buf, size_t len, int
flags, const struct
sockaddr to, int tolen) Return 0 on
success, nonzero on failure
  • Send, sendto, and sendmsg
  • Transfer outgoing data from application
  • send () is used in the socket which is connected
  • sendto() and sendmsg() use to send a message
    using an unconnected socket
  • Sendmsg (socket, msgstruct, flags)
  • Perform the same operation as sendto, but
    abbreviates the arguments by defining a structure
  • Flags
  • MSG_OOB
  • Cause urgent data to be sent or read
  • MSG_PEEK
  • Peek at incoming data without removing it from
    the receive buffer
  • MSG_DONTROUTE
  • Cause the kernel to bypass the normal routing
    function

16
recv(), recvfrom(), recvmsg() system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ int recv (SOCKET s,
void buf, size_t len, int flags) Int recvfrom
(SOCKET s, void buf, size_t len, int
flags, struct sockaddr from, int fromlen)
Return number of bytes transferred on
success, -1 on failure
  • Recv, recvfrom, and recvmsg
  • Transfer incoming data to application
  • recv() uses to receive data from a connected
    socket
  • recvfrom() and recvmsg() is used to receive data
    from unconnected socket, receive data from
    arbitrary set of clients
  • Read and write with sockets such as read and
    write for I/O
  • Used with connected sockets
  • Read (descriptor, buffer, length)
  • Descriptor may correspond to a file or a socket
    (remote)

17
Figure 13 a simple TCP client
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • include ltnetinet/in.hgt
  • include ltarpa/inet.hgt
  • include ltstdio.hgt
  • int main( void )
  • struct sockaddr_in peer
  • int s
  • int rc
  • char buf 1
  • peer.sin_family AF_INET
  • peer.sin_port htons( 7500 )
  • peer.sin_addr.s_addr inet_addr( "127.0.0.1" )
  • s socket( AF_INET, SOCK_STREAM, 0 )
  • if ( s lt 0 )

18
Figure 13 a simple TCP client (cont)
  • rc connect( s, ( struct sockaddr )peer,
    sizeof( peer ) )
  • if ( rc )
  • perror( "connect call failed" )
  • exit( 1 )
  • rc send( s, "1", 1, 0 )
  • if ( rc lt 0 )
  • perror( "send call failed" )
  • exit( 1 )
  • rc recv( s, buf, 1, 0 )
  • if ( rc lt 0 )
  • perror( "recv call failed" )
  • else
  • printf( "c\n", buf 0 )
  • exit( 0 )

19
Basic socket calls in a server
Socket()
sockaddr_in
bind()
Local addr
listen()
sockaddr_in
accept()
peer addr
recv()
send()
close()
20
listen() system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ int listen(SOCKET s,
int backlog) Return 0 on success,
SOCKET_ERROR on error
  • Used by server, TCP is in passive mode, UDP
    server dont require
  • Prepares socket to accept incoming connections
  • backlog parameter is the length of the server
    request queue (connection request queue)

21
accept() system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ SOCKET accept(SOCKET
s, struct sockaddr addr, int addrlen)
Return A connected socket if OK, INVALID_SOCKET
on failure
  • Used by TCP server
  • Waits for next connection establish and returns
    new socket
  • Use the newsock to communication with this client
  • Returns the address of the new connections peer
    in the sockaddr_in structure pointed to by addr

22
A simpler TCP server
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • include ltnetinet/in.hgt
  • include ltstdio.hgt
  • int main( void )
  • struct sockaddr_in local
  • int s
  • int s1
  • int rc
  • char buf 1
  • local.sin_family AF_INET
  • local.sin_port htons( 7500 )
  • local.sin_addr.s_addr htonl( INADDR_ANY )
  • s socket( AF_INET, SOCK_STREAM, 0 )
  • if ( s lt 0 )

23
A simpler TCP server (cont)
  • rc bind( s, ( struct sockaddr )local,
    sizeof( local ) )
  • if ( rc lt 0 )
  • perror( "bind call failure" )
  • exit( 1 )
  • rc listen( s, 5 )
  • if ( rc )
  • perror( "listen call failed" )
  • exit( 1 )
  • s1 accept( s, NULL, NULL )
  • if ( s1 lt 0 )
  • perror( "accept call failed" )
  • exit( 1 )
  • rc recv( s1, buf, 1, 0 )
  • if ( rc lt 0 )
  • perror( "recv call failed" )
  • exit( 1 )
Write a Comment
User Comments (0)
About PowerShow.com