1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.

Slides:



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

Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Socket Programming Application Programming Interface.
15-441: Computer Networking Lecture 3: Application Layer and Socket Programming.
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: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
EECS122 Communications Networks Socket Programming January 30th, 2003 Jörn Altmann.
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)
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
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.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
2: Application Layer 1 Chapter 2 Application Layer Computer Networking: A Top Down Approach, 5th edition. Jim Kurose, Keith Ross Addison-Wesley, April.
Introduction to Project 1 Web Client and Server Jan 2006.
UNIX Sockets COS 461 Precept 1.
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.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
2: Application Layer 1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r Internet gaming r 2.3 FTP r 2.4 Electronic.
IT COOKBOOK Windows Network Programming. Chapter 01. Intro. to Network and Socket Programming.
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.
University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology Networking Concepts, Socket Programming Spring 2012.
1 Internet Socket programming Behzad Akbari. 2 Sharif University of Technology, Kish Island Campus What is an API? API – stands for Application Programming.
Chapter 2 Applications and Layered Architectures Sockets.
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.
Remote Shell CS230 Project #4 Assigned : Due date :
1 Introduction to Computer Networks Ilam University By: Dr. Mozafar Bag Mohammadi Sockets.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
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.
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
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.
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,
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
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.
Review: – Why layer architecture? – peer entities – Protocol and service interface – Connection-oriented/connectionless service – Reliable/unreliable service.
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 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.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
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.
Sockets and Beginning Network Programming
UNIX Sockets COS 461 Precept 1.
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Review: TCP Client-Server Interaction
Introduction to Computer Networks
Imam Ahmad Trinugroho, ST., MMSI
Transport layer API: Socket Programming
Chapter 2 Application Layer
Internet Networking recitation #8
CSI 4118 – UNIVERSITY OF OTTAWA
Chapter 2: Application layer
Presentation transcript:

1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets

Univ. of TehranIntroduction to computer network2 Outline Protocol-to-protocol interface. Process model Socket programming What is a Socket? Why do we need sockets? Types of sockets Uses of sockets Example applications Code

Univ. of TehranIntroduction to computer network3 Protocol-to-Protocol Interface Configure multiple layers static versus extensible Process Model avoid context switches Buffer Model avoid data copies Use shared buffer among protocol layers

Univ. of TehranIntroduction to computer network4 Process Model (a)(b) Process-per-ProtocolProcess-per-Message

Univ. of TehranIntroduction to computer network5 Socket programming Goal- Communication between two processes They use interface/services from the transport layer. The interface is called Application Programming Interface, API. Client process Server process TCP/UDP IP Ethernet Adaptor Socket API

Univ. of TehranIntroduction to computer network6 What are Sockets? It is an API between applications and network protocol software provided by the OS Functions it provides: – Define an “end- point” for communication – Initiate and accept a connection – Send and receive data – Terminate a connection gracefully Supports multiple protocol families – Examples: Unix inter- process communication, TCP/ IP – Only Internet sockets will be covered in this lecture – Berkeley Sockets are the most common (from BSD Unix)

Univ. of TehranIntroduction to computer network7 Type of Sockets Two different types of sockets : – stream vs. datagram Stream socket :( a. k. a. connection- oriented socket) – It provides reliable, connected networking service – Error free; no out- of- order packets (uses TCP) – applications: telnet/ ssh, http, … Datagram socket :( a. k. a. connectionless socket) – It provides unreliable, best- effort networking service – Packets may be lost; may arrive out of order (uses UDP) – applications: streaming audio/ video (realplayer), …

Univ. of TehranIntroduction to computer network8 Socket usages Network applications use sockets at some level, often using higher level protocols on top of sockets – File transfer apps (FTP), Web browsers (HTTP), (SMTP/ POP3), etc… Simplify and expedite application development process Most exploits client-server model.

Univ. of TehranIntroduction to computer network9 A generic server (FTP) Wait for connections on a port When a client connection comes in, loop: – Read in the client’s request – Read data from a file – Send the data to the client – Disconnect when we have reached EOF

Univ. of TehranIntroduction to computer network10 A generic client, high level Connect to a given server loop: – Send a request to the server – Read server’s response – Read server’s data – Disconnect when we have reached EOF

Univ. of TehranIntroduction to computer network11 Client- Server communication (TCP) socket() bind() to a receiving port listen () to socket Accept() connection socket() bind() to any port connect () to server send() recv() send() recv() server client

Univ. of TehranIntroduction to computer network12 Socket Transport protocol needs the following information for multiplexing/demultiplexing. (source port, source address, destination port, destination address) Socket- the end point of connection. Process read/write from/to socket. A socket is specified by a socket descriptor. struct sockaddr_in { u_char sin_family; /* Address Family */ u_short sin_port; /* Port number */ struct in_addr sin_addr; /* IP address */ char sin zero[8]; /* unused */ };

Univ. of TehranIntroduction to computer network13 TCP Server program Make a socket #include int fd, newfd, nbytes, nbytes2; char buf[512], response[512]; struct sockaddr_in srv; fd = socket(AF_INET, SOCK_STREAM, 0);

Univ. of TehranIntroduction to computer network14 TCP Server program The socket was created now bind it to a port and host. srv.sin_family = AF_INET; srv.sin_port = htons(80); srv.sin_addr.s_addr = inet_addr(`` ''); bind(fd, (struct sockaddr*) &srv, sizeof(srv)); Now sit and listen; listen(fd, 5); Now, accept any connection. First, clear the structure. struct sockaddr_ in cli; int cli_ len; memset(& cli, 0, sizeof( cli)); /* clear it */ newfd = accept(fd, (struct sockaddr*) &cli, &cli len);

Univ. of TehranIntroduction to computer network15 TCP Server program Now it can read from socket, newfd, and write to socket, newfd. int BUF_ SIZE = 1024, bytesrecv = 0; char buf[ BUF_ SIZE]; /* receives up to BUF_ SIZE bytes from sock and stores them in buf. */ bytesrecv = recv( newfd, buf, BUF_ SIZE, 0); /* send up BUF_ SIZE bytes */ bytesrecv = send( newfd, buf, BUF_ SIZE, 0); At the end, we need to close both sockets by close command. close( newfd); /* closes the socket newfd */

Univ. of TehranIntroduction to computer network16 TCP client program int fd, newfd, nbytes, nbytes2; char buf[512], response[512]; struct sockaddr_in srv; fd = socket(AF_INET, SOCK_STREAM, 0); The same as server. Now, it needs to connect to server. srv.sin_family = AF_INET; srv.sin_port = htons(80); srv.sin_addr.s_addr = inet_addr(`` ''); connect(fd, (struct sockaddr*) &srv, sizeof(srv)); Connect is blocking and send a SYN signal and is blocked until receive SYNACK, (three way handshaking) It can start now reading and writing sprintf(request, ``Here's my request''); nbytes2 = write(fd, request, strlen(response)); close(fd);

Univ. of TehranIntroduction to computer network17 Client- Server communication (UDP) socket() to create socket bind() to a receiving port recvfrom () sendto() socket() to create scoket bind() to any port recvfrom () sendto () server client

Univ. of TehranIntroduction to computer network18 UDP Server/client program /* fill in declarations */ fd = socket(AF_INET, SOCK_DGRAM, 0); The socket was created now bind it to a port and host exactly the same way as TCP. Now listen; no connection, start reading from the port by nbytes = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr*) &cli, &cli len); UDP client is the same TCP, except instead of read/write, it uses nbytes = sendto(fd, request, sizeof(request), 0, (struct sockaddr*) &srv, sizeof(srv));