© Janice Regan, CMPT 128, 2007-2012 CMPT 371 Data Communications and Networking Socket Programming 0.

Slides:



Advertisements
Similar presentations
Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.
Advertisements

Programming with TCP – I
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Data Communications and Networking (Third Edition)
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.
Socket Programming.
Tutorial 8 Socket Programming
1 Generic Transport Service Primitives Listen –notify Transport layer a call is expected Connect –establish Transport layer connection Send (or Write)
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
Leon-Garcia & Widjaja: Communication Networks Copyright ©2000 The McGraw Hill Companies The user clicks on a link to indicate which document is to be retrieved.
McGraw-Hill©The McGraw-Hill Companies, Inc., 2004 Application Layer PART VI.
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
I NTRODUCTION OF S OCKET P ROGRAMMING L.Aseel AlTurki King Saud University.
Chapter 26 Client Server Interaction Communication across a computer network requires a pair of application programs to cooperate. One application on one.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
ECE 4110 – Internetwork Programming Client-Server Model.
Elementary TCP Sockets
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
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.
Discussion 2 Sockets Programming Applets TCP UDP HTTP Delay Estimation
Application Layer 2-1 ESERCITAZIONE SOCKET PROGRAMMING.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Principles of reliable data transfer 0.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
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.
Application Network programming Using Java. Application Layer2-2 Socket programming socket: door between application process and end-end-transport protocol.
Page 1 Jan 5, 1998 CMPN 369 CPSC441 Sockets Module Sockets Application Programming Interface (API)
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
Distributed Computing A Programmer’s Perspective.
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 Client-Server Interaction. 2 Functionality Transport layer and layers below –Basic communication –Reliability Application layer –Abstractions Files.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
CS 6401 Introduction to Computer Networks 09/21/2010 Outline - UNIX sockets - A simple client-server program - Project 1 - LAN bridges and learning.
Socket Programming.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
CMPT 471 Networking II Network Programming © Janice Regan,
UNIX Sockets Outline UNIX sockets CS 640.
Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 Dimension of Server Designs r Iterative vs Concurrent r Connection-oriented vs. connectionless r Stateful and stateless m Constrained by application.
Application Network programming Using Java. Application Layer2-2 Socket programming socket: door between application process and end-end-transport protocol.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Network Communications A Brief Introduction. 2 Network Communications.
1 K. Salah Application Layer Module K. Salah Network layer duties.
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 Presented By : Divya Sharma.
Sockets and Beginning Network Programming
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Chapter 2 Application Layer
Interacting With Protocol Software
Transport layer API: Socket Programming
UNIX Sockets Outline Homework #1 posted by end of day
TCP Sockets Programming
Advanced Network Programming spring 2007
Internet Networking recitation #8
Presentation transcript:

© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0

Janice Regan © The socket interface  The socket interface is used in application programs to specify the interface between the application program and the transport layer protocol software (TCP, UDP)  The form of the interface (API) is not specified in the protocol, it may vary for different OS’s and between different computer languages.  The socket interface discussed here is the Python interface which is based on the BSD UNIX socket  The Python interface is similar to the BSD UNIX C interface but easier to use.

Janice Regan © IO: FILE vs. NETWORK  When reading or writing a file a file descriptor is used to specify the file referred to  A file is opened using a particular file descriptor, binding the file or device to that file descriptor  Data is read from and/or written to file  to read or write need at least three pieces of information  File descriptor  Buffer (holds data read from file or data being written to the file)  Number of bytes to read/write (or size of buffer)  File is closed

Janice Regan © NETWORK IO  A socket is a communication endpoint, the socket descriptor is an integer describing a particular socket  A socket is not automatically bound to a particular IP or port  The application programmer can choose when to bind the socket to a IP and port, or to let the OS bind to a IP and port at runtime  Two sockets can be connected to form a connection  When a connection is made it is assigned a connection identifier  When sending or receiving data across a network connection we write or read to a connection identifier

Janice Regan © IO: FILE vs. NETWORK  When reading or writing through a socket a connection descriptor is used to specify the particular connection  A socket is connected to another socket (communication endpoint) to create a connection  A created connection has a connection identifier  Data can then be transferred from one endpoint to the other. Once the sockets are connected the connection descriptor can be used like a file descriptor  When the data transfer is complete the connection is closed

