Tutorial on Socket Programming

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

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

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

Computer Networks Prof. Ashok K. Agrawala

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

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

Socket Programming TCP UDP

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

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

UNIX Sockets. COS 461 Precept 1

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Unix Network Programming

TCP: Three-way handshake

Socket Programming for TCP and UDP

Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software

CLIENT-SIDE PROGRAMMING

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

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles

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

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

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

Network programming(i) Lenuta Alboaie

A Client-Server Exchange

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Programming with TCP/IP. Ram Dantu

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

System Programming. Sockets

Introduction to Socket Programming

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Group-A Assignment No. 6

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

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

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

CSE 124 Discussion Section Sockets Programming 10/10/17

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

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

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

Elementary TCP Sockets

Department of Computer Science

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

CS321: Computer Networks Introduction to Application Layer

UNIT 1 TCP/IP PROGRAMMING CONCEPTS

What s an API? Do we need standardization?

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

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

Network Software Implementations

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

STUDY OF SOCKET PROGRAMMING

Network Communication

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

CSE 333 SECTION 8. Sockets, Network Programming

Sockets 15H2. Inshik Song

CS 640: Computer Networking

CS 3516: Computer Networks

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

Internet applications

Topics for this Week

CS321: Computer Networks Socket Programming

UNIT IV- SOCKETS Part A

CSc 450/550 Computer Networks Network Architectures & Client-Server Model

CSE 333 SECTION 7. Client-Side Network Programming

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

CSE 333 Lecture 16 - network programming intro

Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel

Network Socket Programming - 3 BUPT/QMUL

Overview. Administrative. * HW# 5 Due next week. * HW# 5 : Any Questions. Topics. * Client Server Communication. * 12.

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

Introduction to Socket Programming

Networks. Practical Investigation of TCP/IP Ports and Sockets. Gavin Cameron

Network Socket Programming - 3 BUPT/QMUL

Communication. Sockets (Haviland Ch. 10)

UNIX Network Programming. Overview of Socket API Network Programming Basics

EEC-484/584 Computer Networks

9/13/2007. Motivations for Sockets What s in a Socket? Working g with Sockets Concurrent Network Applications Software Engineering for Project 1

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

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

CSE 333 Section 8 - Client-Side Networking

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

SOCKETS. COMP750 Distributed Systems

CSE 333 Lecture network programming intro

Socket Programming(2/2)

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

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

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

C Structures in Practice

The TCP Protocol Stack

Client Server Computing

Socket Programming. What is a socket? Using sockets. Types (Protocols) Associated functions Styles

Socket Programming 2007/03/28

Lab 0. Yvan Petillot. Networks - Lab 0 1

Writing Network Applications using the TCP/IP Protocol Stack: Socket Programming

Transcription:

Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Hao Wang (Slides are mainly from Seyed Hossein Mortazavi, Monia Ghobadi, and Amin Tootoonchian, ) 1

Outline Client-server paradigm Sockets Socket programming in UNIX 2

End System: Computer on the Net Internet Also known as a host 3

Clients and Servers Client program Running on end host Requests service E.g., Web browser Server program Running on end host Provides service E.g., Web server GET /index.html Site under construction 4

Client-Server Communication Client Sometimes on Initiates a request to the server when interested E.g., web browser Needs to know the server s address Server Always on Serve services to many clients E.g.,www.cnn.com Not initiate contact with the clients Needs a fixed address 5

Socket: End Point of Communication Processes send messages to one another Message traverse the underlying network A Process sends and receives through a socket Analogy: the doorway of the house. Socket, as an API, supports the creation of network applications User process socket Operating System User process socket Operating System 6

UNIX Socket API Socket interface A collection of system calls to write a networking program at user-level. Originally provided in Berkeley UNIX Later adopted by all popular operating systems In UNIX, everything is like a file All input is like reading a file All output is like writing a file File is represented by an integer file descriptor Data written into socket on one host can be read out of socket on other host System calls for sockets Client: create, connect, write, read, close Server: create, bind, listen, accept, read, write, close 7

Typical Client Program Prepare to communicate Create a socket Determine server address and port number Why do we need to have port number? 8

Using Ports to Identify Services Client host Client Service request for 128.100.3.40 :80 (i.e., the Web server) Server host 128.100.3.40 OS Web server (port 80) Echo server (port 7) Client Service request for 128.100.3.40 :7 (i.e., the echo server) OS Web server (port 80) Echo server (port 7) 9

Socket Parameters A socket connection has 5 general parameters: The protocol Example: TCP and UDP. The local and remote address Example: 128.100.3.40 The local and remote port number Some ports are reserved (e.g., 80 for HTTP) Root access require to listen on port numbers below 1024 10

Typical Client Program Prepare to communicate Create a socket Determine server address and port number Initiate the connection to the server Exchange data with the server Write data to the socket Read data from the socket Do stuff with the data (e.g., render a Web page) Close the socket 11

Important Functions for Client Program socket() create the socket descriptor connect() connect to the remote server read(),write() communicate with the server close() end communication by closing socket descriptor 12

Creating a Socket int socket(int domain, int type, int protocol) Returns a descriptor (or handle) for the socket Domain: protocol family PF_INET for the Internet Type: semantics of the communication SOCK_STREAM: Connection oriented SOCK_DGRAM: Connectionless Protocol: specific protocol UNSPEC: unspecified (PF_INET and SOCK_STREAM already implies TCP) E.g., TCP: sd = socket(pf_inet, SOCK_STREAM, 0); E.g., UDP: sd = socket(pf_inet, SOCK_DGRAM, 0); 13

