Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.

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.
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()
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.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
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.
Introduction to Socket Programming Advisor: Quincy Wu Speaker: Kuan-Ta Lu Date: Nov. 25, 2010.
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.
Review: How to create a TCP end point? What is the right format for sin_port and sin_addr in the sockaddr_in data structure? How many different ways we.
Remote Shell CS230 Project #4 Assigned : Due date :
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS A: Windows Networking A.2. Windows Sockets.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
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.
Advanced Sockets API-II Vinayak Jagtap
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
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 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.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
UNIX Internet Socket API
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
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 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.
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.
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.
Socket programming Péter Verhás August 2002
CS 3700 Networks and Distributed Systems
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Review: TCP Client-Server Interaction
Tutorial on Socket Programming
Transport layer API: Socket Programming
CS 3700 Networks and Distributed Systems
Introduction to Socket Programming
Socket Programming in C
TCP Sockets Programming
Socket Programming.
TCP/IP Socket Programming in C
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Sockets.
Presentation transcript:

Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages ● UNIX Network Programming by W. Richard Stevens ● TCP/IP Illustrated by W. Richard Stevens

Berkeley Sockets ● Standard C functions for network operations ● Standardized in: – POSIX – SUSv2 ● Other languages call the C functions ● Present in all major Unix operating systems – GNU/Linux, Solaris, BSD, Mac OS X ● Microsoft Windows uses a variant

Overview ● A word about byte order ● Sockets ● How to Create and Manipulate Them ● Datagram versus Stream Protocols ● Datagram Protocol Functions ● Multiplexing with Select

Sockets ● A socket is a 5-tuple: – (Protocol, Local Address, Local Port, Remote Address, Remote Port) ● For example: ● (TCP, salix.cs.uoregon.edu, 32905, 80) ● Socket data structures are stored in the kernel. ● Applications manipulate them with function calls: – socket(): Allocate a socket and return a descriptor – bind(): Set the local address and port – connect(): Set the remote address and port

Byte Order ● Little Endian – Lowest-order byte has the lowest address – Bytes are ordered in the same way as bits – Used on x86 platform ● Big Endian – Highest-order byte has the highest addreess – Bytes are ordered in the way we normally view memory – Used on PowerPC, 68k, Sparc – Official network byte order for Internet protocols

Byte Order ● Special macros exist for portably converting between network byte order and host byte order: – htonl() : convert a 32-bit integer from host to network byte order – htons() : convert a 16-bit integer from host to network byte order – ntohl() : convert a 32-bit integer from network to host byte order – ntohs() : convert a 16-bit integer from network to host byte order

Using socket() int socket(int domain, int type, int protocol); ● Creates a socket and returns a descriptor ● Parameters: – domain : the type of addressing used ● PF_INET, PF_UNIX, PF_INET6 – type: the type of communication ● SOCK_STREAM, SOCK_DGRAM – protocol: the exact protocol to use, usually zero ● Examples: – s = socket(AF_INET, SOCK_STREAM, 0); – s = socket(AF_UNIX, SOCK_DGRAM, 0);

The Socket Address Structure ● Important data structure, used by connect(), bind(), and others struct sockaddr { sa_family_tsa_family; charsa_data[14]; }; ● Different types are used based on address type struct sockaddr_in { sa_family_tsin_family; in_port_tsin_port; struct in_addrsin_addr; charsin_unused; };

The Socket Address Structure ● Another example: Unix domain sockets – Host address is implied – Port is a path in the filesystem struct sockaddr_un { sa_family_tsun_family; charsun_path[108]; }

Using connect() int connect (int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); ● Used by clients to specify a server ● Parameters: – s : A socket previously returned by socket() – name : A socket address structure, specifying the server – namelen : The size of the socket address structure

Connecting to a Web Server int err; struct sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons(80); server.sin_addr.s_addr = htonl (0x7f000001); err = connect (s, &sockaddr, sizeof sockaddr); if (err < 0) perror ("Error calling connect()");

Connecting to a Unix Domain Server int err; struct sockaddr_un server; server.sun_family = AF_UNIX; strcpy (server.sun_path, "/tmp/server-sock"); err = connect (s, &server, sizeof server); if (err < 0) perror ("Error calling connect()");

Using bind int bind (int sockfd, const struct sockaddr *my_addr, socklen_t addrlen); ● Sets the local address and port of the socket ● Not needed for the client side when using the Internet address family ● Parameters are the same as connect()

Connectionless Datagram Protocols ● UDP is the canonical example ● This is what you will use in your first assignment ● Transmitting is done using send() or sendto() ● Receiving is done using recv() or recvfrom()

Connection-Oriented Stream Protocols ● TCP is the canonical example ● Transmitting is done with write() ● Receiving is done with read() ● Additional functions for servers: – listen() : configures a socket to wait for new connections – accept() : returns a new socket for a new connection

send() and sendto() ssize_t send (int s, const void *msg, size_t len, int flags); ssize_t sendto (int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen); ● These functions are used to send datagrams. ● sendto() is like send() mixed with connect() ● Parameters: – s : A bound socket. For send(), the socket must also be connected. – msg : The data for the datagram – len : The size of the data – flags : Usually zero

recv() and recvfrom() ssize_t recv(int s, void *buf, size_t len, int flags); ssize_t recvfrom (int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); ● These functions are used to receive datagrams. ● Parameters: – s : A bound socket. For recv(), it should also be connected. – buf : Memory to store the datagram – len : The size of buf. – flags : Usually zero – from : Memory for a socket address structure – fromlen : Before calling, the size of from. After calling, the size of from actually used.

Multiplexing with select() int select (int n, fd_set *readfds, fd_set *writefds, fd_set exceptfds, struct timeval *timeout); ● select() is used to block on multiple sockets at once. ● It can also be used to block on input from the user. ● Parameters: – n : The largest file descriptor in use, plus one – readfds : Wake when any of these descriptors are readable – writefds : Wake when any of these descriptors are writable – exceptfds : Wake when any of these descriptors have exceptions, often NULL – timeout : The maximum duration to block

Multiplexing with select() ● fd_set structures are manipulated with special macros. FD_CLR (int fd, fd_set *set); FD_ISSET (int fd, fd_set *set); FD_SET (int fd, fd_set *set); FD_ZERO (fd_set *set);

Summary ● Sockets ● How to Create and Manipulate Them ● Datagram versus Stream Protocols ● Datagram Protocol Functions ● Multiplexing with Select