Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.

Slides:



Advertisements
Similar presentations
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Advertisements

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
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.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Computer Networks Sockets.
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 IMGD 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
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Unix Network Programming Part 2: Elementary Sockets Jani Peusaari.
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.
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.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
ECE 4110 – Internetwork Programming Client-Server Model.
Elementary TCP Sockets
Client-Side Network Programming
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.
Introduction to Socket Programming Advisor: Quincy Wu Speaker: Kuan-Ta Lu Date: Nov. 25, 2010.
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.
1 CMPT 471 Networking II Transport Layer Network Programming © Janice Regan, 2013.
Lecture 15 Overview.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Introduction to Socket
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
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.
Review: – Why layer architecture? – peer entities – Protocol and service interface – Connection-oriented/connectionless service – Reliable/unreliable service.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Advanced UNIX programming Fall 2002 Instructor: Ashok Srinivasan Lecture 17 Acknowledgements: The syllabus and power point presentations are modified versions.
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.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Operating Systems Sockets ENCE 360.
Socket Programming (Cont.)
Network Programming CSC- 341
Chapter4 Elementary TCP Socket
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
Chapter 3 Sockets Introduction
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Network Programming CSC- 341
Introduction to Socket Programming
TCP Sockets Programming
Advanced Network Programming spring 2007
TCP/IP Socket Programming in C
Lecture 2 Socket Programming
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close

Basic connection-oriented client/server application Server gets ready to service clients –Creates a socket –Binds a local address (port) to the socket Server publishes the local address (port) so clients know who to talk to Client contacts the server –Creates a socket –Connects to the server Client has to supply the address of the server Server accepts connection requests Further communication is specific to the application.

A basic sequential server

Socket: #include int socket(int family, int type, int protocol); Family: AF_INET, AF_INET6, AF_LOCAL, AF_ROUTE, AF_KEY. Type: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW –TCP: AF_INET, SOCK_STREAM –UDP: AF_INET, SOCK_DGRAM –IPv4: AF_INET, SOCK_RAW Protocol: usually 0 except for raw sockets Return descriptor (like file descriptor), -1 on error. –AF_XXX and PF_XXX

Connect: #include int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); Servaddr: socket address structure (ip address and port) –Connect actively starts the TCP connection establishment phase. Possible errors: –No response: retry a number of times before it quits. –Get response RST (not SYN/ACK), ECONNREFUSED, nobody is listening in that port –Get ICMP response, keep trying, EHOSTUNREACH or ENETUNREACH.

Socket Address structure: struct in_addr { in_addr_t s_addr; } 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]; } Always use sockaddr_in type for manipulation and convert it to sockaddr. See example1.c. struct sockaddr { uint8_t sa_len; sa_family_t sa_family; char sa_data[14]; }

Bind –Client does not have to bind, system assigns a dynamic port number. #include Int bind(int sockfd, const struct sockaddr &myaddr, socklen_t addlen); Myaddr constains its own address. Address port result INADDR_ANY 0 system selects addr and port INADDR_ANY !=0 system selects addr, user selects port Local IP address 0 user selects addr, system selects port Local IP address !=0 user selects both addr and port See example2.c

Listen –Convert a socket into a passive socket #include int listen(int sockfd, int backlog) Backlog: number of pending connections that the kernel should queue for the socket. –The socket will refuse connections if the queue is full. Backlog not well defined. E.g: backlog= 5: AIX 4.2(8), Linux2.0.27(5), SunOS4.1.4(8), Solaris2.5.1(6).

Accept: Blocking by default #include int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); Return client’s address in cliaddr See example2.c

Network Byte Order What happen when we run example2.c as server on program and example1.c as client on linprog? –Sockaddr_in revisit sin_port and sin_addr must be in network byte order.

Some useful functions converting the byte orders #include uint16_t htons(uint16_t host16bitvalue); uint32_t htonl(uint32_t host32bitvalue); uint16_t ntohs(uint16_t net16bitvalue); uint32_t ntohl(uint32_t net32bitvalue); Byte manipulations #include void *memset(void *dst, int c, size_t len); void *memcpy(void *dst, void *src, size_t nbytes); void *memcmp(const void *ptr1, const void *ptr2, size_t nbytes);

Address conversion functions inet_aton/inet_addr/inet_ntoa Information about socket –getsockname and getpeername –getsockopt and setsockopt Name and address conversions –gethostbyname, gethostbyaddr –getservbyname, getservbyport –getaddrinfo, gai_strerror, freeaddrinfo

Echo client and server See echo_client.cpp and echo_server.cpp