Lecture 15 Overview.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
CSCE 515: Computer Network Programming TCP Details Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Lecture 16 Overview. Creating a TCP socket int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); int mysock; struct sockaddr_in myaddr;
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
CSCE 515: Computer Network Programming Socket Wenyuan Xu Department of Computer Science and Engineering.
Tutorial 8 Socket Programming
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Lecture 8 UDP Sockets & I/O Multiplexing
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
ECE 4110 – Internetwork Programming Client-Server Model.
Elementary TCP Sockets
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
 Wind River Systems, Inc Chapter - 13 Network Programming.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
UNIT8: SOCKETS Topics Introduction to sockets Socket Addresses
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Sockets Introduction Socket address structures Value-result arguments
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Network Programming Berkeley Sockets (BSD)
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
1 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
Chapter 2 Transport Layer Protocols TCP UDP SCTP
Network Programming CSC- 341
Lecture 4 Socket Programming Issues
UDP Sockets Programming
Socket Programming in C
Lecture 11 Overview.
TCP Sockets Programming
Advanced Network Programming spring 2007
TCP/IP Socket Programming in C
Lecture 2 Socket Programming
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Today’s topic: Basic TCP API
Presentation transcript:

Lecture 15 Overview

Network API Application Programming Interface Services that provide the interface between application and protocol software often by the operating system Application Network API Protocol A Protocol B Protocol C CPE 401/601 Lecture 15 : Socket Programming

Network API wish list Generic Programming Interface Support multiple communication protocol suites (families) Address (endpoint) representation independence Provide special services for Client and Server? Support for message oriented and connection oriented communication Work with existing I/O services when this makes sense Operating System independence CPE 401/601 Lecture 15 : Socket Programming

Socket A socket is an abstract representation of a communication endpoint Sockets work with Unix I/O services just like files, pipes & FIFOs Sockets needs to establishing a connection specifying communication endpoint addresses CPE 401/601 Lecture 15 : Socket Programming

Creating a Socket int socket(int family, int type, int proto); family specifies the protocol family PF_INET for TCP/IP type specifies the type of service SOCK_STREAM, SOCK_DGRAM protocol specifies the specific protocol usually 0, which means the default CPE 401/601 Lecture 15 : Socket Programming

socket() The socket() system call returns a socket descriptor (small integer) or -1 on error socket() allocates resources needed for a communication endpoint but it does not deal with endpoint addressing CPE 401/601 Lecture 15 : Socket Programming

Specifying an Endpoint Address Sockets API is generic There must be a generic way to specify endpoint addresses TCP/IP requires an IP address and a port number for each endpoint address Other protocol suites (families) may use other schemes CPE 401/601 Lecture 15 : Socket Programming

sockaddr sockaddr_in6 sockaddr sockaddr_in sa_len length AF_INET6 sa_family sa_data length AF_INET port addr zero sockaddr sockaddr_in sockaddr_in6 AF_INET6 Flow-label Scope ID 28 bytes variable 16 bytes CPE 401/601 Lecture 15 : Socket Programming

