Tutorial on Socket Programming

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
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: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Tutorial 8 Socket Programming
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.
CSE 486/586 Distributed Systems Socket Programming and Android
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
1 Networked Applications: Sockets COS 461: Computer Networks Spring 2006 (MW 1:30-2:50 in Friend 109) Jennifer Rexford Teaching Assistant: Mike Wawrzoniak.
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
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.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
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.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Lecture 4: Networked Applications & Sockets Reading: Chapter 1 ? CMSC 23300/33300 Computer Networks
Sirak Kaewjamnong Computer Network Systems
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.
Remote Shell CS230 Project #4 Assigned : Due date :
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
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
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.
Introduction to Sockets
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.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Sockets API Developing Applications using the Sockets API.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Sockets and Beginning Network Programming
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Transport layer API: Socket Programming
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Network Programming CSC- 341
Socket Programming in C
TCP Sockets Programming
Advanced Network Programming spring 2007
Chapter 2 Application Layer
Tutorial on Socket Programming
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Socket Programming What is a socket? Using sockets Types (Protocols)
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

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, …)

Client-server paradigm Sockets Outline Client-server paradigm Sockets Socket programming in UNIX

End System: Computer on the Net Internet Also known as a “host”…

Clients and Servers Client program Server 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 Always on Serve services to many clients E.g.,www.cnn.com Not initiate contact with the clients Needs a fixed address Client Sometimes on Initiates a request to the server when interested E.g., web browser Needs to know the server’s address

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 User process socket Operating System Operating System

UNIX Socket API Socket interface In UNIX, everything is like a file 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

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

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

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

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

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

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);

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

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)

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

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

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

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

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

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

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

Putting it All Together Server socket() bind() Client listen() socket() accept() block read() process request connect() write() 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

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>

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

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

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. Sequenced- packet 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 packets. 28