1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.

Slides:



Advertisements
Similar presentations
Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Taekyung Kim 0x410 ~ 0x International Standards Organization (ISO) is a multinational body dedicated to worldwide agreement on international.
Review: –What functionality is supported by IP? –What IP does not do? –How many classes of IP addresses? –Explain fields in an IP header? –How subnet works?
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
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.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Socket Programming.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
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,
Introduction to Linux Network 劉德懿
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.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed Communication.
Assignment 3 A Client/Server Application: Chatroom.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
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.
Sirak Kaewjamnong Computer Network Systems
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.
Chapter 2 Applications and Layered Architectures Sockets.
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Advanced Sockets API-II Vinayak Jagtap
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
UNIT8: SOCKETS Topics Introduction to sockets Socket Addresses
1 Introduction to TCP/IP. 2 OSI and Protocol Stack OSI: Open Systems Interconnect OSI ModelTCP/IP HierarchyProtocols 7 th Application Layer 6 th Presentation.
Socket Programming Lab 1 1CS Computer Networks.
Advanced UNIX programming Fall 2002, lecture 16 Instructor: Ashok Srinivasan Acknowledgements: The syllabus and power point presentations are modified.
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.
Review: – Why layer architecture? – peer entities – Protocol and service interface – Connection-oriented/connectionless service – Reliable/unreliable service.
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.
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.
Lecture 15 Socket Programming 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.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Assignment 3 A Client/Server Application: Chatroom
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
Socket Programming in C
Transport layer API: Socket Programming
Linux Networks TCP/IP Networking Layers BSD Socket Interface
Tutorial on 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
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

1 Networking (Stack and Sockets API)

2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example of Client / Server Socket Structures Connection Walk-Through ( socket, connect, close) Network Files in /proc Advanced Topics References

3 Introduction Protocol Models –OSI seven-layer model Application Presentation Session Transport Network Data Link Physical

4 Protocol Models TCP/IP Protocol stack Link Layer Computer AComputer B Network Layer Transport Layer Application Layer

5 Protocol Models Example : File Transfer File on Disk Link Layer Network Layer Transport Layer Application LayerFTP Server TCP Header Chunk TCP Functions IP v4 Functions IP Header Driver Functions Ethernet Header FTP Header

6 TCP/IP : Abstracted View

7 Linux Kernel Support The TCP/IP is the heart of the Linux messaging system. BSD Net3 Socket API support Supported Network Stacks –AppleTalk –IPX (Internetwork Packet Exchange) IPv6 Support –Theoretical maximum of addresses

8 A socket is a bidirectional communication device that can be used to communicate with another process on the same machine or with a process running on other machines. Sockets are created and used differently from pipes. An inode is associated with each socket. The socket's descriptor is similar to a file descriptor (entry in the descriptor table).inode Defining Sockets Sockets int socketFd;

9 Socket Attributes Sockets are characterized by three attributes: domain, type, and protocol. domain: Domains specify the network medium that the socket communication will use. Domains are of two types; (Abstracted View) (Abstracted View) –AF_INET refers to Internet networking (Requires a Network Interface device) –AF_UNIX sockets based on a single computer (No network interface device required) eg: X11 Window Server

10 Socket Attributes Socket types : –Stream Sockets Provides a connection that is a sequenced and reliable two- way byte stream. Specified by the type SOCK_STREAM. Eg: TCP –Datagram Sockets Doesn’t establish and maintain a connection. Data may be lost, duplicated, or arrive out of sequence. Specified by the type SOCK_DGRAM. Eg: UDP

11 Socket Attributes Socket Protocols –Determined by Socket type and domain –Default protocol is 0

12 Using Sockets Server operations –Creating a Socket –Fill in the Socket Address structure –Naming a Socket –Creating a Socket Queue –Accepting Connections –Closing a Socket Client operations –Requesting Connections

13 Creating a Socket (2) socket() system callsocket socket system call returns a descriptor that is similar to a low-level file descriptor. socket created is one end point of a communication channel. #include int socket(int domain, int type, int protocol);socket

14 Creating a Socket (3) Domains types:Code ExampleCode Example AF_UNIXUNIX internal (file system sockets) AF_INETARPA Internet protocols (network sockets) AF_ISOISO standard protocols AF_NSXerox Network Systems protocols AF_IPXNovell IPX protocol AF_APPLETALKAppletalk DDS

15 Creating a Socket (4) Socket Addresses - Each socket domain requires its own address format. For AF_UNIX socket [sys/un.h] struct sockaddr_un { sa_family_t sun_family; /* AF_UNIX */ char sun_path[]; /* pathname */ };

16 Creating a Socket (5) For AF_INET socket [netinet/in.h] struct sockaddr_in { short int sin_family; /* AF_INET */ unsigned short int sin_port; /* Port number */ struct in_addr sin_addr; /* Internet address */ }; struct in_addr { unsigned long int s_addr; };

17 Naming a Socket AF_UNIX sockets are associated with a file system pathname, AF_INET sockets are associated with an IP port number. bind() system call assigns the address specified in the parameter, address, to the unnamed socket associated with the file descriptor socket.bind() #include int bind(int socket, const struct sockaddr *address, size_t address_len);

18 Naming a Socket (2) bind return value –0 on success, -1 on failure –Error codes EBADFThe file descriptor is invalid. ENOTSOCKThe file descriptor doesn’t refer to a socket. EINVALThe named file descriptor already exists. EADDRNOTAVAILThe address is unavailable. EADDRINUSEThe address has a socket bound to it already

19 Creating a Socket Queue A server program must create a queue to store pending requests. listen() system call.listen() Allows incoming connections to be held pending while a server program is busy dealing with a previous client. Return values same as for bind. #include int listen ( int socket, int backlog );

20 Accepting Connections Wait for connections to be made to the socket by using the accept() system call. accept Creates a new socket to communicate with the client and returns its descriptor. #include int accept(int socket, struct sockaddr *address, size_t *address_len);

21 Accepting Connections (2) By default, accept will block until a client makes a connection. O_NONBLOCK flag on the socket file descriptor. Returns EWOULDBLOCK, where O_NONBLOCK has been specified and there are no pending connections. int flags = fcntl(socket, F_GETFL, 0); fcntl(socket, F_SETFL, O_NONBLOCK|flags);

22 Requesting Connections connect() system call.connect() –Establishes connection between an unnamed socket and the server listen socket. Error Codes: #include int connect(int socket, const struct sockaddr *address, size_t address_len); EBADFAn invalid file descriptor was passed in socket. EALREADYA connection is already in progress for this socket. ETIMEDOUTA connection timeout has occurred. ECONNREFUSEDRequested connection was refused by the server.

23 Host and Network Byte Ordering Client and server programs must convert their internal integer representation to the network ordering before transmission. They do this by using functions defined in netinet/in.h Link Link #include unsigned long int htonl(unsigned long int hostlong); unsigned short int htons(unsigned short int hostshort); unsigned long int ntohl(unsigned long int netlong); unsigned short int ntohs(unsigned short int netshort);

24 Client-Server Request Flow

25 Socket Structures There are two main socket structures in Linux: –general BSD sockets –IP specific INET sockets. They are strongly interrelated; a BSD socket has an INET socket as a data member and and INET socket has a BSD socket as its owner.

26 Socket Structures BSD sockets are of type struct socket as defined in include/linux/socket.h –struct proto_ops *ops - contains pointers to protocol specific functions for implementing general socket behavior. –struct inode *inode - this structure points to the file inode that is associated with this socket. –struct sock *sk - this is the INET socket that is associated with this socket.

27 Socket Structures INET sockets are of type struct sock as defined in include/net/sock.h struct sock

28 socket() Call Walk-Through Check for errors in call Create (allocate memory for) socket object Put socket into INODE list Establish pointers to protocol functions (INET) Store values for socket type and protocol family Set socket state to closed Initialize packet queues socket() is a glibc-2.0 library function which ends up calling sys_socket()socket()

29 connect() Call Walk-Through Check for errors Determine route to destination: –Check routing table for existing entry –Look up destination in FIB –Build new routing table entry –Put entry in routing table and return it Store pointer to routing entry in socket Call protocol specific connection function Set socket state to established

30 Close connection Walk-Through Check for errors (does the socket exist?) Change the socket state to disconnecting to prevent further use Do any protocol closing actions (e.g., send a TCP packet with the FIN bit set) Free memory for socket data structures (TCP/UDP and INET) Remove socket from INODE list.

31 Network Files in /proc/net arp –displays the neighbor table (arp_tbl); (arp_get_info() : net/ipv4/arp.c) dev –displays reception and transmission statistics for each registered interface netstat –displays ICMP statistics (netstat_get_info() : net/ipv4/proc.c) raw –displays address, queue, and timeout information for each open RAW socket from struct proto raw_prot (get__netinfo() : net/ipv4/proc.c) sockstat –displays number of sockets that have been used and statistics on how many were TCP, UDP, and RAW (afinet_get_info() : net/ipv4/proc.c) tcp –displays address, queue, and timeout information for each open TCP socket from struct proto tcp_prot (get__netinfo() : net/ipv4/proc.c) udp –displays address, queue, and timeout information for each open UDP socket from struct proto udp_prot (get__netinfo() : net/ipv4/proc.c)

32 Advanced Topics Multiple Clients ( select system call) Multithreaded Sockets

33 References The Linux Documentation Project –( ) Advanced Linux Programming (GPL) –( ) TCP/IP Illustrated, Volume 2 –by Gary Wright, W. R. Stevens