Introduction to Computer Networks

Similar documents
2007 Microsoft Corporation. All rights reserved.

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

UNIX System Programming Lecture 19: IP Sockets

Winsock Server adding Multiple clients support C++

Computer Network Programming

Introduction to Lab 2 and Socket Programming. -Vengatanathan Krishnamoorthi

CSE 333 Section 8 - Client-Side Networking

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

UNIX Sockets. COS 461 Precept 1

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

Sockets 15H2. Inshik Song

WinSock. What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics

Redesde Computadores(RCOMP)

Simple network applications using sockets (BSD and WinSock) Revision 1 Copyright Clifford Slocombe

What s an API? Do we need standardization?

Sockets and Parallel Computing. CS439: Principles of Computer Systems April 11, 2018

Client Server Computing

CSE 333 SECTION 8. Sockets, Network Programming

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

CSCI 4061: Sockets and Network Programming

CSE 333 SECTION 6. Networking and sockets

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

Ports under 1024 are often considered special, and usually require special OS privileges to use.

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Chapter 6. The Transport Layer. Transport Layer 3-1

Redesde Computadores(RCOMP)

last time redo logging copy-on-write filesystems / snapshots distributed systems motivation, etc.

Network Programming in Python. based on Chun, chapter 2; plus material on classes

Server-side Programming

CSCE 463/612 Networks and Distributed Processing Spring 2017

Redes de Computadores (RCOMP)

Elementary TCP Sockets

A. Basic Function Calls for Network Communications

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

Socket Programming for TCP and UDP

A Client-Server Exchange

CSE 333 SECTION 7. Client-Side Network Programming

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

Network Software Implementations

// print product names in alphabetical order with total // number sold of each product using format name: total void PrintTotals();

Networked Applications: Sockets. End System: Computer on the Net

CS 4400 Fall 2017 Final Exam Practice

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

CSE 333 Section 3. Thursday 12 April Thursday, April 12, 12

SOCKETS. COMP750 Distributed Systems

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

IPv6 Porting Applications

Client-side Networking

Server-side Programming

Oral. Total. Dated Sign (2) (5) (3) (2)

Tutorial on Socket Programming

CSE/EE 461 Lecture 14. Connections. Last Time. This Time. We began on the Transport layer. Focus How do we send information reliably?

Modern System Calls(IPv4/IPv6)

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

Windows Sockets: A Quick And Dirty Primer

CSE 333 SECTION 7. Client-Side Network Programming

IP Addresses, DNS. CSE 333 Summer Teaching Assistants: Renshu Gu William Kim Soumya Vasisht

CSE 124 January 18, Winter 2017, UCSD Prof. George Porter

Azblink API for Sending XMPP Messages via HTTP POST

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

IP Addresses, DNS. CSE 333 Spring Instructor: Justin Hsia

Group-A Assignment No. 6

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

CS 3516: Computer Networks

Interprocess Communication Mechanisms

shared storage These mechanisms have already been covered. examples: shared virtual memory message based signals

EEC-484/584 Computer Networks

UNIT IV- SOCKETS Part A

Application Programming Interfaces

JFx Joystick. June 21, Unrestricted Distribution. Revision 1.00

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

Computer Networks Prof. Ashok K. Agrawala

EECS122 Communications Networks Socket Programming. Jörn Altmann

ECE 650 Systems Programming & Engineering. Spring 2018

Windows Socket Modern System Calls & Concurrent, Connection-Oriented Servers. Prof. Lin Weiguo Copyleft 2009~2015, College of Computing, CUC

CptS 360 (System Programming) Unit 17: Network IPC (Sockets)

Transport Layer Review

QUIZ: Longest Matching Prefix

CSE 124 January 12, Winter 2016, UCSD Prof. George Porter

Overview. Last Lecture. This Lecture. Daemon processes and advanced I/O functions

Programming with TCP/IP. Ram Dantu

Lab 0. Yvan Petillot. Networks - Lab 0 1

??? Traceroute. app. app. host. host. Apps talk to other apps with no real idea of what is inside the network

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

ECE 435 Network Engineering Lecture 2

Transport Layer. Chapter 3: Transport Layer