struct sockaddr_in (IPv4) struct sockaddr_in { uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8]; }; struct in_addr { in_addr_t s_addr; Length of structure (16) AF_INET 16 bit Port number Make structure 16 bytes 32 bit IPv4 address CPE 401/601 Lecture 15 : Socket Programming

struct sockaddr_in (IPv6) struct sockaddr_in6 { uint8_t sin6_len; sa_family_t sin6_family; in_port_t sin6_port; uint32_t sin6_flowinfo; struct in6_addr sin6_addr; uint32_t sin6_scope_id; }; struct in6_addr { uint8_t s6_addr[16]; Length of structure (28) AF_INET6 Port number Flow label Scope of address 128 bit IPv6 address CPE 401/601 Lecture 15 : Socket Programming

Network Byte Order All values stored in a sockaddr_in must be in network byte order. sin_port a TCP/IP port number sin_addr an IP address uint16_t htons(uint16_t); uint16_t ntohs(uint_16_t); uint32_t htonl(uint32_t); uint32_t ntohl(uint32_t); CPE 401/601 Lecture 15 : Socket Programming

TCP/IP Addresses We don’t need to deal with sockaddr structures since we will only deal with a real protocol family. We can use sockaddr_in structures BUT: The C functions that make up the sockets API expect structures of type sockaddr CPE 401/601 Lecture 15 : Socket Programming

Assigning an address to a socket The bind() system call is used to assign an address to an existing socket. int bind( int sockfd, const struct sockaddr *myaddr, int addrlen); You can give bind() a sockaddr_in structure: int bind( mysock, (struct sockaddr*) &myaddr, sizeof(myaddr) ); CPE 401/601 Lecture 15 : Socket Programming

bind() Example int mysock,err; struct sockaddr_in myaddr; mysock = socket(PF_INET,SOCK_STREAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons( portnum ); myaddr.sin_addr = htonl( ipaddress); err=bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr)); CPE 401/601 Lecture 15 : Socket Programming

Port schmort - who cares ? Clients typically don’t care what port they are assigned When you call bind you can tell it to assign you any available port: myaddr.port = htons(0); CPE 401/601 Lecture 15 : Socket Programming

What is my IP address ? How can you find out what your IP address is so you can tell bind() ? There is no realistic way for you to know the right IP address to give bind() what if the computer has multiple network interfaces? specify the IP address as: INADDR_ANY, this tells the OS to take care of things. CPE 401/601 Lecture 15 : Socket Programming

IPv4 Address Conversion int inet_aton(char *, struct in_addr *); Convert ASCII dotted-decimal IP address to network byte ordered 32 bit value. Returns 1 on success, 0 on failure. char *inet_ntoa(struct in_addr); Convert network byte ordered value to ASCII dotted-decimal (a string). CPE 401/601 Lecture 15 : Socket Programming

IPv4 & IPv6 Address Conversion int inet_pton(int, const char*, void*); (family, string_ptr, address_ptr) Convert IP address string to network byte ordered 32 or 128 bit value 1 on success, -1 on failure, 0 on invalid input char *inet_ntop(int, const void*, char*, size_t); (family, address_ptr, string_ptr, length) Convert network byte ordered value to IP address string x:x:x:x:x:x:x:x or x:x:x:x:x:x:a.b.c.d CPE 401/601 Lecture 15 : Socket Programming

Other socket system calls General Use read() write() close() Connection-oriented (TCP) connect() listen() accept() Connectionless (UDP) send() recv() CPE 401/601 Lecture 15 : Socket Programming

Lecture 16 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger slides are modified from Dave Hollinger

TCP Sockets Programming Creating a passive mode (server) socket Establishing an application-level connection send/receive data Terminating a connection CPE 401/601 Lecture 16 : TCP Socket Programming

TCP state diagram

Creating a TCP socket family: AF_INET, AF_INET6, AF_LOCAL, … int socket(int family,int type,int proto); family: AF_INET, AF_INET6, AF_LOCAL, … type: SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET, SOCK_RAW protocol: IPPROTO_TCP, IPPROTO_UDP, IPPROTO_SCTP int sock; sock = socket(PF_INET, SOCK_STREAM, 0); if (sock<0) { /* ERROR */ } CPE 401/601 Lecture 16 : TCP Socket Programming

Binding to well known address int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); int mysock; struct sockaddr_in myaddr; mysock = socket(PF_INET,SOCK_STREAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons( 80 ); myaddr.sin_addr = htonl( INADDR_ANY ); bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr)); CPE 401/601 Lecture 16 : TCP Socket Programming

Establishing a passive mode TCP socket Address already determined Tell the kernel to accept incoming connection requests directed at the socket address 3-way handshake Tell the kernel to queue incoming connections for us CPE 401/601 Lecture 16 : TCP Socket Programming

listen() sockfd is the TCP socket int listen(int sockfd, int backlog); sockfd is the TCP socket already bound to an address backlog is the number of incoming connections the kernel should be able to keep track of (queue for us) Sum of incomplete and completed queues CPE 401/601 Lecture 16 : TCP Socket Programming

Accepting an incoming connection Once we call listen(), the O.S. will queue incoming connections Handles the 3-way handshake Queues up multiple connections When our application is ready to handle a new connection we need to ask the O.S. for the next connection CPE 401/601 Lecture 16 : TCP Socket Programming

accept() sockfd is the passive mode TCP socket int accept( int sockfd, struct sockaddr* cliaddr, socklen_t *addrlen); sockfd is the passive mode TCP socket initiated by socket(), bind(), and listen() cliaddr is a pointer to allocated space addrlen is a value-result argument must be set to the size of cliaddr on return, will be set to be the number of used bytes in cliaddr CPE 401/601 Lecture 16 : TCP Socket Programming

accept() return value accept() returns a new socket descriptor (small positive integer) or -1 on error After accept returns a new socket descriptor, I/O can be done using the read() and write() system calls read() and write() operate a little differently on sockets! vs. file operation! CPE 401/601 Lecture 16 : TCP Socket Programming

Terminating a TCP connection int close(int sockfd); Either end of the connection can call the close() system call What if there is data being sent? If the other end has closed the connection, and there is no buffered data, reading from a TCP socket returns 0 to indicate EOF. CPE 401/601 Lecture 16 : TCP Socket Programming

Client Code TCP clients can call connect() which: takes care of establishing an endpoint address for the client socket Attempts to establish a connection to the specified server 3-way handshake no need to call bind first, the O.S. will take care of assigning the local endpoint address TCP port number, IP address CPE 401/601 Lecture 16 : TCP Socket Programming

connect() sockfd is an already created TCP socket int connect( int sockfd, const struct sockaddr *server, socklen_t addrlen); sockfd is an already created TCP socket server contains the address of the server connect() returns 0 if OK, -1 on error No response to SYN segment (3 trials) RST signal ICMP destination unreachable (3 trials) CPE 401/601 Lecture 16 : TCP Socket Programming

Reading from a TCP socket int read(int fd, char *buf, int max); By default read() will block until data is available reading from a TCP socket may return less than max bytes whatever is available You must be prepared to read data 1 byte at a time! CPE 401/601 Lecture 16 : TCP Socket Programming

Writing to a TCP socket int write(int fd, char *buf, int num); write might not be able to write all num bytes on a nonblocking socket readn(), writen() and readline() functions CPE 401/601 Lecture 16 : TCP Socket Programming

Metaphor for Good Relationships To succeed in relationships*: you need to establish your own identity. you need to be open & accepting. you need to establish contacts. you need to take things as they come, not as you expect them. you need to handle problems as they arise. bind() accept() connect() read might return 1 byte check for errors *Copyright Dr. Laura’s Network Programming Corp. CPE 401/601 Lecture 16 : TCP Socket Programming

UDP Sockets

UDP Sockets Programming Creating UDP sockets Client Server Sending data Receiving data Connected Mode CPE 401/601 Lecture 16 : UDP Socket Programming

Creating a UDP socket int socket(int family, int type, int proto); int sock; sock = socket(PF_INET, SOCK_DGRAM, 0); if (sock<0) { /* ERROR */ } CPE 401/601 Lecture 16 : UDP Socket Programming