Janice Regan © Establish connection socket( ) connect( ) accept( ) listen( ) bind( ) socket( ) TCP server TCP client Connection establishment TCP 3-way handshake Well known port Blocks until connection from client

TCP server (Python) for socket import * serverPort = 1200 serverSocket = socket.socket(AF_INET, SOCK_STREAM) serverSocket.bind((‘ ‘,serverPort)) serverSocket.listen(1) print ‘The server is ready to receive’ while 1; connectionSocket.addr = serverSocket.accept() sentence = connetionSocket.recv(1024) capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence) connection Socket.close() : Janice Regan ©

TCP client (Python) from socket import * serverName = ‘servername’ serverPort = 1200 clientSocket = socket.socket( AF_INET, SOCK_STREAM ) clientSocket.connect (serverName,serverPort) clientSocket.send (sentence) modifiedSentence = clientSocket.recv(1024) clientSocket.close; Janice Regan ©

8 TCP Data Transfer: write( ) read( ) close( ) read( ) write( ) Process request close( ) Data (request) Data (reply) End of file notification TCP client TCP server

Janice Regan © UDP Data Transfer sendto( ) recvfrom( ) bind( ) socket( ) sendto( ) socket( ) Data (request) Blocks until connection from client Process request recvfrom( ) UDP client Data (reply) close( )

Janice Regan © The socket method: socket(AF_INET, SOCK_STREAM, PROTOCOL )  The family indicates the family of protocols that will use the socket (AF_INET for IPv4, AF_INET6 for IPv6)  The type indicates the particular type of transfer that will be used (SOCK_STREAM for TCP, SOCK_DATAGRAM for UDP, SOCK_RAW for raw data)  The protocol indicates the particular protocol to use (usually 0)

Janice Regan © Creating a socket socketfd = socket.socket ( AF_INET, SOCK_STREAM);  socket is the socket descriptor for the newly created IPv4 TCP socket  This socket is not yet associated with any communication endpoint (IP address, port pair)  The socket is an active socket (for use in active connection mode)  You can see all the possible values for protocol family, protocol type and protocol in the descriptions of the python interface available online

Janice Regan © The bind function  The bind function associates the socket descriptor with a local (same host that the process doing the bind is running on) communication endpoint socketfd.bind( ‘ ‘, serverPort )  ‘ ‘ means all available interfaces, this means the server will accept requests regardless of which of its interfaces the requests arrive on

Janice Regan © local communication endpoints  The local port and IP address of the host’s interface can be determined automatically within the sockets software at run time. If a port or IP address is not specified then:  An interface to the network will be specified for you (local IP address)  An available (ephemeral) port on the host will be chosen to associate with the endpoint  Normally servers bind to a specified port, clients allow the OS kernel to choose an interface (the one that can connect to the server) and an ephemeral port

Janice Regan © Specifying Destination Address  In the connect function associates the socket descriptor with a destination address [(IP address, port) port pair specifying the destination connection endpoint]  For TCP this function initiates the three way handshake.  A client normally requests a connection using the connect function. A server normally waits for that connect request.  A socket can be connected regardless of whether it has been bound to a local address. If no call has been made to bind the OS kernel will assign the local ephemeral port and select a local IP address

Janice Regan © The connect function socketfd.connect( servername, serverport)  The socket address specifies the IP address and port number of the destination connection endpoint

Janice Regan © Waiting for a connection  The listen function converts an unconnected socket into a passive socket.  When created using socket the socket is an active socket  Listen is usually used by a server process.  The OS kernel queues requests for connection to the passive socket.

Janice Regan © The listen function socketfd.listen( backlog )  The backlog indicates the maximum number of connections the kernel (OS) should queue for this socket.

Janice Regan © Closing a connection  The close functions default results are  mark the socket with socket descriptor sockfd as closed  For a TCP connection, start the three way handshake to terminate the connection by sending a FIN  Prevents further use of the socket by the application  Allows any previously queued data to be sent before closing the connection socket.close( )

Janice Regan © The socket interface  We have discussed the basics of the socket interface when dealing with a simple iterative server and clients  The server will queue connect requests from different clients and deal with them one by one  For short requests like sending a single packet this can work well  For longer requests that take significant processing time this is not efficient, we would like the server to be able to deal with the requests simultaneously  The solution is to use a concurrent server, that makes a copy of itself or child to deal with each client