5. System Calls or Bust

Network Communication

Introduction to Computer Systems. Networks 2. c Theodore Norvell. The Sockets API

A Case Study of IPv6 Deployment in tcd.ie

ECE 435 Network Engineering Lecture 2

Assignment description: This is a C++ project. The comms class containing the

Memory-Mapped Files. generic interface: vaddr mmap(file descriptor,fileoffset,length) munmap(vaddr,length)

Transcription:

Introduction to Computer Networks Tian Song ( 嵩天 ), Ph.D., Assoc. Prof. songtian@bit.edu.cn

Introduction to Computer Networks Socket and Network Programming Tian Song ( 嵩天 ), Ph.D., Assoc. Prof. songtian@bit.edu.cn

Outline Transport Layer Socket & Network Programming Elements of Transport Protocols 3

Transport Layer Question: Why we need a new layer? Answer: provide reliable connection oriented services provide unreliable connectionless services provide parameters for specifying quality of services 4

Transport Layer Essence: To develop applications, a standard communication interface is required to provide particular services. 5

Transport Layer Features: Logic communication Running in end systems application transport network data link physical network data link physical network data link physical network data link physical network data link physical TCP vs. UDP network data link physical application transport network data link physical 6

Transport Layer Transport Service Primitives : 7

Socket Berkeley Socket: host or server host or server process controlled by app developer process socket socket Process A Process B TCP with buffers, variables Internet TCP with buffers, variables a socket controlled by OS 8

Socket Berkeley Socket: socket API Process A Process B socket API transport layer software transport layer software connection oriented datagram socket 9

Socket Berkeley Socket: 10

Socket Client Server (CS Model) 11

Socket Winsock: Winsock API is supported by a DLL ws2_32.dll / ws2_32.lib / winsock2.h Windows Socket API (WSA) Server Client Mode (CS Mode) Lab2: programming 12

Winsock Server side Procedure WSAStartup(... ); //1. Initialize getaddrinfo (... ); //2. Collect network info socket (... ); //3. Create a socket bind (... ); //4. Bind the socket to IP addr listen (... ); //5. Listen to incoming request accept (... ); //6. If connect request recv, accept send (... ); / recv (... ); //7. Send or receive data : closesocket (... ); //8. Close the socket after using it WSACleanup (... ); //9. Free resource allocated 13

Elements of Transport Protocols Addressing Connection Establishment Connection Release Flow Control and Buffering Multiplexing 14

Addressing Question: How to distinguish different transport services in one end node? Answer: Port ( 端口 ): 0 ~ 65535 source port, destination port 15

Addressing 16

Question: Addressing How do we get to know where the other party is? Well Known Port ( 周知端口 ) 17

Connection Establishment Basic Idea: To establish a connection, you send off a connection request to the other end. The other end then accepts the connection, and returns an acknowledgment. Three way Handshake 18

Transport Layer 19

Connection Establishment Big Problem: What if the unstable network? 20

Connection Establishment Big Problem: What if the unstable network? 21

Connection Release Asymmetric Release one side Symmetric Release two sides 22

Symmetric Connection Release What if the unstable network? 23

Symmetric Connection Release What if the unstable network? 24

Outline Transport Layer Socket & Network Programming Elements of Transport Protocols 25

Attachment Winsock Programming 26

Winsock Initialization To initialize, a nonstandard Winsock specific function WSAStartup() must be the first function to call For example, WORD sockversion; WSADATA wsadata; //Store the socket version //Store socket info sockversion =... ; //Input the version number int iresult = // Return non-zero if error WSAStartup (sockversion, &wsadata); 27

WSAStartup() Parameters sockversion Indicates the highest version of the WinSock DLL you need Returns a non zero value if the DLL cannot support the version you want Can use a macro MAKEWORD to generate the number sockversion = MAKEWORD(2,2); //version 2.2 &wsadata points to a WSADATA structure that returns information on the configuration of the DLL iresult should be equal to zero if successful 28