Binding to well known address typically done by server only int mysock; struct sockaddr_in myaddr; Mysock=socket(PF_INET,SOCK_DGRAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons(1234); myaddr.sin_addr = htonl(INADDR_ANY); bind(mysock, &myaddr, sizeof(myaddr)); CPE 401/601 Lecture 16 : UDP Socket Programming

Sending UDP Datagrams sockfd is a UDP socket ssize_t sendto( int sockfd, void *buff, size_t nbytes, int flags, const struct sockaddr* to, socklen_t addrlen); sockfd is a UDP socket buff is the address of the data (nbytes long) to is the destination address Return value is the number of bytes sent, or -1 on error. CPE 401/601 Lecture 16 : UDP Socket Programming

sendto() The return value of sendto() indicates how much data was accepted by the O.S. for sending as a datagram not how much data made it to the destination. There is no error condition that indicates that the destination did not get the data!!! You can send 0 bytes of data! CPE 401/601 Lecture 16 : UDP Socket Programming

Receiving UDP Datagrams ssize_t recvfrom( int sockfd, void *buff, size_t nbytes, int flags, struct sockaddr* from, socklen_t *fromaddrlen); sockfd is a UDP socket buff is the address of a buffer (nbytes long) from is the address of a sockaddr Return value is the number of bytes received and put into buff, or -1 on error CPE 401/601 Lecture 16 : UDP Socket Programming

recvfrom() If buff is not large enough, any extra data is lost forever... You can receive 0 bytes of data! The sockaddr at from is filled in with the address of the sender CPE 401/601 Lecture 16 : UDP Socket Programming

More recvfrom() recvfrom doesn’t return until there is a datagram available, unless you do something special You should set fromaddrlen before calling If from and fromaddrlen are NULL we don’t find out who sent the data CPE 401/601 Lecture 16 : UDP Socket Programming

Typical UDP client code Create UDP socket Create sockaddr with address of server Call sendto(), sending request to the server No call to bind() is necessary! Possibly call recvfrom() if we need a reply CPE 401/601 Lecture 16 : UDP Socket Programming

Typical UDP Server code Create UDP socket and bind to well known address Call recvfrom() to get a request, noting the address of the client Process request and send reply back with sendto() CPE 401/601 Lecture 16 : UDP Socket Programming

Typical UDP Communication CPE 401/601 Lecture 16 : UDP Socket Programming

UDP Echo Server NEED TO CHECK FOR ERRORS!!! int mysock; struct sockaddr_in myaddr, cliaddr; char msgbuf[MAXLEN]; socklen_t clilen; int msglen; mysock = socket(PF_INET,SOCK_DGRAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons( S_PORT ); myaddr.sin_addr = htonl( INADDR_ANY ); bind(mysock, &myaddr, sizeof(myaddr)); while (1) { clilen=sizeof(cliaddr); msglen=recvfrom(mysock,msgbuf,MAXLEN,0, cliaddr,&clilen); sendto(mysock,msgbuf,msglen,0,cliaddr,clilen); } NEED TO CHECK FOR ERRORS!!! CPE 401/601 Lecture 16 : UDP Socket Programming

Debugging Debugging UDP can be difficult Write routines to print out sockaddrs Use trace, strace, ptrace, truss, etc Include code that can handle unexpected situations CPE 401/601 Lecture 16 : UDP Socket Programming

Timeout when calling recvfrom() It might be nice to have each call to recvfrom() return after a specified period of time even if there is no incoming datagram We can do this by using SIGALRM and wrapping each call to recvfrom() with a call to alarm() CPE 401/601 Lecture 16 : UDP Socket Programming

recvfrom()and alarm() signal(SIGALRM, sig_alrm); alarm(max_time_to_wait); if (recvfrom(…)<0) if (errno==EINTR) /* timed out */ else /* some other error */ /* no error or time out - turn off alarm */ alarm(0); There are some other (better) ways to do this CPE 401/601 Lecture 16 : UDP Socket Programming

Connected mode A UDP socket can be used in a call to connect() This simply tells the O.S. the address of the peer No handshake is made to establish that the peer exists No data of any kind is sent on the network as a result of calling connect() on a UDP socket CPE 401/601 Lecture 16 : UDP Socket Programming

Connected UDP Once a UDP socket is connected: can use sendto() with a null dest address can use write() and send() can use read() and recv() only datagrams from the peer will be returned Asynchronous errors will be returned to the process OS Specific, some won’t do this! CPE 401/601 Lecture 16 : UDP Socket Programming

Asynchronous Errors What happens if a client sends data to a server that is not running? ICMP “port unreachable” error is generated by receiving host and sent to sending host The ICMP error may reach the sending host after sendto() has already returned! The next call dealing with the socket could return the error CPE 401/601 Lecture 16 : UDP Socket Programming

Back to UDP connect() Connect() is typically used with UDP when communication is with a single peer only. It is possible to disconnect and connect the same socket to a new peer More efficient to send multiple datagrams to the same user Many UDP clients use connect() Some servers (TFTP) CPE 401/601 Lecture 16 : UDP Socket Programming