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

Slides:



Advertisements
Similar presentations
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Advertisements

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.
Distributed Computing Systems Sockets. 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.
Sockets Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
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.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
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,
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Elementary TCP Sockets
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
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.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
 Wind River Systems, Inc Chapter - 13 Network Programming.
Review: How to create a TCP end point? What is the right format for sin_port and sin_addr in the sockaddr_in data structure? How many different ways we.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
Lecture 15 Overview.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
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.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
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.
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.
Elementary UDP Sockets
Socket Programming in C
Transport layer API: Socket Programming
Chapter 2 Transport Layer Protocols TCP UDP SCTP
Network Programming CSC- 341
UDP Sockets Programming
Socket Programming in C
Lecture 11 Overview.
TCP Sockets Programming
Advanced Network Programming spring 2007
TCP/IP Socket Programming in C
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Today’s topic: Basic TCP API
Presentation transcript:

Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems 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 2 CPE 401/601 Lecture 3 : TCP Socket Programming

TCP state diagram

Creating a TCP socket 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 */ } 4 CPE 401/601 Lecture 3 : 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)); 5 CPE 401/601 Lecture 3 : TCP Socket Programming

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

listen() int listen(int sockfd, int backlog); sockfd is the TCP socket – already bound to an address backlog is the max number of buffered incoming connections – incomplete and completed handshakes When app is ready to handle a new connection – we need to ask the O.S. for the next connection 7 CPE 401/601 Lecture 3 : TCP Socket Programming

accept() 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 – will be set to the number of used bytes in cliaddr 8 CPE 401/601 Lecture 3 : 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! 9 CPE 401/601 Lecture 3 : 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 10 CPE 401/601 Lecture 3 : 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 11 CPE 401/601 Lecture 3 : TCP Socket Programming

connect() 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) 12 CPE 401/601 Lecture 3 : 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! 13 CPE 401/601 Lecture 3 : 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 14 CPE 401/601 Lecture 3 : 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 15 bind() accept() connect() check for errors read might return 1 byte *Copyright Dr. Laura’s Network Programming Corp. CPE 401/601 Lecture 3 : TCP Socket Programming

UDP Sockets

UDP Sockets Programming Creating UDP sockets – Client – Server Sending data Receiving data Connected Mode 17 CPE 401/601 Lecture 3: 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 */ } 18 CPE 401/601 Lecture 3: 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)); 19 CPE 401/601 Lecture 3: UDP Socket Programming

Sending UDP Datagrams 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. 20 CPE 401/601 Lecture 3: 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! 21 CPE 401/601 Lecture 3: 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 22 CPE 401/601 Lecture 3: 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 23 CPE 401/601 Lecture 3: 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 24 CPE 401/601 Lecture 3: 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 25 CPE 401/601 Lecture 3: 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() 26 CPE 401/601 Lecture 3: UDP Socket Programming

Typical UDP Communication 27 CPE 401/601 Lecture 3: UDP Socket Programming

UDP Echo Server 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); } 28 NEED TO CHECK FOR ERRORS!!! CPE 401/601 Lecture 3: UDP Socket Programming