outline
play

Outline ! Socket basics ! TCP sockets ! Socket details ! Socket - PDF document

Outline ! Socket basics ! TCP sockets ! Socket details ! Socket options Operating Systems ! Final notes ! Project 3 Sockets Socket Basics Ports ! An end-point for a IP network connection ! Numbers (vary in BSD, Solaris): what the


  1. Outline ! Socket basics ! TCP sockets ! Socket details ! Socket options Operating Systems ! Final notes ! Project 3 Sockets Socket Basics Ports ! An end-point for a IP network connection ! Numbers (vary in BSD, Solaris): – what the application layer “plugs into” – 0-1023 “reserved”, must be root – programmer cares about Application – 1024 - 5000 “ephemeral” Programming Interface (API) – however, many systems allow > 5000 ports ! End-point determined by two things: N (50,000 is correct number) – Host address (IP address) - name of machine ! /etc/services: – Port number - location of process ftp 21/tcp ! Two end-points determine a connection: telnet 23/tcp socket pair finger 79/tcp – ex: 206.62.226.35,p21 + 198.69.10.2,p1500 snmp 161/udp – ex: 206.62.226.35,p21 + 198.69.10.2,p1499 Network Communication Sockets and the OS ! UDP : User Datagram Protocol – no acknowledgements User – no retransmissions Socket – out of order, duplicate possible Operating System – connectionless (Transport Layer) ! TCP : Transmission Control Protocol – reliable (in order, all arrive, no duplicates) ! User sees “descriptor”, integer index – flow control – like: FILE * , or file index – connection – returned by socket() call (more later) – duplex – (Project 3 uses TCP) 1

  2. Socket Details Addresses and Sockets ! Structure to hold address information Unix Network Programming , W. Richard Stevens, 2nd edition,  1998, Prentice Hall ! Functions pass address from app to OS – bind() – connect() ! Socket address structure – sendto() ! TCP client-server ! Functions pass address from OS to app ! Misc stuff – accept() – setsockopt(), getsockopt() – recvfrom() – fcntl() Server TCP Client-Server Socket Address Structure socket() “well-known” bind() port struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 addresses */ listen() Client }; accept() struct sock_ addr_in { socket() (Block until connection ) “Handshake” unit8_t sin_ len; /* length of structure */ connect() sa_family_t sin_family; /* AF_INET */ Data (request) send() in_port_t sin_port; /* TCP/UDP Port num */ recv() struct in_ addr sin_addr; /* IPv4 address */ Data (reply) send() char sin_zero[8]; /* unused */ recv() } End-of-File close() recv() ! Are also “generic” and “IPv6” socket structures close() socket() Server Functions int socket(int family , int type , int protocol ); socket() Create a socket, giving access to transport layer service. “well-known” bind() port ! family is one of listen() – AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix), – AF_ROUTE (access to routing tables), AF_KEY (new, for encryption) accept() ! type is one of (Block until connection ) “Handshake” – SOCK_STREAM (TCP), SOCK_DGRAM (UDP) – SOCK_RAW (for special IP packets, PING, etc. Must be root) Data (request) N setuid bit ( -rws--x--x root 1997 / sbin/ping*) recv() ! protocol is 0 (used for some raw socket options) Data (reply) send() ! upon success returns socket descriptor End-of-File – similar to a file descriptor or semaphore id recv() – returns -1 if failure close() 2

  3. bind() listen() int bind(int sockfd , const struct sockaddr * myaddr , socklen_t addrlen ); Assign a local protocol address (“name”) to a socket . int listen(int sockfd , int backlog ); ! sockfd is socket descriptor from socket() Change socket state for TCP server. ! myaddr is a pointer to address struct with: ! sockfd is socket descriptor from socket() – port number and IP address ! backlog is maximum number of incomplete – if port is 0, then host will pick ephemeral port connections N not usually for server (exception RPC port-map) – historically 5 – IP address != INADDR_ANY (multiple nics) – rarely above 15 on a even moderate web server! ! addrlen is length of structure ! Sockets default to active (for client) ! returns 0 if ok, -1 on error – change to passive to OS will accept connection – EADDRINUSE (“Address already in use”) close() accept() int close(int sockfd) ; int accept(int sockfd , struct sockaddr cliaddr , socklen_t * addrlen ); Close socket for use. Return next completed connection. ! sockfd is socket descriptor from socket() ! sockfd is socket descriptor from socket() ! closes socket for reading/writing ! cliaddr and addrlen return protocol address – returns (doesn’t block) from client – attempts to send any unsent data ! returns brand new descriptor, created by OS – socket option SO_LINGER ! if used with fork(), can create N block until data sent N or discard any remaining data concurrent server (more later) – -1 if error connect() Client functions int connect(int sockfd , const struct sockaddr * servaddr , socklen_t addrlen ); Connect to server. ! sockfd is socket descriptor from socket() Client ! servaddr is a pointer to a structure with: socket() “Handshake” – port number and IP address connect() – must be specified (unlike bind() ) Data (request) send() ! addrlen is length of structure Data (reply) ! client doesn’t need bind() recv() – OS will pick ephemeral port End-of-File close() ! returns socket descriptor if ok, -1 on error 3

  4. Sending and Receiving Socket Options ! setsockopt (), getsockopt () int recv(int sockfd , void * buff , size_t mbytes , int flags ); ! SO_LINGER – upon close, discard data or block until sent int send(int sockfd , void * buff , size_t mbytes , int flags ); ! SO_RCVBUF, SO_SNDBUF – change buffer sizes ! Same as read() and write() but for flags – for TCP is “pipeline”, for UDP is “discard” – MSG_DONTWAIT (this send non-blocking) ! SO_RCVLOWAT, SO_SNDLOWAT – MSG_OOB (out of band data, 1 byte sent ahead) – how much data before “readable” via select () – MSG_PEEK (look, but don’t remove) ! SO_RCVTIMEO, SO_SNDTIMEO – MSG_WAITALL (don’t give me less than max ) – timeouts – MSG_DONTROUTE (bypass routing table) fcntl() Socket Options (TCP) ! ‘File control’ but used for sockets, too ! TCP_KEEPALIVE ! Signal driven sockets – idle time before close (2 hours, default) ! Set socket owner ! TCP_MAXRT ! Get socket owner – set timeout value ! Set socket non-blocking ! TCP_NODELAY flags = fcntl(sockfd, F_GETFL, 0); – disable Nagle Algorithm flags |= O_NONBLOCK; fcntl(sockfd, F_SETFL, flags); ! Beware not getting flags before setting! ! (Should not need for project 3) Concurrent Servers Project 3: Macro Shell Text segment Parent sock = socket() ! Distributed Shell (1) connect /* setup socket */ int sock; ! Client/Server while (1) { Server Client int newsock; (2) ls newsock = accept(sock) ! Non-interactive fork() (3) fork() – command line args and exec() if child Child (4) data read(newsock) – get-opt.c int sock; until exit Server int newsock; ! Uses TCP sockets } – listen.c and talk.c ! Close sock in child, newsock in parent ! Security – password ! Reference count for socket descriptor 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend