1 Programming with TCP/IP by Armin R. Mikler. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software.

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.
Socket Programming Application Programming Interface.
Networks: TCP/IP Socket Calls1 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.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
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.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
Introduction to Project 1 Web Client and Server Jan 2006.
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.
1 Programming with TCP/IP Ram Dantu. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software cannot.
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.
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.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
9/12/2015B.R1 Socket Abstraction and Interprocess Communication B.Ramamurthy CSE421.
1 Programming with TCP/IP by Dr. Yingwu Zhu. 2 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
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.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
The Sockets Library and Concepts Rudra Dutta CSC Spring 2007, Section 001.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
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
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 3.1 Internet Applications Ch. 28,… (Client-Server Concept, Use of Protocol Ports, Socket API)
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Chapter 27 Socket API Interface The interface between an application program and the communication protocols in an operating system is known as the Application.
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.
CS 6401 Introduction to Computer Networks 09/21/2010 Outline - UNIX sockets - A simple client-server program - Project 1 - LAN bridges and learning.
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.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
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.
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.
Socket Programming in C
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Transport layer API: Socket Programming
UNIX Sockets Outline Homework #1 posted by end of day
Socket Abstraction and Interprocess Communication
Socket Programming in C
Programming with TCP/IP
Socket Programming.
Programming with TCP/IP
Socket Abstraction and Interprocess Communication
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Presentation transcript:

1 Programming with TCP/IP by Armin R. Mikler

2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software cannot initiate contact with, or accept contact from, a remote computer. Instead, two application programs must participate in any communication with one application initiates communication and the one accepts it.

3 r In network applications, a SERVER application waits passively for contact after informing local protocol software that a specific type of message is expected, while a CLIENT application initiates communication actively by sending a matched type of message.

4 Identifying A Particular Service r Transport protocols assign each service a unique identifier. r Both client and server specify the service identifier; protocol software uses the identifier to direct each incoming request to the correct server. r In TCP/IP, TCP uses 16-bit integer values known as protocol port numbers to identify services.

5 Concurrent Server r Concurrent execution is fundamental to servers because concurrency permits multiple clients to obtain a given service without having to wait for the server to finish previous requests. r In concurrent server designs, the server creates a new thread or process to handle each client. r Transport protocols assign an identifier to each client as well as to each service. r Protocol software on the server’s machine uses the combination of client and server identifiers to choose the correct copy of a concurrent server.

6 The Socket API r The interface between an application program and the communication protocols in an operating system (OS) is known as the Application Program Interface or API. r Sockets provide an implementation of the SAP (Service Access Point) abstraction at the Transport Layer in the TCP/IP protocol suite, which is part of the BSD Unix.

7 r A socket library can provide applications with a socket API on an operating system that does not provide native sockets (e.g. Windows 3.1). When an application calls one of the socket procedures, control passes to a library routine that makes one or more calls to the underlying OS to implement the socket function. r A socket may be thought of as a generalization of the BSD Unix file access mechanism (open-read- write-close) that provides an end-point for communication.

8 r When an application creates a socket, the application is given a small integer descriptor used to reference the socket. If a system uses the same descriptor space for sockets and other I/O, a single application can be used for network communication as well as for local data transfer. r An application must supply many details for each socket by specifying many parameters and options (e.g. an application must choose a particular protocol, provide address of remote machine, specify whether it is a client or server, etc.)

9 r To avoid having a single socket function with separate parameters for each options, designers of the socket API chose to define many functions, each with a few parameters.

10 Functions needed r Specify local and remote communication endpoints r Initiate a connection r Wait for incoming connection r Send and receive data r Terminate a connection gracefully r Error handling

11 read() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept() read() write() socket() connect() write() process request data (request) data (reply) Socket system calls for connection-orientedprotocol

12 Server (connectionless protocol) socket() blocks until data received from client bind() recvfrom() sendto() socket() bind() sendto() revfrom() process request data (request) data (reply) Client Socket system calls for connectionless protocol Not necessary in UDP!!

13 r Data communication between two hosts on the Internet require the five components of what is called an association to be initialized: {protocol,local-addr, local- process, foreign-addr, foreign-process} r The different system calls for sockets provides values for one or more of these components.

14 Socket system call r The first system call any process wishing to do network I/O has to call is the socket system call. r int sockfd = socket (int family, int type, int protocol) r Examples of Family include: m AF_UNIX m AF_INET r Examples of Type include m SOCK_STREAM m SOCK_DGRAM m SOCK_RAW

15 r The protocol argument is typically zero, but may be specified to request an actual protocol like UDP, TCP, ICMP, etc. r The socket system call just fills in one element of the five-tuple we’ve looked at - the protocol. The remaining are filled in by the other calls as shown in the figure.

16 Connection-Oriented Server Connection-oriented Client Connectionless Server Connectionless Client socket() bind() accept() connect() recvfrom() sendto() local_addr, local_process foreign_addr, foreign_process protocol

17 Specifying an Endpoint Address r Remember that the sockets API is generic r There must be a generic way to specify endpoint addresses r TCP/IP requires an IP address and a port number for each endpoint address. r Other protocol suites(families) may use other schemes. r Generic socket addresses r ( The C function that make up the sockets API expect structures of type sockaddr. ) : struct sockaddr { unsigned short sa_family; //specifies the address type char sa_data[14]; //specifies the address value };

18 AF_INET--TCP/IP address r For AF_INET we need: m 16 bit port number m 32 bit IP address (IPv4 only) struct sockaddr_in{ short sin_family; unsigned shortsin_port; struct in_addrsin_addr; char sin_zero[8]; }; r how these fields to be set and interpreted?

19 Network Byte Order Functions Example: struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(9999); sin.sin_addr.s_addr = inet_addr; unsigned short htons(unsigned short); unsigned short ntohs(unsigned short); unsigned long htonl(unsigned long); unsigned long ntohl(unsigned long);

20 Bind System Call r The bind system call assigns an address to an unnamed socket. Example: r m int bind(int sockfd, struct sockaddr_in *myaddr, int addrlen)

21 r What is bind used for ? m Servers (both connection oriented and connectionless) NEED to register their well-known address to be able to accept connection requests. m A client can register a specific address for itself. m A connectionless client NEEDS to assure that it is bound to some unique address, so that the server has a valid return address to send its responses to – However, it does not have to bind to a particular port! WHY?

22 m The bind system call provides the values for the local_addr and local_process elements in the five_tuple in an association. m An address for the Internet domain sockets is a combination of a hostname and a port number, as shown below: struct sockaddr_in { short sin_family ; /*typically AF_INET*/ u_short sin_port; /* 16 bit port number, network byte ordered */ struct in_addr sin_addr ; /* 32 bit netid/hostid, network byte ordered */ char sin_zero[8]; /* unused*/ }

23 Connect/Listen/Accept System Calls r Connect m A client process connects a socket descriptor after a socket system call to establish a connection with the server. m int connect(int sockfd, struct sockaddr_in *servaddr, int addrlen) m For a connection-oriented client, the connect (along with an accept at the server side) assigns all four addresses and process components of the association.

24 r Listen m The listen system call is used by a connection- oriented server to indicate it is willing to receive connections. r int listen(int socket, int qlength) m allows servers to prepare a socket for incoming connections m puts the socket in a passive mode ready to accept connections m informs the OS that the protocol software should enqueue multiple simultaneous requests that arrive at the socket m applies only to sockets that have selected reliable stream delivery service

25 r Accept m After the connection-oriented server executes a listen, it waits for connection requests from client(s) in the accept system call, e.g., newsockfd = accept(sockfd, peer, addrlen) m needs to wait for a connection m blocks until a connection request arrives m addrlen is a pointer to an integer; when a request arrives, the system fills in argument addr with the address of the client that has placed the request and sets addrlen to the length of the address. system creates a new socket, returns the new socket descriptor

26 m accept returns a new socket descriptor, which has all five components of the association specified - three (protocol, local addr, local_process) are inherited from the existing sockfd (which however has its foreign address and process components unspecified, and hence can be re-used to accept another request. This scenario is typical for concurrent servers.

27 Sending and Receiving Data r Here’s how you might read from a socket: m num_read = read(sockfd, buff_ptr, num_bytes) r And here’s how you read from an open file descriptor in Unix: m num_read = read(fildes, buff_ptr, num_bytes) r There are other ways (with different parameters) to send and receive data: read, readv, recv, recvfrom, recvmsg to receive data through a socket; and write, writev, send, sendto, sendmsg to send data through a socket.

28 sendto()--UDP Sockets r int sendto(int socket, char *buffer, int length, int flags, struct sockaddr *destination_address, int address_size); r For example: struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(12345); sin.sin_addr.s_addr = inet_addr(" "); char *msg = "Hello, World"; sendto(s, msg, strlen(msg)+1, 0, (struct sockaddr *)sin, sizeof(sin));

29 recvfrom()--UDP Sockets r Int recvfrom(int socket, char *buffer, int length, int flags, struct sockaddr *sender_address, int *address_size) r For example: struct sockaddr_in sin; char msg[10000]; int ret; int sin_length; sin_length = sizeof(sin); ret = recvfrom(s, msg, 10000, 0, (struct sockaddr *)sin, &sin_length); printf("%d bytes received from %s (port %d)\n", ret, inet_ntoa(sin.sin_addr), sin.sin_port);

30 send() and recv() -- TCP Sockets r int send(int s, const char *msg, int len, int flags) m connected socket m argument flags controls the transmission. allows the sender to specify that the message should be sent out-of- band messages correspond to TCP’s urgent data allows the caller to request that the message be sent without using local routine tables (take control of routine) r int recv(int s, char *buf, int len, int flags) m connected socket m argument flags allow the caller to control the reception look ahead by extracting a copy of the next incoming message without removing the message from the socket

31 close() and shutdown() r close(int socket) m For UDP sockets, this will release the ownership on the local port that is bound to this socket m For TCP, this will initiate a two-way shutdown between both hosts before giving up port ownership. r shutdown(int socket, int how) m f the how field is 0, this will disallow further reading (recv) from the socket. m If the how field is 1, subsequent writes (send) will be disallowed. The socket will still need to be passed to close.

32 Relationship Between Sockets and File Descriptors r Socket handles are integer values. In UNIX, socket handles can be passed to most of the low-level POSIX I/O functions. m read(s, buffer, buff_length); //s could be a file descriptor too m write(s, buffer, buff_length) ; r Calling read on an open socket is equivalent to recv and recvfrom m if the socket is UDP, then information about the sender of the datagram will not be returned r Similarly the write function call is equivalent to send and sendto m UDP sockets may call connect to use send and write r use the socket library functions instead of the file I/O equivalents.

33 Utility Functions r unsigned int inet_addr(char *str) m str represents an IP address(dotted-quad notation); inet_addr will return it's equivalent 32-bit value in network byte order. m This value can be passed into the sin_addr.s_addr field of a socketaddr_in structure m -1 is returned if the string can not be interpreted r char *inet_ntoa(struct in_addr ip) m Converts the 32-bit value which is assumed to be in network byte order and contained in ip to a string m The pointer returned by inet_ntoa contains this string. However, subsequent calls to inet_ntoa will always return the same pointer, so copying the string to another buffer is recommended before calling again.

34 Utility Functions ( cont’d ) r int gethostname(char *name, int length) m Copies the name (up to length bytes) of the hostname of the local computer into the character array pointed to by name r struct hostent *gethostbyname(char *strHost) r int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)

35 Other Socket API r JAVA m platform independence; m Java API for network programming java.net Pakage classes – Socket – ServerSocket –DatagramSocket –DatagramPacket –InetAddress –etc. m compile and run

36 Others r Include files m #include ; #include ; #include ; #include ; #include ; #include ; #include ; #include ; #include ; #include ; #include ; #include ; r Compiling and Linking m Under most versions of UNIX (Linux, BSD, SunOS, IRIX) compiling is done as usual: gcc my_socket_program.c -o my_socket_program m Solaris: cc my_socket_program.c -o my_socket_program -lsocket -lnsl r Programming tips m always check the return value for each function call m consult the UNIX on-line manual pages ("man") for a complete description

37 Summary r Network Application Programming Interface (API) r TCP/IP basic r UNIX/C Sockets m socket() ; bind() ; connect() ; listen() ; accept() ; sendto() ; recvfrom(); send() ; recv() ; read() ; write(); m some utility functions r Java Socket API