outline
play

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

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


  1. Outline ! Socket basics ! TCP sockets ! Socket details ! Socket options Computer Networks ! Final notes 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 > 3977 ports ! End point determined by two things: N (50,000 is correct number) – Host address: IP address is Network Layer ! /etc/services: – Port number: is Transport Layer 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 Transport Layer Sockets and the OS ! UDP: User Datagram Protocol User – no acknowledgements Socket – no retransmissions Operating System – out of order, duplicate possible (Transport Layer) – connectionless ! 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 – (proj 2) 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 user to OS – bind() – connect() ! Socket address structure – sendto() ! TCP client-server ! Functions pass address from OS to user ! 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() bind() int socket(int family , int type , int protocol ); int bind(int sockfd , const struct sockaddr * myaddr , socklen_t addrlen ); Create a socket, giving access to transport layer service. Assign a local protocol address (“name”) to a socket . ! family is one of ! sockfd is socket descriptor from socket() – AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix), ! myaddr is a pointer to address struct with: – AF_ROUTE (access to routing tables), AF_KEY (new, for encryption) – port number and IP address ! type is one of – SOCK_STREAM (TCP), SOCK_DGRAM (UDP) – if port is 0, then host will pick ephemeral port – SOCK_RAW (for special IP packets, PING, etc. Must be root) N not usually for server (exception RPC port-map) N setuid bit ( -rws--x--x root 1997 /sbin/ping*) – IP address != INADDR_ANY (multiple net cards) ! protocol is 0 (used for some raw socket options) ! addrlen is length of structure ! upon success returns socket descriptor ! returns 0 if ok, -1 on error – like file descriptor – EADDRINUSE (“Address already in use”) – -1 if failure 2

  3. accept() listen() int accept(int sockfd , struct sockaddr cliaddr , int listen(int sockfd , int backlog ); socklen_t * addrlen ); Change socket state for TCP server. Return next completed connection. ! sockfd is socket descriptor from socket() ! sockfd is socket descriptor from socket() ! backlog is maximum number of incomplete ! cliaddr and addrlen return protocol address connections from client – historically 5 ! returns brand new descriptor, created by OS – rarely above 15 on a even moderate web server! ! if used with fork(), can create ! Sockets default to active (for client) concurrent server (more later) – change to passive to OS will accept connection connect() close() int connect(int sockfd , const struct sockaddr int close(int sockfd) ; * servaddr , socklen_t addrlen ); Connect to server. Close socket for use. ! sockfd is socket descriptor from socket() ! sockfd is socket descriptor from socket() ! servaddr is a pointer to a structure with: ! closes socket for reading/writing – port number and IP address – returns (doesn’t block) – must be specified (unlike bind() ) – attempts to send any unsent data ! addrlen is length of structure – socket option SO_LINGER ! client doesn’t need bind() N block until data sent – OS will pick ephemeral port N or discard any remaining data – -1 if error ! returns socket descriptor if ok, -1 on error Sending and Receiving Socket Options ! setsockopt (), getsockopt () int recv(int sockfd , void * buff , ! SO_LINGER size_t mbytes , int flags ); – upon close, discard data or block until sent int send(int sockfd , void * buff , ! SO_RCVBUF, SO_SNDBUF size_t mbytes , int flags ); – 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) 3

  4. 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! Concurrent TCP Server UDP Client-Server Server Text segment socket() Parent sock = socket() /* setup socket */ “well-known” int sock; bind() Client port while (1) { int newsock; newsock = accept(sock) recvfrom() socket() fork() (Block until receive datagram) Data (request) sendto() if child Child read(newsock) int sock; until exit sendto() int newsock; recvfrom() Data (reply) } close() - No “handshake” ! Close sock in child, newsock in parent - No simultaneous close() - Note, usually fork() for concurrent servers! ! Reference count for socket descriptor Called iterative server Sending and Receiving connect() with UDP int recvfrom(int sockfd , void * buff , size_t mbytes , int flags, struct sockaddr *from, socklen _ t *addrlen ); int sendto(int sockfd , void * buff , size_t mbytes , int ! Record address and port of peer flags, const struct sockaddr * to , socklen_t addrlen ); – datagrams to/from others are not allowed ! Same as recv() and send() but for addr – does not do three way handshake, or connection – recvfrom fills in address of where packet came – connect a misnomer, here. Should be from setpeername () – sento requires address of where sending packet to ! Use send () instead of sendto () ! Use recv () instead of recvfrom () ! Can change connect (or unconnect) by repeating connect () call 4

  5. Why else about connected UDP? Why use connected UDP? ! Consider client: ! Send two datagrams ! Send two datagrams sendto() unconnected: connected: recvfrom() – connect the socket – connect the socket – NULL in recvfrom args could come from other – output first dgram – output first dgram – could fill in, or done in connect() – unconnect the socket – output second dgram ! Asynchronous errors not returned – connect the socket – talk.udp reno // server down, unconncected – output second dgram – unconnect the socket – hey! // no response, error not returned to process vs. – talk.udp reno // server down, conncected – port unreachable // error returned Mcast is Group and Port Mcast Extensions to UDP Client Server join Application Application socket() Sendto socket() host = 224.0.1.1 setsockopt() port = 123 port=123 Frames UDP UDP sendto() recvfrom() host=224.0.1.1 IP IP Other pause play clients Data Link Data Link LAN - sendto() in server to multicast group to router - setsockopt() in client to join group IP_ADD_MEMBERSHIP sendto() and recvfrom() specify port and group Scope of IPv4 Addresses Scope TTL Addr 0 node 1 224.0.0.0 to 224.0.0.255 link <32 239.255.0.0 to 239.255.255.255 site <32 239.192.0.0 to 239.195.255.255 org <255 224.0.1.0 to 238.255.255.255 global ! Use “link-local” addresses – only 255, but port numbers also unique ! On garden , ernie and bert 5

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