Computer Networks : TP1 Prof. Dr. Amine Berqia and Prof. Dr. Fernando Lobo {bamine,

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Socket Programming CS3320 Fall 2010.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
1 Netcomm Recitation 1: Sockets Communication Networks Recitation 1.
1 Netcomm Sockets Communication Networks Recitation 1.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
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.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles.
Socket Programming Based on tutorial prepared by EUISOK CHUNG CS3320 Spring2008.
CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C.
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.
Operating Systems Chapter 9 Distributed Communication.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
University of Calgary – CPSC 441.  UDP stands for User Datagram Protocol.  A protocol for the Transport Layer in the protocol Stack.  Alternative to.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
9/12/2015B.R1 Socket Abstraction and Interprocess Communication B.Ramamurthy CSE421.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
CS x760 Computer Networks1 Socket Programming. CS 6760 Computer Networks2 Socket Programming  What is a socket?  Using sockets  Types (Protocols) ‏
Remote Shell CS230 Project #4 Assigned : Due date :
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
1 Introduction to Computer Networks Ilam University By: Dr. Mozafar Bag Mohammadi Sockets.
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.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C m Note:
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.
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.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
CPSC Application Layer 1 Trying out HTTP (client side) for yourself 1. Telnet to your favorite Web server: Opens TCP connection to port 80 (default.
@Yuan Xue CS 283Computer Networks Spring 2013 Instructor: Yuan Xue.
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.
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.
Socket Abstraction and Interprocess Communication
Socket Programming What is a socket? Using sockets Types (Protocols)
CSCE 313 Network Socket MP8 DUE: FRI MAY 5, 2017
Sockets and Beginning Network Programming
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Introduction to Computer Networks
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Abstraction and Interprocess Communication
Socket Programming in C
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming(1/2)
Socket Programming What is a socket? Using sockets Types (Protocols)
Internet Networking recitation #8
Socket Programming What is a socket? Using sockets Types (Protocols)
Presentation transcript:

Computer Networks : TP1 Prof. Dr. Amine Berqia and Prof. Dr. Fernando Lobo {bamine,

Socket Programming b What is a socket? b Using sockets Types (Protocols)Types (Protocols) Associated functionsAssociated functions StylesStyles We will look at using sockets in CWe will look at using sockets in C

What is a socket? b An interface between application and network The application creates a socketThe application creates a socket b Once configured the application can pass data to the socket for network transmissionpass data to the socket for network transmission receive data from the socket (transmitted through the network by some other host)receive data from the socket (transmitted through the network by some other host)

Two essential types of sockets b SOCK_STREAM TCPTCP reliable deliveryreliable delivery in-order guaranteedin-order guaranteed connection-orientedconnection-oriented bidirectionalbidirectional b SOCK_DGRAM UDP unreliable delivery no order guarantees no notion of “connection” – app indicates dest. for each packet can send or receive

Socket Creation in C: socket b int s = socket(domain, type, protocol); s: socket descriptor, an integer (like a file-handle)s: socket descriptor, an integer (like a file-handle) domain: integer, communication domaindomain: integer, communication domain –e.g., PF_INET (IPv4 protocol) – typically used type: communication typetype: communication type –SOCK_STREAM: reliable, 2-way, connection-based service –SOCK_DGRAM: unreliable, connectionless, –other values: need root permission, rarely used, or obsolete protocol: specifies protocol (see file /etc/protocols for a list of options) - usually set to 0protocol: specifies protocol (see file /etc/protocols for a list of options) - usually set to 0 b NOTE: socket call does not specify where data will be coming from, nor where it will be going to – it just creates the interface!

Ports b Each host has 65,536 ports b Some ports are reserved for specific apps 20,21: FTP20,21: FTP 23: Telnet23: Telnet 80: HTTP80: HTTP r A socket provides an interface to send data to/from the network through a port

Addresses, Ports and Sockets b Like apartments and mailboxes You are the applicationYou are the application Your apartment building address is the addressYour apartment building address is the address Your mailbox is the portYour mailbox is the port The post-office is the networkThe post-office is the network The socket is the key that gives you access to the right mailbox (one difference: assume outgoing mail is placed by you in your mailbox)The socket is the key that gives you access to the right mailbox (one difference: assume outgoing mail is placed by you in your mailbox) b Q: How do you choose which port a socket connects to?

The bind function b associates and (can exclusively) reserves a port for use by the socket b int status = bind(sockid, &addrport, size); status: error status, = -1 if bind failedstatus: error status, = -1 if bind failed sockid: integer, socket descriptorsockid: integer, socket descriptor addrport: struct sockaddr, the (IP) address and port of the machine (address usually set to INADDR_ANY – chooses a local address)addrport: struct sockaddr, the (IP) address and port of the machine (address usually set to INADDR_ANY – chooses a local address) size: the size (in bytes) of the addrport structuresize: the size (in bytes) of the addrport structure

Connection Setup ( SOCK_STREAM ) b Recall: no connection setup for SOCK_DGRAM b A connection occurs between two kinds of participants passive: waits for an active participant to request connectionpassive: waits for an active participant to request connection active: initiates connection request to passive sideactive: initiates connection request to passive side b Once connection is established, passive and active participants are “similar” both can send & receive databoth can send & receive data either can terminate the connectioneither can terminate the connection

Connection setup: listen & accept b int status = listen(sock, queuelen); status: 0 if listening, -1 if errorstatus: 0 if listening, -1 if error sock: integer, socket descriptorsock: integer, socket descriptor queuelen: integer, # of active participants that can “wait” for a connectionqueuelen: integer, # of active participants that can “wait” for a connection listen is non-blocking: returns immediatelylisten is non-blocking: returns immediately b int s = accept(sock, &name, &namelen); s: integer, the new socket (used for data-transfer)s: integer, the new socket (used for data-transfer) sock: integer, the orig. socket (being listened on)sock: integer, the orig. socket (being listened on) name: struct sockaddr, address of the active participantname: struct sockaddr, address of the active participant namelen: sizeof(name)namelen: sizeof(name) accept is blocking: waits for connection before returningaccept is blocking: waits for connection before returning

connect call b int status = connect(sock, &name, namelen); status: 0 if successful connect, -1 otherwisestatus: 0 if successful connect, -1 otherwise sock: integer, socket to be used in connectionsock: integer, socket to be used in connection name: struct sockaddr: address of passive participantname: struct sockaddr: address of passive participant namelen: integer, sizeof(name)namelen: integer, sizeof(name) b connect is blocking

Sending / Receiving Data b With a connection (SOCK_STREAM): int count = send(sock, &buf, len, flags);int count = send(sock, &buf, len, flags); –count: # bytes transmitted (-1 if error) –buf: char[], buffer to be transmitted –len: integer, length of buffer (in bytes) to transmit –flags: integer, special options, usually just 0 int count = recv(sock, &buf, len, flags);int count = recv(sock, &buf, len, flags); –count: # bytes received (-1 if error) –buf: void[], stores received bytes –len: # bytes received –flags: integer, special options, usually just 0 Calls are blocking [returns only after data is sent (to socket buf) / received]Calls are blocking [returns only after data is sent (to socket buf) / received]

Sending / Receiving Data (cont’d) b Without a connection (SOCK_DGRAM): int count = sendto(sock, &buf, len, flags, &addr, addrlen);int count = sendto(sock, &buf, len, flags, &addr, addrlen); –count, sock, buf, len, flags: same as send –addr: struct sockaddr, address of the destination –addrlen: sizeof(addr) int count = recvfrom(sock, &buf, len, flags, &addr,int count = recvfrom(sock, &buf, len, flags, &addr, &addrlen); &addrlen); –count, sock, buf, len, flags: same as recv –name: struct sockaddr, address of the source –namelen: sizeof(name): value/result parameter b Calls are blocking [returns only after data is sent (to socket buf) / received]

close b When finished using a socket, the socket should be closed: b status = close(s); status: 0 if successful, -1 if errorstatus: 0 if successful, -1 if error s: the file descriptor (socket being closed)s: the file descriptor (socket being closed) b Closing a socket closes a connection (for SOCK_STREAM)closes a connection (for SOCK_STREAM) frees up the port used by the socketfrees up the port used by the socket