getaddrinfo() getaddrinfo() provides protocolindependent translation from an ANSI host name to an addr struct addrinfo *result = NULL, hints; ZeroMemory(&hints, sizeof(hints)); hints.ai_family = AF_INET; Result kept here hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = AI_PASSIVE; iresult = getaddrinfo(null, portno, &hints,&result); if ( iresult!= 0 ) { WSACleanup(); Port number for making the return false; connection, e.g. 8888 } 29Means the School system of Computer will Science be free and Technology, to use any BIT, http://cs.bit.edu.cn/~songtian registered IP addr

addrinfo parameters ai_family = AF_INET denotes the address family. designates the Internet protocol (IP) ai_socktype = SOCK_STREAM specifies connectionoriented (for datagram communications, use SOCK_DGRAM) ai_protocol = IPPROTO_TCP specifies transport layer protocol is TCP ai_flag = AI_PASSIVE indicates the caller intends to use the returned socket address structure in a call to the bind function 30

Create a Socket Call socket() to create (or open) a socket mlistensocket = socket( result->ai_family, result->ai_socktype, result->ai_protocol); if (mlistensocket == INVALID_SOCKET) { freeaddrinfo(result); WSACleanup(); return false; } 31 Assume mlistensocket is a member variable of type SOCKET in your class to store the created socket If create failed, free the memory for result and cleanup everything

Bind to the Socket The bind() function associates a local address with a socket. For example, iresult = bind( mlistensocket, result->ai_addr, (int)result->ai_addrlen); if (iresult == SOCKET_ERROR) { freeaddrinfo(result); closesocket(mlistensocket); result is created in WSACleanup(); getaddrinfo() return false; mlistensocket is } created in socket() freeaddrinfo(result); result is not needed 32 School of Computer Science and Technology, any more, BIT, so http://cs.bit.edu.cn/~songtian free it here

Listen and Accept The listen() function places a socket in a state in which it is listening for an incoming connection Client connection requests will be queued iresult = listen(mlistensocket, SOMAXCONN); if (iresult == SOCKET_ERROR) { closesocket(mlistensocket); WSACleanup(); return false; It is an integer that indicates the } maximum length of the queue of pending connections. If set to SOMAXCONN, the underlying service provider responsible for socket will set the backlog to a 33 School of Computer maximum Science and Technology, reasonable BIT, http://cs.bit.edu.cn/~songtian value.

Listen and Accept (cont) The accept() function permits an incoming connection attempt on a socket SOCKET mclientsocket = accept (mlistensocket, NULL, NULL); //return a new socket with connection It extracts the first connection on the queue of pending connections on the created socket However, if no pending connections are present on the queue, the accept() function can block the caller until a connection is present 34

Send or Receive Data After a connection is established, the server can send or receive data to or from the client Partner stations exchange data using send() and recv() send() and recv() have identical arguments: int send ( int recv ( SOCKET s, SOCKET s, LPSTR lpbuffer, LPSTR lpbuffer, int nbufferlen, int nbufferlen, int nflags); int nflags); 35

send / recv Parameters lpbuffer send: the buffer that keeps the string to be sent recv: the buffer to keep the received string nbufferlen send: the length of the string recv: the size of the buffer used to keep the string nflags Can be used to indicate urgency Can also be used to allow reading the data but not removing it In general, use 0 Return the actual number of bytes transmitted or received. An error is indicated by the value less than or equal to 0 36

send / recv Examples mclientsocket is created by accept() char buffer[default_buflen]; int rval = recv(mclientsocket, buffer, DEFAULT_BUFLEN, 0); //if rval <= 0, error rval = send(mclientsocket, "Hello client, have a nice day!\n", 31, 0); //if rval <= 0, error 37 If no incoming data is available at the socket, the recv call blocks and waits for data to arrive

When Finish When finish transmitting or receiving data, remember to destroy the sockets and release the resource Use closesocket() and WSACleanup() SOCKET mlistensocket, mclientsocket; : //After finish using the socket... closesocket(mlistensocket); closesocket(mclientsocket); WSACleanup(); //Release the resource acquired 38

Example: IP Allocation Given a C class IP: 192.168.1.0/24 1) How many hosts it can support? 2) If we require 4 subnets, how many? 3) How to divide the network into 4? 39

End This page is intended blank. 40