Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Network Server Programming Expression Evaluation Tutorial #10 CPSC 261.
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.
Sockets Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
תקשורת באינטרנט Tutorial 8. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
Tutorial 8 Socket Programming
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.
ISP – 9 th Recitation Socket programming – Client side.
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.
UNIX Sockets COS 461 Precept 1.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
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.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
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.
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.
Sockets API Overview Sockets with UDP Sockets with TCP Fast Sockets (Fast UDP) IP Multicasting.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
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.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
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.
Chapter 27 Socket API Interface The interface between an application program and the communication protocols in an operating system is known as the Application.
Socket Programming Lab 1 1CS Computer Networks.
Socket Programming.
1 CS716 Advanced Computer Networks By A. Wahid Shaikh.
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.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
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.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
CSCE 313 Network Socket MP8 DUE: FRI MAY 5, 2017
UNIX Sockets COS 461 Precept 1.
Advanced Computer Networks
Socket Programming in C
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Transport layer API: Socket Programming
UNIX Sockets Outline Homework #1 posted by end of day
Socket Abstraction and Interprocess Communication
Socket Programming in C
TCP Sockets Programming
Starting TCP Connection – A High Level View
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Internet Networking recitation #8
Sockets.
Presentation transcript:

Network Programming Tutorial #9 CPSC 261

A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere else in the world Using either the TCP or UDP transport protocol

Network API Sockets are the key data structure Collection of system calls that create/destroy/manage sockets – Create/destroy: socket, close – Connect: bind, connect, listen, accept – Manage: shutdown, setsockopt – Send/Receive: send, recv, write, read, sendto, recvfrom

Socket calls in sequence (UDP) Client side Socket Bind Sendto Recvfrom Close Server side Socket Bind Recvfrom Sendto Close

Socket calls in sequence (TCP) Client side Socket Bind Connect Send Recv Close Server side Socket Bind Listen Accept Recv Send Close

Socket programming is... baroque * The socket calls were standardized a long time ago – There were lots of different network standards – Each with their own addressing information – The socket calls need to be general enough to handle them all (and any other ones that might come along) – Easy things are much harder than they should need to be * Extravagant, complex, or bizarre

socket() Create a communication endpoint Returns a file descriptor (an integer) which must be used in every call referring to this communication endpoint Returns -1 on error – errno is set to indicate why

socket() socket(domain, type, protocol) domain: AF_UNIX, AF_INET, AF_INET6 type: SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET, SOCK_RDM protocol: 0 means the only available protocol

socket addresses struct sockaddr An address that can be in any address family struct sockaddr_in An address in the INET (v4) address family Consists of a 32 bit IP address ( s_addr ) and a 16 bit port ( s_port ) Both in network byte order All unused bits in an address must be 0

socket addresses – C evil You allocate variables of type struct sockaddr But then treat them as if they are of type struct sockaddr_in For example: struct sockaddr s; struct sockaddr_in *si = (struct sockaddr_in *) &s; si->sin_family... si->sin_addr... si->sin_port...

bind() Establish a local address for a socket The local address is the address of this endpoint, so will be the current machine’s IP address and a port Returns 0 on success Returns -1 on error – errno is set to indicate why

bind() bind(socket, addr, addrlen) socket: a file descriptor returned from socket() addr: a pointer to a struct sockaddr addrlen: the length of the addr

sendto() Sends data through an unconnected socket The destination address is specified each time Returns the number of bytes sent on success Returns -1 on error – errno is set to indicate why

sendto() sendto(socket, buf, len, flags, destaddr, addrlen) socket: the socket file descriptor buf: pointer to data to send len: length of data to send flags: almost always 0 destaddr: a struct sockaddr * indicating the destination socket addrlen: the length of destaddr

recvfrom() Receives data through an unconnected socket The source address is specified each time Returns the number of bytes received on success Returns -1 on error – errno is set to indicate why

recvfrom() recvfrom(socket, buf, len, flags, srcaddr, addrlen) socket: the socket file descriptor buf: pointer to recv buffer len: length of recv buffer flags: almost always 0 srcaddr: a struct sockaddr * for receiving the source socket addr addrlen: a pointer to the length of srcaddr, changed by the call

close() closes a socket makes it unusable in the future for sending or receiving Returns 0 on success Returns -1 on error – errno is set to indicate why

close() close(socket) socket: the socket file descriptor

Look at a couple of examples All are in the network directory of the lectures repository – receive.c – send.c – shared.c

Socket calls in sequence (TCP) Client side Socket Bind Connect Send Recv Close Server side Socket Bind Listen Accept Recv Send Close

listen() Indicates that the socket is to be used for accepting incoming connections Done on the “server” side only Returns 0 on success Returns -1 on error – errno is set to indicate why

listen() listen(socket, backlog) socket: a file descriptor returned from socket() backlog: the number of incoming connection requests to allow to queue

connect() Connects a socket to a destination socket whose address is given as an argument Done on the “client” side only Returns 0 on success Returns -1 on error – errno is set to indicate why

connect() connect(socket, addr, addrlen) socket: a file descriptor returned from socket() addr: a pointer to a struct sockaddr addrlen: the length of the addr

accept() Accepts an incoming connection – waits for an incoming connection request Done on the “server” side only Returns a new socket file descriptor on success Returns -1 on error – errno is set to indicate why

accept() accept(socket, addr, addrlen) socket: a file descriptor returned from socket() addr: a pointer to a struct sockaddr addrlen: a pointer to the length of the addr, changed by the call

send() Sends data through a connected socket No destination address is specified Returns the number of bytes sent on success Returns -1 on error – errno is set to indicate why

send() send(socket, buf, len, flags) socket: the socket file descriptor buf: pointer to data to send len: length of data to send flags: almost always 0

recv() Receives data through a connected socket No source address is indicated Returns the number of bytes received on success – 0 usually means “end-of-file”, meaning there will be no more data to receive, ever again Returns -1 on error – errno is set to indicate why

recv() recv(socket, buf, len, flags) socket: the socket file descriptor buf: pointer to recv buffer len: length of recv buffer flags: almost always 0

shutdown() Indicates that an application is done with either sending or receiving data on a socket When a connected stream socket is shutdown for sends, then the other side receives “end- of-file” Returns 0 on success Returns -1 on error – errno is set to indicate why

shutdown() shutdown(socket, how) socket: a file descriptor returned from socket() how: SHUT_RD no more receives SHUT_WR no more sends SHUT_RDWR no more communication at all

Web resources There are lots of network programming guides on the net The “standard” reference is Beej’s guide It has become way too long over the years The 1999 version is pretty good – On the web pointed to by the lab page

Bits of advice Pay very close attention to the arguments to every network call – Especially addresses Check the returned value from every network call – If something goes wrong, you need to know about it early – And bail out, or start over and try again