Connecting to the Server int connect(int sockfd, struct sockaddr *server_address, socketlen_t addrlen) Arguments: socket descriptor, server address, and address size Remote address and port are in struct sockaddr Returns 0 on success, and -1 if an error occurs 14

Sending and Receiving Data Sending data write(int sockfd, void *buf, size_t len) Arguments: socket descriptor, pointer to buffer of data, and length of the buffer Returns the number of characters written, and -1 on error Receiving data read(int sockfd, void *buf, size_t len) Arguments: socket descriptor, pointer to buffer to place the data, size of the buffer Returns the number of characters read (where 0 implies end of file ), and -1 on error Closing the socket int close(int sockfd) 15

Byte Ordering: Little and Big Endian Hosts differ in how they store data E.g., four-byte number (byte3, byte2, byte1, byte0) Little endian ( little end comes first ) ß Intel PCs!!! Low-order byte stored at the lowest memory location byte0, byte1, byte2, byte3 Big endian ( big end comes first ) High-order byte stored at lowest memory location byte3, byte2, byte1, byte 0 IP is big endian (aka network byte order ) Use htons() and htonl() to convert to network byte order Use ntohs() and ntohl() to convert to host order 16

Servers Differ From Clients Passive open Prepare to accept connections but don t actually establish one until hearing from a client Hearing from multiple clients Allow a backlog of waiting clients... in case several try to start a connection at once Create a socket for each client Upon accepting a new client create a new socket for the communication 17

Typical Server Program Prepare to communicate Create a socket Associate local address and port with the socket Wait to hear from a client (passive open) Indicate how many clients-in-waiting to permit Accept an incoming connection from a client Exchange data with the client over new socket Receive data from the socket Send data to the socket Close the socket Repeat with the next connection request 18

Important Functions for Server Program socket() create the socket descriptor bind() associate the local address listen() wait for incoming connections from clients accept() accept incoming connection read(),write() communicate with client close() close the socket descriptor 19

Socket Preparation for Server Program Bind socket to the local address and port int bind (int sockfd, struct sockaddr *my_addr, socklen_t addrlen) Arguments: socket descriptor, server address, address length Returns 0 on success, and -1 if an error occurs Define the number of pending connections int listen(int sockfd, int backlog) Arguments: socket descriptor and acceptable backlog Returns 0 on success, and -1 on error 20

Accepting a New Connection int accept(int sockfd, struct sockaddr *addr, socketlen_t *addrlen) Arguments: socket descriptor, structure that will provide client address and port, and length of the structure Returns descriptor for a new socket for this connection What happens if no clients are around? The accept() call blocks waiting for a client What happens if too many clients are around? Some connection requests don t get through But, that s okay, because the Internet makes no promises 21

Server Operation accept() returns a new socket descriptor as output New socket should be closed when done with communication Initial socket remains open, can still accept more connections 22

Putting it All Together Server socket() bind() block process request listen() accept() read() write() Client socket() connect() write() read() 23

Supporting Function Calls gethostbyname() get address for given host name (e.g. 128.100.3.40 for name cs.toronto.edu ); getservbyname() get port and protocol for a given service e.g. ftp, http (e.g. http is port 80, TCP) getsockname() get local address and local port of a socket getpeername() get remote address and remote port of a socket 24

Useful Structures struct sockaddr { u_short sa_family; char sa_data[14]; }; struct sockaddr_in { u_short sa_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; struct in_addr { u_long s_addr; }; Generic address, connect(), bind(), accept() <sys/socket.h> Client and server addresses TCP/UDP address (includes port #) <netinet/in.h> IP address <netinet/in.h> 25

Other useful stuff Address conversion routines Convert between system s representation of IP addresses and readable strings (e.g. 128.100.3.40 ) unsigned long inet_addr(char* str); char * inet_ntoa(struct in_addr inaddr); Important header files: <sys/types.h>, <sys/socket.h>, <netinet/in.h>, <arpa/inet.h> man pages socket, accept, bind, listen 26

Next tutorial session: Assignment 1 overview Please post questions to the bulletin board Office hours posted on website 27

Socket types Stream Sockets: Delivery in a networked environment is guaranteed. If you send through the stream socket three items "A, B, C", they will arrive in the same order - "A, B, C". These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do not have any boundaries. Datagram Sockets: Delivery in a networked environment is not guaranteed. They're connectionless because you don't need to have an open connection as in Stream Sockets - you build a packet with the destination information and send it out. They use UDP (User Datagram Protocol). Raw Sockets: These provide users access to the underlying communication protocols, which support socket abstractions. These sockets are normally datagram oriented, though their exact characteristics are dependent on the interface provided by the protocol. Raw sockets are not intended for the general user; they have been provided mainly for those interested in developing new communication protocols, or for gaining access to some of the more cryptic facilities of an existing protocol. Sequenced Packet Sockets: They are similar to a stream socket, with the exception that record boundaries are preserved. This interface is provided only as a part of the Network Systems (NS) socket abstraction, and is very important in most serious NS applications. Sequencedpacket sockets allow the user to manipulate the Sequence Packet Protocol (SPP) or Internet Datagram Protocol (IDP) headers on a packet or a group of packets, either by writing a prototype header along with whatever data is to be sent, or by specifying a default header to be used with all outgoing data, and allows the user to receive the headers on incoming 28 packets.